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