]> arthur.barton.de Git - netatalk.git/blob - contrib/shell_utils/apple_mv.in
Revert change from 3194183
[netatalk.git] / contrib / shell_utils / apple_mv.in
1 #!@PERL@
2
3 # $Id: apple_mv.in,v 1.1 2002-01-17 05:59:25 srittau Exp $
4
5 $USAGE = <<USAGE;
6 Usage: $0 filename1 filename2
7        $0 filename ...  directory
8 Do an apple move, moving the resource fork as well
9 USAGE
10
11 @from = @ARGV; pop(@from);
12 $to = $ARGV[-1];
13
14 if (-f $to && @from > 1) { die $USAGE; }
15
16 foreach $from (@from) {
17     if (!-f $from) {    
18         print STDERR "file $from does not exist\n";
19         die $USAGE;
20     }
21
22     if (!-d $to && @from >1) {
23         print STDERR "directory $to does not exist\nCan't move multiple files into one file.\n";
24         die $USAGE;
25     }
26     
27     $from = escape_bad_chars($from);
28     $to = escape_bad_chars($to);
29     $cmd = "mv $from $to";
30     system $cmd || die "error executing $cmd";
31     
32     ($from_dir, $from_file) = split_dir_file($from);
33
34     if (-d $to) {
35         if (!-d "$to/.AppleDouble") {
36             mkdir("$to/.AppleDouble", 0777);
37         }
38         $cmd = "mv $from_dir/.AppleDouble/$from_file $to/.AppleDouble/$from_file";
39     } else {
40         ($to_dir, $to_file) = split_dir_file($to);
41
42         if (!-d $to_dir) {
43             print STDERR "directory $to does not exist\n";
44             die $USAGE;
45         }
46     
47         if (!-d "$to_dir/.AppleDouble") {
48             mkdir("$to_dir/.AppleDouble", 0777);
49         }
50         $cmd = "mv $from_dir/.AppleDouble/$from_file $to_dir/.AppleDouble/$to_file";
51     }
52
53     system $cmd || die "error executing $cmd";
54 }
55
56 sub escape_bad_chars {
57     my($file) = @_;
58     $file=~s/([^a-zA-Z0-9.-_])/\\$1/;
59     return $file;
60 }
61
62 # split a file path into a directory and file name.
63 sub split_dir_file {
64     my $path = shift;
65
66     @path_elems = split(/\//, $path);
67
68     my $file = pop(@path_elems);
69     my $dir;
70     if (!@path_elems) {
71         $dir = '.';
72     } else {
73         $dir = join('/', @path_elems);
74     }
75
76     $dir, $file;
77 }
78
79