File: fexpack

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 (162 lines) | stat: -rwxr-xr-x 3,068 bytes parent folder | download | duplicates (2)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/perl -w

# CLI client for the F*EX service for file packing
#
# see also: fexsend, fexpush
#
# Author: Ulli Horlacher <framstag@belwue.de>
#
# Perl Artistic Licence

use 5.010;
use Getopt::Std;
use Term::ReadLine;
use Sys::Hostname;

$prg = $0;
$prg =~ s:.*/::;
$| = 1;

$ENV{FUA} = 'fexpack';

$usage = mjoin(
  "$prg: pack file(s) on fexserver (for copy&paste)",
  "usage: $prg [-k DAYS] [-a ARCHIVE] FILE...",
  "usage: $prg -u",
  "options:",
  "  -k  keep DAYS (default: 1 day)",
  "  -a  force ARCHIVE package (may have suffix .tar .tgz .zip .7z)",
  "  -u  generate upload function for copy&paste",
  "examples:",
  "  $prg *.jpg",
  "  $prg -k 10 -a PDF.zip *.pdf",
  "see also: xxx, fexsend",
);

$opt_h = $opt_v = $opt_u = $opt_U = 0;
$opt_k = 1;
$opt_a = '';
getopts('hvuUk:a:') or die $usage;
$opt_u ||= $opt_U;

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

if ($opt_u) {
  $_ = vsystem('fexsend -~ GENUKEY|');
  if ($? == 0 and /FUP=(http.+)/) {
    my $fup = $1;
    print "# to get upload function (one day valid) copy&paste:\n";
    print "eval \$(curl -s $fup)\n";
    print if $opt_v;
  }
  exit $?;
}

# $opt_v = 1;

if (scalar(@ARGV) == 0 ) {
  die $usage;
} elsif (scalar(@ARGV) == 1 and not $opt_a) {
  $arg = $ARGV[0];
  $arg =~ s:/+$::;
  if ($arg =~ /\.(tar|tgz|tar\.[\w\.]+|zip|7z)$/) {
    vexec(qw"fexsend -k",$opt_k,$arg,'.');
  } else {
    vexec(qw"fexsend -k",$opt_k,"-a","$arg.tar",$arg,'.');
  }
} else {
  if ($opt_a) {
    $archive = $opt_a;
  } else {
    $archive = inputline("archive name: ") or exit;
  }
  $archive =~ s/[^\w.+-]/_/g;
  $archive .= '.tar' if $archive !~ /\.(tar|tgz|zip|7z)$/;
  vexec(qw'fexsend -k',$opt_k,'-a',$archive,@ARGV,'.');
}


sub inputline {
  my $prompt = shift;
  my $default = shift||'';
  my $term = new Term::ReadLine $prg;

  $term->ornaments(0) unless $ENV{PERL_RL};
  return $term->readline($prompt,$default)||'';
}


sub mjoin {
  local $_;
  my $s;
  $s .= $_."\n" foreach @_;
  return $s;
}


sub shellquote {
  local $_ = shift;
  s/([^\w\@\/!^%:_.,=+-])/\\$1/g;
  return $_;
}


sub vexec {
  vsystem(@_);
  exit $?;
}


sub vsystem {
  my @cmd = @_;
  my $cmd;
  my $shellmeta = '[\\\'"`*?&|<>(){};]';
  local $_;

  if ($opt_v) {
    if (-t STDIN) { print STDERR '$ ' }
    else          { print STDERR '| ' }
    if (scalar(@cmd) == 1) {
      warn "@cmd\n";
    } else {
      my @w;
      foreach (@cmd) {
        if (/\'/) {
          push @w,shellquote($_);
        } elsif (/[^\w\/:=~^%@,.+-]/) {
          push @w,"'$_'";
        } else {
          push @w,$_;
        }
      }
      warn "@w\n";
    }
  }

  $_ = "@cmd";
  if (scalar(@cmd) == 1 and /\s/ and not (/^\w+=/ or /$shellmeta/)) {
    @cmd = split;
  }

  if (scalar(@cmd) == 1) {
    $cmd = "@cmd";
    $cmd =~ s/\|&$/ 2>&1|/;
    if ($cmd =~ s/\|$//) {
      my @a = `$cmd`;
      if (wantarray) {
        map { chomp } @a;
        return @a;
      } else {
        chomp $a[0] if scalar(@a) == 1;
        return join('',@a);
      }
    } else {
      system $cmd;
    }
  } else {
    system @cmd;
  }
}