autoupdate
[gnulib.git] / build-aux / gnupload
1 #!/bin/sh
2 # Sign files and upload them.
3
4 scriptversion=2009-01-30.00
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 to=
27 delete=false
28
29 usage="Usage: $0 [OPTIONS]... FILES...
30
31 Sign all FILES, and upload them to (or delete them from) selected
32 destinations, according to
33 <http://www.gnu.org/prep/maintain/html_node/Automated-FTP-Uploads.html>.
34
35 Options:
36   --help                   print this help text and exit
37   --to DEST                specify one destination for FILES
38                            (multiple --to options are allowed)
39   --user NAME              sign with key NAME
40   --delete                 delete FILES from destination instead of uploading
41   --version                output version information and exit
42
43 Recognized destinations are:
44   alpha.gnu.org:DIRECTORY
45   savannah.gnu.org:DIRECTORY
46   savannah.nongnu.org:DIRECTORY
47   ftp.gnu.org:DIRECTORY
48                            build directive files and upload files by FTP
49   [user@]host:DIRECTORY    upload files with scp
50
51 Deletion only works for ftp.gnu.org and alpha.gnu.org (using the
52 archive: directive).  Otherwise it is a no-op.  Deleting a file foo also
53 deletes foo.sig; do not specify the .sig explicitly.
54
55 Simple single-target single-file examples:
56   gnupload --to alpha.gnu.org:automake automake-1.8.2b.tar.gz
57   gnupload --to ftp.gnu.org:automake automake-1.8.3.tar.gz
58   gnupload --to alpha.gnu.org:automake --delete automake-oops.tar.gz
59
60 Multiple-target multiple-file example:
61   gnupload --to sources.redhat.com:~ftp/pub/automake \\
62            --to alpha.gnu.org:automake \\
63            automake-1.8.2b.tar.gz automake-1.8.2b.tar.bz2
64
65 You can get the latest version of this script from savannah:
66 <http://git.savannah.gnu.org/cgit/automake.git/plain/lib/gnupload>
67
68 Report bugs to <bug-automake@gnu.org>.
69 Send patches to <automake-patches@gnu.org>."
70
71 while test -n "$1"; do
72   case $1 in
73     --delete)
74       delete=true
75       shift
76       ;;
77     --help)
78       echo "$usage"
79       exit $?
80       ;;
81     --to)
82       if test -z "$2"; then
83         echo "$0: Missing argument for --to" 1>&2
84         exit 1
85       else
86         to="$to $2"
87         shift 2
88       fi
89       ;;
90     --user)
91       if test -z "$2"; then
92         echo "$0: Missing argument for --user" 1>&2
93         exit 1
94       else
95         GPG="$GPG --local-user $2"
96         shift 2
97       fi
98       ;;
99     --version)
100       echo "gnupload $scriptversion"
101       exit $?
102       ;;
103     -*)
104       echo "$0: Unknown option \`$1', try \`$0 --help'" 1>&2
105       exit 1
106       ;;
107     *)
108       break
109       ;;
110   esac
111 done
112
113 if test $# = 0; then
114   echo "$0: No file to upload or delete" 1>&2
115   exit 1
116 else
117   :
118 fi
119
120 if $delete; then :; else
121   # Make sure all files exist.  We don't want to ask
122   # for the passphrase if the script will fail.
123   for file
124   do
125     if test ! -f $file; then
126       echo "$0: Cannot find \`$file'" 1>&2
127       exit 1
128     else
129       :
130     fi
131   done
132 fi
133
134 # Make sure passphrase is not exported in the environment.
135 unset passphrase
136
137 # Reset PATH to be sure that echo is a built-in.  We will later use
138 # `echo $passphrase' to output the passphrase, so it is important that
139 # it is a built-in (third-party programs tend to appear in `ps'
140 # listings with their arguments...).
141 # Remember this script runs with `set -e', so if echo is not built-in
142 # it will exit now.
143 PATH=/empty echo -n "Enter GPG passphrase: "
144 stty -echo
145 read -r passphrase
146 stty echo
147 echo
148
149 # Nothing to sign if deleting.
150 if $delete; then :; else
151   for file
152   do
153     echo "Signing $file..."
154     rm -f $file.sig
155     echo $passphrase | $GPG --passphrase-fd 0 -ba -o $file.sig $file
156   done
157 fi
158
159 for dest in $to
160 do
161   for file
162   do
163     # Prepare arguments.
164     if $delete; then
165       echo "Removing $file from $dest..."
166       files=  # nothing to upload if deleting
167       directive="archive: $file"
168     else
169       echo "Uploading $file to $dest..."
170       files="$file $file.sig"
171       directive="filename: "`basename -- "$file"`
172     fi
173     destdir=`echo $dest | sed 's/[^:]*://'`
174
175     case $dest in
176       alpha.gnu.org:*)
177         rm -f $file.directive $file.directive.asc
178         cat >$file.directive<<EOF
179 version: 1.1
180 directory: $destdir
181 $directive
182 EOF
183         echo "$passphrase" | $GPG --passphrase-fd 0 --clearsign $file.directive
184         ncftpput ftp-upload.gnu.org /incoming/alpha $files $file.directive.asc
185         rm -f $file.directive $file.directive.asc
186         ;;
187       ftp.gnu.org:*)
188         rm -f $file.directive $file.directive.asc
189         cat >$file.directive<<EOF
190 version: 1.1
191 directory: $destdir
192 $directive
193 EOF
194         echo "$passphrase" | $GPG --passphrase-fd 0 --clearsign $file.directive
195         ncftpput ftp-upload.gnu.org /incoming/ftp $files $file.directive.asc
196         rm -f $file.directive $file.directive.asc
197         ;;
198       savannah.gnu.org:*)
199         # We only know how to implement delete for {ftp,alpha}.gnu.org.
200         $delete \
201         || ncftpput savannah.gnu.org /incoming/savannah/$destdir $files
202         ;;
203       savannah.nongnu.org:*)
204         $delete \
205         || ncftpput savannah.nongnu.org /incoming/savannah/$destdir $files
206         ;;
207       *)
208         $delete \
209         || scp $files $dest
210         ;;
211     esac
212   done
213 done
214
215 # Local variables:
216 # eval: (add-hook 'write-file-hooks 'time-stamp)
217 # time-stamp-start: "scriptversion="
218 # time-stamp-format: "%:y-%02m-%02d.%02H"
219 # time-stamp-end: "$"
220 # End: