| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
# CLI client for the F*EX service for file packing |
| 4 |
# |
| 5 |
# see also: fexsend, fexpush |
| 6 |
# |
| 7 |
# Author: Ulli Horlacher <[email protected]> |
| 8 |
# |
| 9 |
# Perl Artistic Licence |
| 10 |
|
| 11 |
use 5.010; |
| 12 |
use Getopt::Std; |
| 13 |
use Term::ReadLine; |
| 14 |
use Sys::Hostname; |
| 15 |
|
| 16 |
$prg = $0; |
| 17 |
$prg =~ s:.*/::; |
| 18 |
$| = 1; |
| 19 |
|
| 20 |
$ENV{FUA} = 'fexpack'; |
| 21 |
|
| 22 |
$usage = mjoin( |
| 23 |
"$prg: pack file(s) on fexserver (for copy&paste)", |
| 24 |
"usage: $prg [-k DAYS] [-a ARCHIVE] FILE...", |
| 25 |
"usage: $prg -u", |
| 26 |
"options:", |
| 27 |
" -k keep DAYS (default: 1 day)", |
| 28 |
" -a force ARCHIVE package (may have suffix .tar .tgz .zip .7z)", |
| 29 |
" -u generate upload function for copy&paste", |
| 30 |
"examples:", |
| 31 |
" $prg *.jpg", |
| 32 |
" $prg -k 10 -a PDF.zip *.pdf", |
| 33 |
"see also: xxx, fexsend", |
| 34 |
); |
| 35 |
|
| 36 |
$opt_h = $opt_v = $opt_u = $opt_U = 0; |
| 37 |
$opt_k = 1; |
| 38 |
$opt_a = ''; |
| 39 |
getopts('hvuUk:a:') or die $usage; |
| 40 |
$opt_u ||= $opt_U; |
| 41 |
|
| 42 |
if ($opt_h) { |
| 43 |
print $usage; |
| 44 |
exit; |
| 45 |
} |
| 46 |
|
| 47 |
if ($opt_u) { |
| 48 |
$_ = vsystem('fexsend -~ GENUKEY|'); |
| 49 |
if ($? == 0 and /FUP=(http.+)/) { |
| 50 |
my $fup = $1; |
| 51 |
print "# to get upload function (one day valid) copy&paste:\n"; |
| 52 |
print "eval \$(curl -s $fup)\n"; |
| 53 |
print if $opt_v; |
| 54 |
} |
| 55 |
exit $?; |
| 56 |
} |
| 57 |
|
| 58 |
# $opt_v = 1; |
| 59 |
|
| 60 |
if (scalar(@ARGV) == 0 ) { |
| 61 |
die $usage; |
| 62 |
} elsif (scalar(@ARGV) == 1 and not $opt_a) { |
| 63 |
$arg = $ARGV[0]; |
| 64 |
$arg =~ s:/+$::; |
| 65 |
if ($arg =~ /\.(tar|tgz|tar\.[\w\.]+|zip|7z)$/) { |
| 66 |
vexec(qw"fexsend -k",$opt_k,$arg,'.'); |
| 67 |
} else { |
| 68 |
vexec(qw"fexsend -k",$opt_k,"-a","$arg.tar",$arg,'.'); |
| 69 |
} |
| 70 |
} else { |
| 71 |
if ($opt_a) { |
| 72 |
$archive = $opt_a; |
| 73 |
} else { |
| 74 |
$archive = inputline("archive name: ") or exit; |
| 75 |
} |
| 76 |
$archive =~ s/[^\w.+-]/_/g; |
| 77 |
$archive .= '.tar' if $archive !~ /\.(tar|tgz|zip|7z)$/; |
| 78 |
vexec(qw'fexsend -k',$opt_k,'-a',$archive,@ARGV,'.'); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
sub inputline { |
| 83 |
my $prompt = shift; |
| 84 |
my $default = shift||''; |
| 85 |
my $term = new Term::ReadLine $prg; |
| 86 |
|
| 87 |
$term->ornaments(0) unless $ENV{PERL_RL}; |
| 88 |
return $term->readline($prompt,$default)||''; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
sub mjoin { |
| 93 |
local $_; |
| 94 |
my $s; |
| 95 |
$s .= $_."\n" foreach @_; |
| 96 |
return $s; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
sub shellquote { |
| 101 |
local $_ = shift; |
| 102 |
s/([^\w\@\/!^%:_.,=+-])/\\$1/g; |
| 103 |
return $_; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
sub vexec { |
| 108 |
vsystem(@_); |
| 109 |
exit $?; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
sub vsystem { |
| 114 |
my @cmd = @_; |
| 115 |
my $cmd; |
| 116 |
my $shellmeta = '[\\\'"`*?&|<>(){};]'; |
| 117 |
local $_; |
| 118 |
|
| 119 |
if ($opt_v) { |
| 120 |
if (-t STDIN) { print STDERR '$ ' } |
| 121 |
else { print STDERR '| ' } |
| 122 |
if (scalar(@cmd) == 1) { |
| 123 |
warn "@cmd\n"; |
| 124 |
} else { |
| 125 |
my @w; |
| 126 |
foreach (@cmd) { |
| 127 |
if (/\'/) { |
| 128 |
push @w,shellquote($_); |
| 129 |
} elsif (/[^\w\/:=~^%@,.+-]/) { |
| 130 |
push @w,"'$_'"; |
| 131 |
} else { |
| 132 |
push @w,$_; |
| 133 |
} |
| 134 |
} |
| 135 |
warn "@w\n"; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
$_ = "@cmd"; |
| 140 |
if (scalar(@cmd) == 1 and /\s/ and not (/^\w+=/ or /$shellmeta/)) { |
| 141 |
@cmd = split; |
| 142 |
} |
| 143 |
|
| 144 |
if (scalar(@cmd) == 1) { |
| 145 |
$cmd = "@cmd"; |
| 146 |
$cmd =~ s/\|&$/ 2>&1|/; |
| 147 |
if ($cmd =~ s/\|$//) { |
| 148 |
my @a = `$cmd`; |
| 149 |
if (wantarray) { |
| 150 |
map { chomp } @a; |
| 151 |
return @a; |
| 152 |
} else { |
| 153 |
chomp $a[0] if scalar(@a) == 1; |
| 154 |
return join('',@a); |
| 155 |
} |
| 156 |
} else { |
| 157 |
system $cmd; |
| 158 |
} |
| 159 |
} else { |
| 160 |
system @cmd; |
| 161 |
} |
| 162 |
} |