Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / perl5 / eg / van / unvanish
1 #!/usr/bin/perl
2
3 # $RCSfile: unvanish,v $$Revision: 4.1 $$Date: 92/08/07 17:20:52 $
4
5 sub it {
6     if ($olddir ne '.') {
7         chop($pwd = `pwd`) if $pwd eq '';
8         (chdir $olddir) || die "Directory $olddir is not accesible";
9     }
10     unless ($olddir eq '.deleted') {
11         if (-d '.deleted') {
12             chdir '.deleted' || die "Directory .deleted is not accesible";
13         }
14         else {
15             chop($pwd = `pwd`) if $pwd eq '';
16             die "Directory .deleted does not exist" unless $pwd =~ /\.deleted$/;
17         }
18     }
19     print `mv $startfiles$filelist..$force`;
20     if ($olddir ne '.') {
21         (chdir $pwd) || die "Can't get back to original directory $pwd: $!\n";
22     }
23 }
24
25 if ($#ARGV < 0) {
26     open(lastcmd,'.deleted/.lastcmd') || 
27         open(lastcmd,'.lastcmd') || 
28             die "No previous vanish in this dir";
29     $ARGV = <lastcmd>;
30     close(lastcmd);
31     @ARGV = split(/[\n ]+/,$ARGV);
32 }
33
34 while ($ARGV[0] =~ /^-/) {
35     $_ = shift;
36     /^-f/ && ($force = ' >/dev/null 2>&1');
37     /^-i/ && ($interactive = 1);
38     if (/^-+$/) {
39         $startfiles = '- ';
40         last;
41     }
42 }
43
44 while ($file = shift) {
45     if ($file =~ s|^(.*)/||) {
46         $dir = $1;
47     }
48     else {
49         $dir = '.';
50     }
51
52     if ($dir ne $olddir) {
53         do it() if $olddir;
54         $olddir = $dir;
55     }
56
57     if ($interactive) {
58         print "unvanish: restore $dir/$file? ";
59         next unless <stdin> =~ /^y/i;
60     }
61
62     $filelist .= $file; $filelist .= ' ';
63
64 }
65
66 do it() if $olddir;