]> arthur.barton.de Git - netatalk.git/blob - contrib/shell_utils/apple_mv
ae5c5ded697f184e8f53e99bd983a44b870ac047
[netatalk.git] / contrib / shell_utils / apple_mv
1 #! /usr/bin/perl
2
3 # $Header: /home/ralph/netatalk/rsync/netatalk/contrib/shell_utils/Attic/apple_mv,v 1.3 2001-10-17 05:21:52 lancel Exp $
4 #
5 # $Log: apple_mv,v $
6 # Revision 1.3  2001-10-17 05:21:52  lancel
7 # Added error checking against multiple files to one file. Eliminated
8 # depreciated special variable. $ARGV($#ARGV) to $ARGV(-1)
9 #
10 # Revision 1.2  2000/08/09 14:12:06  rufustfirefly
11 # /usr/local/bin/perl -> /usr/bin/perl and portability fixes
12 #
13 # Revision 1.1  2000/08/09 14:08:06  rufustfirefly
14 # Shell utils from http://www.-genome.wi.mit.edu/ftp/distribution/software/Bass/bass-1.29/apple_util/ (initial import)
15 #
16 # Revision 1.2  1996/06/14 18:06:58  lstein
17 # Fixed bugs in the character escaping routines.
18 #
19 # Revision 1.2  1996/02/09  18:44:44  will
20 # fix to usage string
21 #
22 # Revision 1.1  1996/02/09  18:21:35  will
23 # Initial revision
24 #
25 #
26
27 $USAGE = <<USAGE;
28 Usage: $0 filename1 filename2
29        $0 filename ...  directory
30 Do an apple move, moving the resource fork as well
31 USAGE
32
33 @from = @ARGV; pop(@from);
34 $to = $ARGV[-1];
35
36 if (-f $to && @from > 1) { die $USAGE; }
37
38 foreach $from (@from) {
39     if (!-f $from) {    
40         print STDERR "file $from does not exist\n";
41         die $USAGE;
42     }
43
44     if (!-d $to && @from >1) {
45         print STDERR "directory $to does not exist\nCan't move multiple files into one file.\n";
46         die $USAGE;
47     }
48     
49     $from = escape_bad_chars($from);
50     $to = escape_bad_chars($to);
51     $cmd = "mv $from $to";
52     system $cmd || die "error executing $cmd";
53     
54     ($from_dir, $from_file) = split_dir_file($from);
55
56     if (-d $to) {
57         if (!-d "$to/.AppleDouble") {
58             mkdir("$to/.AppleDouble", 0777);
59         }
60         $cmd = "mv $from_dir/.AppleDouble/$from_file $to/.AppleDouble/$from_file";
61     } else {
62         ($to_dir, $to_file) = split_dir_file($to);
63
64         if (!-d $to_dir) {
65             print STDERR "directory $to does not exist\n";
66             die $USAGE;
67         }
68     
69         if (!-d "$to_dir/.AppleDouble") {
70             mkdir("$to_dir/.AppleDouble", 0777);
71         }
72         $cmd = "mv $from_dir/.AppleDouble/$from_file $to_dir/.AppleDouble/$to_file";
73     }
74
75     system $cmd || die "error executing $cmd";
76 }
77
78 sub escape_bad_chars {
79     my($file) = @_;
80     $file=~s/([^a-zA-Z0-9.-_])/\\$1/;
81     return $file;
82 }
83
84 # split a file path into a directory and file name.
85 sub split_dir_file {
86     my $path = shift;
87
88     @path_elems = split(/\//, $path);
89
90     my $file = pop(@path_elems);
91     my $dir;
92     if (!@path_elems) {
93         $dir = '.';
94     } else {
95         $dir = join('/', @path_elems);
96     }
97
98     $dir, $file;
99 }
100
101