5
my $WGET = '/usr/bin/wget' ;
6
my $RSYNC = '/usr/bin/rsync' ;
9
my $tmp_dir = '/tmp/mirmon' ;
11
my $prog = substr($0,rindex($0,'/')+1) ;
13
Usage: $prog [-v] [-q] [-d] [-t timeout] url
16
option d : show debug info
17
option t : timeout in seconds (default $timeout)
19
rsync://host.dom.com/module/file
20
http://host.dom.com/some/file
21
ftp://host.dom.com/some/file
23
sub Usage { die "$_[0]$Usage" ; }
24
sub Error { die "$prog: $_[0]\n" ; }
25
sub Warn { warn "$prog: $_[0]\n" ; }
27
# usage: &GetOptions(ARG,ARG,..) defines $opt_ID as 1 or user spec'ed value
28
# usage: &GetOptions(\%opt,ARG,ARG,..) defines $opt{ID} as 1 or user value
29
# ARG = 'ID' | 'ID=SPC' | 'ID:SPC' for no-arg, required-arg or optional-arg
30
# ID = perl identifier
31
# SPC = i|f|s for integer, fixedpoint real or string argument
34
Getopt::Long::config('no_ignore_case') ;
35
my %opt = () ; Usage('') unless GetOptions
36
( \%opt, qw(v q d t=i) ) ;
37
Usage("Arg count\n") unless @ARGV == 1 ;
40
$timeout = $opt{t} if exists $opt{t} ;
43
my $opt_v = '' ; $opt_v = '-v' if $opt{v} ;
44
my $opt_q = '' ; $opt_q = '-q' if $opt{q} ;
46
# make a tmp dir for rsync
48
-d $tmp_dir or mkdir $tmp_dir or Error "can't mkdir $tmp_dir ($!)" ;
50
# handle rsync urls with rsync
51
# rewrite rysnc://host.dom.com/module/file -> host.dom.com::module/file
52
# handle ftp/http urls with wget
54
if ( $url =~ m!^rsync://(.*)$! )
58
my $TMP = "$tmp_dir/$dst" ;
60
unlink $TMP ; # ignore status
61
my $cmd = "$RSYNC $opt_v $opt_q --no-motd --timeout $timeout $src $TMP" ;
62
Warn sprintf "'%s'\n", $cmd if $opt{d} ;
65
{ print <TMP> ; close TMP ; }
67
{ Warn "can't open $TMP" ; }
70
{ my $cmd = "$WGET -O - $opt_v $opt_q -t 1 -T $timeout $url |" ;
71
Warn sprintf "'%s'\n", $cmd if $opt{d} ;
73
{ print <CMD> ; close CMD ; }
75
{ Warn "can't popen $cmd ($!)" ; }