File: autodox

package info (click to toggle)
fex 20200429-1
  • links: PTS, VCS
  • area: non-free
  • in suites: sid
  • size: 3,532 kB
  • sloc: perl: 32,103; sh: 410; javascript: 53; makefile: 42
file content (128 lines) | stat: -rwxr-xr-x 2,461 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/perl -w

use Getopt::Std;

$0 =~ s:(.*)/:: or die "$0: unknown path\n";
$fexdox = $1.'/fexdox';
$| = 1;

$usage = "$0: fexdox new created or modified files
usage: $0 [-v] [-n] [-w SECONDS] [-m MAX] [-d] DIRECTORY
options:
  -v  verbose mode
  -n  dry-run without transfer
  -w  wait SECONDS to check for new files (default: 60)
  -m  limit throuput to MAX kB/s
  -d  delete extraneous files
";

$ENV{FUA} = 'autodox';

$opt_h = $opt_v = $opt_n = $opt_d = 0;
$opt_w = 60;
$opt_i = $opt_m = '';
getopts('hvndw:i:m:') or die $usage;

$wait = 10;
$wait = $1 if $opt_w =~ /^(\d+)$/;

if ($opt_h) {
  print $usage;
  exit;
}

if (scalar(@ARGV) != 1) {
  die $usage;
}

$dir = shift;
chdir $dir or die "$0: $dir - $!\n";

@fexdox = ($fexdox,'-s');
push @fexdox,'-d' if $opt_d;
push @fexdox,('-m',$opt_m) if $opt_m;
push @fexdox,('-i',$opt_i) if $opt_i;

$comment = 'waiting for new files... ';
%files = ();
$t0 = time;
collect('.');
vsystem(@fexdox,'.');

for (;;) {
  if (-t STDOUT) {
    for (my $i = $wait; $i; $i--) {
      printf "%s%3d\r",$comment,$i;
      sleep 1;
    }
    print ' ' x length($comment),"   \r";
  } else {
    sleep $wait;
  }

  %ofiles = %files;
  @files = ();
  $t0 = time;
  collect('.');

  foreach my $file (keys %files) {
    if ($files{$file} != ($ofiles{$file}||0)) {
      vsystem(@fexdox,'.');
      last;
    }
  }

}



# collect all files
sub collect {
  my $DIR;
  my ($dir,$file);
  local $_;

  foreach $dir (@_) {
    if (-l $dir or -f $dir) {
      $file = $dir;
      if (-l $file or -r $file) {
        if (my @s = lstat $file) {
          $files{$file} = $s[9];
        }
      } else {
        warn "$prg: cannot read $::dir/$file - $!\n";
        $error++;
      }
    } elsif (opendir $DIR,$dir) {
      my @files = ();
      while (defined(my $file = readdir $DIR)) {
        next if $file =~ /^\.\.?$/;
        $file = "$dir/$file" if $dir ne '.';
        if ($file =~ s/\n/?/g) {
          warn "$prg: ignoring file with newline character in filename: $file\n";
          next;
        }
        push @files,$file;
      }
      closedir $DIR;
      collect(@files);
    } else {
      warn "$prg: cannot open $::dir/$dir/ - $!\n";
      $error++;
    }
  }
}


sub isodate {
  my @d = localtime shift;
  return sprintf('%d-%02d-%02d %02d:%02d:%02d',
                 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]);
}


sub vsystem {
  print "\$ @_\n" if $opt_v;
  system @_ unless $opt_n;
  print "\n";
}