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