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