| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
use Getopt::Std; |
| 4 |
|
| 5 |
$0 =~ s:(.*)/:: or die "$0: unknown path\n"; |
| 6 |
$fexdox = $1.'/fexdox'; |
| 7 |
$| = 1; |
| 8 |
|
| 9 |
$usage = "$0: fexdox new created or modified files |
| 10 |
usage: $0 [-v] [-n] [-w SECONDS] [-m MAX] [-d] DIRECTORY |
| 11 |
options: |
| 12 |
-v verbose mode |
| 13 |
-n dry-run without transfer |
| 14 |
-w wait SECONDS to check for new files (default: 60) |
| 15 |
-m limit throuput to MAX kB/s |
| 16 |
-d delete extraneous files |
| 17 |
"; |
| 18 |
|
| 19 |
$ENV{FUA} = 'autodox'; |
| 20 |
|
| 21 |
$opt_h = $opt_v = $opt_n = $opt_d = 0; |
| 22 |
$opt_w = 60; |
| 23 |
$opt_i = $opt_m = ''; |
| 24 |
getopts('hvndw:i:m:') or die $usage; |
| 25 |
|
| 26 |
$wait = 10; |
| 27 |
$wait = $1 if $opt_w =~ /^(\d+)$/; |
| 28 |
|
| 29 |
if ($opt_h) { |
| 30 |
print $usage; |
| 31 |
exit; |
| 32 |
} |
| 33 |
|
| 34 |
if (scalar(@ARGV) != 1) { |
| 35 |
die $usage; |
| 36 |
} |
| 37 |
|
| 38 |
$dir = shift; |
| 39 |
chdir $dir or die "$0: $dir - $!\n"; |
| 40 |
|
| 41 |
@fexdox = ($fexdox,'-s'); |
| 42 |
push @fexdox,'-d' if $opt_d; |
| 43 |
push @fexdox,('-m',$opt_m) if $opt_m; |
| 44 |
push @fexdox,('-i',$opt_i) if $opt_i; |
| 45 |
|
| 46 |
$comment = 'waiting for new files... '; |
| 47 |
%files = (); |
| 48 |
$t0 = time; |
| 49 |
collect('.'); |
| 50 |
vsystem(@fexdox,'.'); |
| 51 |
|
| 52 |
for (;;) { |
| 53 |
if (-t STDOUT) { |
| 54 |
for (my $i = $wait; $i; $i--) { |
| 55 |
printf "%s%3d\r",$comment,$i; |
| 56 |
sleep 1; |
| 57 |
} |
| 58 |
print ' ' x length($comment)," \r"; |
| 59 |
} else { |
| 60 |
sleep $wait; |
| 61 |
} |
| 62 |
|
| 63 |
%ofiles = %files; |
| 64 |
@files = (); |
| 65 |
$t0 = time; |
| 66 |
collect('.'); |
| 67 |
|
| 68 |
foreach my $file (keys %files) { |
| 69 |
if ($files{$file} != ($ofiles{$file}||0)) { |
| 70 |
vsystem(@fexdox,'.'); |
| 71 |
last; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
# collect all files |
| 80 |
sub collect { |
| 81 |
my $DIR; |
| 82 |
my ($dir,$file); |
| 83 |
local $_; |
| 84 |
|
| 85 |
foreach $dir (@_) { |
| 86 |
if (-l $dir or -f $dir) { |
| 87 |
$file = $dir; |
| 88 |
if (-l $file or -r $file) { |
| 89 |
if (my @s = lstat $file) { |
| 90 |
$files{$file} = $s[9]; |
| 91 |
} |
| 92 |
} else { |
| 93 |
warn "$prg: cannot read $::dir/$file - $!\n"; |
| 94 |
$error++; |
| 95 |
} |
| 96 |
} elsif (opendir $DIR,$dir) { |
| 97 |
my @files = (); |
| 98 |
while (defined(my $file = readdir $DIR)) { |
| 99 |
next if $file =~ /^\.\.?$/; |
| 100 |
$file = "$dir/$file" if $dir ne '.'; |
| 101 |
if ($file =~ s/\n/?/g) { |
| 102 |
warn "$prg: ignoring file with newline character in filename: $file\n"; |
| 103 |
next; |
| 104 |
} |
| 105 |
push @files,$file; |
| 106 |
} |
| 107 |
closedir $DIR; |
| 108 |
collect(@files); |
| 109 |
} else { |
| 110 |
warn "$prg: cannot open $::dir/$dir/ - $!\n"; |
| 111 |
$error++; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
sub isodate { |
| 118 |
my @d = localtime shift; |
| 119 |
return sprintf('%d-%02d-%02d %02d:%02d:%02d', |
| 120 |
$d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
sub vsystem { |
| 125 |
print "\$ @_\n" if $opt_v; |
| 126 |
system @_ unless $opt_n; |
| 127 |
print "\n"; |
| 128 |
} |