Imported Upstream version 1.5
[debian/gzip] / build-aux / gnupload
1 #!/bin/sh
2 # Sign files and upload them.
3
4 scriptversion=2012-06-11.00; # UTC
5
6 # Copyright (C) 2004-2012 Free Software Foundation, Inc.
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3, or (at your option)
11 # any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 # Originally written by Alexandre Duret-Lutz <adl@gnu.org>.
22 # The master copy of this file is maintained in the gnulib Git repository.
23 # Please send bug reports and feature requests to bug-gnulib@gnu.org.
24
25 set -e
26
27 GPG='gpg --batch --no-tty'
28 conffile=.gnuploadrc
29 to=
30 dry_run=false
31 replace=
32 symlink_files=
33 delete_files=
34 delete_symlinks=
35 collect_var=
36 dbg=
37 nl='
38 '
39
40 usage="Usage: $0 [OPTION]... [CMD] FILE... [[CMD] FILE...]
41
42 Sign all FILES, and process them at selected destinations according to CMD.
43 <http://www.gnu.org/prep/maintain/html_node/Automated-FTP-Uploads.html>
44 explains further.
45
46 Commands:
47   --delete                 delete FILES from destination
48   --symlink                create symbolic links
49   --rmsymlink              remove symbolic links
50   --                       treat the remaining arguments as files to upload
51
52 Options:
53   --help                   print this help text and exit
54   --to DEST                specify one destination for FILES
55                            (multiple --to options are allowed)
56   --user NAME              sign with key NAME
57   --replace                allow replacements of existing files
58   --symlink-regex[=EXPR]   use sed script EXPR to compute symbolic link names
59   --dry-run                do nothing, show what would have been done
60                            (including the constructed directive file)
61   --version                output version information and exit
62
63 If --symlink-regex is given without EXPR, then the link target name
64 is created by replacing the version information with '-latest', e.g.:
65
66   foo-1.3.4.tar.gz -> foo-latest.tar.gz
67
68 Recognized destinations are:
69   alpha.gnu.org:DIRECTORY
70   savannah.gnu.org:DIRECTORY
71   savannah.nongnu.org:DIRECTORY
72   ftp.gnu.org:DIRECTORY
73                            build directive files and upload files by FTP
74   download.gnu.org.ua:{alpha|ftp}/DIRECTORY
75                            build directive files and upload files by SFTP
76   [user@]host:DIRECTORY    upload files with scp
77
78 Options and commands are applied in order.  If the file $conffile exists
79 in the current working directory, its contents are prepended to the
80 actual command line options.  Use this to keep your defaults.  Comments
81 (#) and empty lines in $conffile are allowed.
82
83 Examples:
84 1. Upload foobar-1.0.tar.gz to ftp.gnu.org:
85   gnupload --to ftp.gnu.org:foobar foobar-1.0.tar.gz
86
87 2. Upload foobar-1.0.tar.gz and foobar-1.0.tar.xz to ftp.gnu.org:
88   gnupload --to ftp.gnu.org:foobar foobar-1.0.tar.gz foobar-1.0.tar.xz
89
90 3. Same as above, and also create symbolic links to foobar-latest.tar.*:
91   gnupload --to ftp.gnu.org:foobar \\
92            --symlink-regex \\
93            foobar-1.0.tar.gz foobar-1.0.tar.xz
94
95 4. Upload foobar-0.9.90.tar.gz to two sites:
96   gnupload --to alpha.gnu.org:foobar \\
97            --to sources.redhat.com:~ftp/pub/foobar \\
98            foobar-0.9.90.tar.gz
99
100 5. Delete oopsbar-0.9.91.tar.gz and upload foobar-0.9.91.tar.gz
101    (the -- terminates the list of files to delete):
102   gnupload --to alpha.gnu.org:foobar \\
103            --to sources.redhat.com:~ftp/pub/foobar \\
104            --delete oopsbar-0.9.91.tar.gz \\
105            -- foobar-0.9.91.tar.gz
106
107 gnupload uses the ncftpput program to do the transfers; if you don't
108 happen to have an ncftp package installed, the ncftpput-ftp script in
109 the build-aux/ directory of the gnulib package
110 (http://savannah.gnu.org/projects/gnulib) may serve as a replacement.
111
112 Send patches and bug reports to <bug-gnulib@gnu.org>."
113
114 # Read local configuration file
115 if test -r "$conffile"; then
116   echo "$0: Reading configuration file $conffile"
117   conf=`sed 's/#.*$//;/^$/d' "$conffile" | tr "\015$nl" '  '`
118   eval set x "$conf \"\$@\""
119   shift
120 fi
121
122 while test -n "$1"; do
123   case $1 in
124   -*)
125     collect_var=
126     case $1 in
127     --help)
128       echo "$usage"
129       exit $?
130       ;;
131     --to)
132       if test -z "$2"; then
133         echo "$0: Missing argument for --to" 1>&2
134         exit 1
135       else
136         to="$to $2"
137         shift
138       fi
139       ;;
140     --user)
141       if test -z "$2"; then
142         echo "$0: Missing argument for --user" 1>&2
143         exit 1
144       else
145         GPG="$GPG --local-user $2"
146         shift
147       fi
148       ;;
149     --delete)
150       collect_var=delete_files
151       ;;
152     --replace)
153       replace="replace: true"
154       ;;
155     --rmsymlink)
156       collect_var=delete_symlinks
157       ;;
158     --symlink-regex=*)
159       symlink_expr=`expr "$1" : '[^=]*=\(.*\)'`
160       ;;
161     --symlink-regex)
162       symlink_expr='s|-[0-9][0-9\.]*\(-[0-9][0-9]*\)\{0,1\}\.|-latest.|'
163       ;;
164     --symlink)
165       collect_var=symlink_files
166       ;;
167     --dry-run|-n)
168       dry_run=:
169       ;;
170     --version)
171       echo "gnupload $scriptversion"
172       exit $?
173       ;;
174     --)
175       shift
176       break
177       ;;
178     -*)
179       echo "$0: Unknown option '$1', try '$0 --help'" 1>&2
180       exit 1
181       ;;
182     esac
183     ;;
184   *)
185     if test -z "$collect_var"; then
186       break
187     else
188       eval "$collect_var=\"\$$collect_var $1\""
189     fi
190     ;;
191   esac
192   shift
193 done
194
195 dprint()
196 {
197   echo "Running $* ..."
198 }
199
200 if $dry_run; then
201   dbg=dprint
202 fi
203
204 if test -z "$to"; then
205   echo "$0: Missing destination sites" >&2
206   exit 1
207 fi
208
209 if test -n "$symlink_files"; then
210   x=`echo "$symlink_files" | sed 's/[^ ]//g;s/  //g'`
211   if test -n "$x"; then
212     echo "$0: Odd number of symlink arguments" >&2
213     exit 1
214   fi
215 fi
216
217 if test $# = 0; then
218   if test -z "${symlink_files}${delete_files}${delete_symlinks}"; then
219     echo "$0: No file to upload" 1>&2
220     exit 1
221   fi
222 else
223   # Make sure all files exist.  We don't want to ask
224   # for the passphrase if the script will fail.
225   for file
226   do
227     if test ! -f $file; then
228       echo "$0: Cannot find '$file'" 1>&2
229       exit 1
230     elif test -n "$symlink_expr"; then
231       linkname=`echo $file | sed "$symlink_expr"`
232       if test -z "$linkname"; then
233         echo "$0: symlink expression produces empty results" >&2
234         exit 1
235       elif test "$linkname" = $file; then
236         echo "$0: symlink expression does not alter file name" >&2
237         exit 1
238       fi
239     fi
240   done
241 fi
242
243 # Make sure passphrase is not exported in the environment.
244 unset passphrase
245
246 # Reset PATH to be sure that echo is a built-in.  We will later use
247 # 'echo $passphrase' to output the passphrase, so it is important that
248 # it is a built-in (third-party programs tend to appear in 'ps'
249 # listings with their arguments...).
250 # Remember this script runs with 'set -e', so if echo is not built-in
251 # it will exit now.
252 if $dry_run; then :; else
253   PATH=/empty echo -n "Enter GPG passphrase: "
254   stty -echo
255   read -r passphrase
256   stty echo
257   echo
258 fi
259
260 if test $# -ne 0; then
261   for file
262   do
263     echo "Signing $file ..."
264     rm -f $file.sig
265     echo "$passphrase" | $dbg $GPG --passphrase-fd 0 -ba -o $file.sig $file
266   done
267 fi
268
269
270 # mkdirective DESTDIR BASE FILE STMT
271 # Arguments: See upload, below
272 mkdirective ()
273 {
274   stmt="$4"
275   if test -n "$3"; then
276     stmt="
277 filename: $3$stmt"
278   fi
279
280   cat >${2}.directive<<EOF
281 version: 1.2
282 directory: $1
283 comment: gnupload v. $scriptversion$stmt
284 EOF
285   if $dry_run; then
286     echo "File ${2}.directive:"
287     cat ${2}.directive
288     echo "File ${2}.directive:" | sed 's/./-/g'
289   fi
290 }
291
292 mksymlink ()
293 {
294   while test $# -ne 0
295   do
296     echo "symlink: $1 $2"
297     shift
298     shift
299   done
300 }
301
302 # upload DEST DESTDIR BASE FILE STMT FILES
303 # Arguments:
304 #  DEST     Destination site;
305 #  DESTDIR  Destination directory;
306 #  BASE     Base name for the directive file;
307 #  FILE     Name of the file to distribute (may be empty);
308 #  STMT     Additional statements for the directive file;
309 #  FILES    List of files to upload.
310 upload ()
311 {
312   dest=$1
313   destdir=$2
314   base=$3
315   file=$4
316   stmt=$5
317   files=$6
318
319   rm -f $base.directive $base.directive.asc
320   case $dest in
321     alpha.gnu.org:*)
322       mkdirective "$destdir" "$base" "$file" "$stmt"
323       echo "$passphrase" | $dbg $GPG --passphrase-fd 0 --clearsign $base.directive
324       $dbg ncftpput ftp-upload.gnu.org /incoming/alpha $files $base.directive.asc
325       ;;
326     ftp.gnu.org:*)
327       mkdirective "$destdir" "$base" "$file" "$stmt"
328       echo "$passphrase" | $dbg $GPG --passphrase-fd 0 --clearsign $base.directive
329       $dbg ncftpput ftp-upload.gnu.org /incoming/ftp $files $base.directive.asc
330       ;;
331     savannah.gnu.org:*)
332       if test -z "$files"; then
333         echo "$0: warning: standalone directives not applicable for $dest" >&2
334       fi
335       $dbg ncftpput savannah.gnu.org /incoming/savannah/$destdir $files
336       ;;
337     savannah.nongnu.org:*)
338       if test -z "$files"; then
339         echo "$0: warning: standalone directives not applicable for $dest" >&2
340       fi
341       $dbg ncftpput savannah.nongnu.org /incoming/savannah/$destdir $files
342       ;;
343     download.gnu.org.ua:alpha/*|download.gnu.org.ua:ftp/*)
344       destdir_p1=`echo "$destdir" | sed 's,^[^/]*/,,'`
345       destdir_topdir=`echo "$destdir" | sed 's,/.*,,'`
346       mkdirective "$destdir_p1" "$base" "$file" "$stmt"
347       echo "$passphrase" | $dbg $GPG --passphrase-fd 0 --clearsign $base.directive
348       for f in $files $base.directive.asc
349       do
350         echo put $f
351       done | $dbg sftp -b - puszcza.gnu.org.ua:/incoming/$destdir_topdir
352       ;;
353     /*)
354       dest_host=`echo "$dest" | sed 's,:.*,,'`
355       mkdirective "$destdir" "$base" "$file" "$stmt"
356       echo "$passphrase" | $dbg $GPG --passphrase-fd 0 --clearsign $base.directive
357       $dbg cp $files $base.directive.asc $dest_host
358       ;;
359     *)
360       if test -z "$files"; then
361         echo "$0: warning: standalone directives not applicable for $dest" >&2
362       fi
363       $dbg scp $files $dest
364       ;;
365   esac
366   rm -f $base.directive $base.directive.asc
367 }
368
369 #####
370 # Process any standalone directives
371 stmt=
372 if test -n "$symlink_files"; then
373   stmt="$stmt
374 `mksymlink $symlink_files`"
375 fi
376
377 for file in $delete_files
378 do
379   stmt="$stmt
380 archive: $file"
381 done
382
383 for file in $delete_symlinks
384 do
385   stmt="$stmt
386 rmsymlink: $file"
387 done
388
389 if test -n "$stmt"; then
390   for dest in $to
391   do
392     destdir=`echo $dest | sed 's/[^:]*://'`
393     upload "$dest" "$destdir" "`hostname`-$$" "" "$stmt"
394   done
395 fi
396
397 # Process actual uploads
398 for dest in $to
399 do
400   for file
401   do
402     echo "Uploading $file to $dest ..."
403     stmt=
404     #
405     # allowing file replacement is all or nothing.
406     if test -n "$replace"; then stmt="$stmt
407 $replace"
408     fi
409     #
410     files="$file $file.sig"
411     destdir=`echo $dest | sed 's/[^:]*://'`
412     if test -n "$symlink_expr"; then
413       linkname=`echo $file | sed "$symlink_expr"`
414       stmt="$stmt
415 symlink: $file $linkname
416 symlink: $file.sig $linkname.sig"
417     fi
418     upload "$dest" "$destdir" "$file" "$file" "$stmt" "$files"
419   done
420 done
421
422 exit 0
423
424 # Local variables:
425 # eval: (add-hook 'write-file-hooks 'time-stamp)
426 # time-stamp-start: "scriptversion="
427 # time-stamp-format: "%:y-%02m-%02d.%02H"
428 # time-stamp-time-zone: "UTC"
429 # time-stamp-end: "; # UTC"
430 # End: