chiark / gitweb /
Better message for 503 from peer on idle conn. Conventional macro protection for...
[inn-innduct.git] / support / install-sh
1 #!/bin/sh
2 #
3 # install - install a program, script, or datafile
4 # This comes from X11R5 (mit/util/scripts/install.sh).
5 #
6 # Copyright 1991 by the Massachusetts Institute of Technology
7 #
8 # Permission to use, copy, modify, distribute, and sell this software and its
9 # documentation for any purpose is hereby granted without fee, provided that
10 # the above copyright notice appear in all copies and that both that
11 # copyright notice and this permission notice appear in supporting
12 # documentation, and that the name of M.I.T. not be used in advertising or
13 # publicity pertaining to distribution of the software without specific,
14 # written prior permission.  M.I.T. makes no representations about the
15 # suitability of this software for any purpose.  It is provided "as is"
16 # without express or implied warranty.
17 #
18 # Calling this script install-sh is preferred over install.sh, to prevent
19 # `make' implicit rules from creating a file called install from it
20 # when there is no Makefile.
21 #
22 # This script is compatible with the BSD install script, but was written
23 # from scratch.  It can only install one file at a time, a restriction
24 # shared with many OS's install programs.
25
26 # Modified for INN by adding the -B option to request saving of the original
27 # file (if install is overwriting an existing file).  -B takes an argument,
28 # the suffix to use.  INN invokes this script as install-sh -B .OLD.  Also
29 # modified to use cp -p instead of just cp to install programs when invoked
30 # as install -c.
31
32
33 # set DOITPROG to echo to test this script
34
35 # Don't use :- since 4.3BSD and earlier shells don't like it.
36 doit="${DOITPROG-}"
37
38
39 # put in absolute paths if you don't have them in your path; or use env. vars.
40
41 mvprog="${MVPROG-mv}"
42 cpprog="${CPPROG-cp}"
43 chmodprog="${CHMODPROG-chmod}"
44 chownprog="${CHOWNPROG-chown}"
45 chgrpprog="${CHGRPPROG-chgrp}"
46 stripprog="${STRIPPROG-strip}"
47 rmprog="${RMPROG-rm}"
48 mkdirprog="${MKDIRPROG-mkdir}"
49
50 backupsuffix=""
51 transformbasename=""
52 transform_arg=""
53 instcmd="$mvprog"
54 chmodcmd="$chmodprog 0755"
55 chowncmd=""
56 chgrpcmd=""
57 stripcmd=""
58 rmcmd="$rmprog -f"
59 mvcmd="$mvprog"
60 src=""
61 dst=""
62 dir_arg=""
63
64 while [ x"$1" != x ]; do
65     case $1 in
66         -B) backupsuffix="$2"
67             shift
68             shift
69             continue;;
70
71         -c) instcmd="$cpprog -p"
72             shift
73             continue;;
74
75         -d) dir_arg=true
76             shift
77             continue;;
78
79         -m) chmodcmd="$chmodprog $2"
80             shift
81             shift
82             continue;;
83
84         -o) chowncmd="$chownprog $2"
85             shift
86             shift
87             continue;;
88
89         -g) chgrpcmd="$chgrpprog $2"
90             shift
91             shift
92             continue;;
93
94         -s) stripcmd="$stripprog"
95             shift
96             continue;;
97
98         -t=*) transformarg=`echo $1 | sed 's/-t=//'`
99             shift
100             continue;;
101
102         -b=*) transformbasename=`echo $1 | sed 's/-b=//'`
103             shift
104             continue;;
105
106         *)  if [ x"$src" = x ]
107             then
108                 src=$1
109             else
110                 # this colon is to work around a 386BSD /bin/sh bug
111                 :
112                 dst=$1
113             fi
114             shift
115             continue;;
116     esac
117 done
118
119 if [ x"$src" = x ]
120 then
121         echo "install:  no input file specified"
122         exit 1
123 else
124         true
125 fi
126
127 # For Cygwin compatibility.
128 if [ -x "$src".exe ]; then
129         src=${src}.exe
130 fi
131
132 if [ x"$dir_arg" != x ]; then
133         dst=$src
134         src=""
135         
136         if [ -d $dst ]; then
137                 instcmd=:
138                 chmodcmd=""
139                 chowncmd=""
140         else
141                 instcmd=mkdir
142         fi
143 else
144
145 # Waiting for this to be detected by the "$instcmd $src $dsttmp" command
146 # might cause directories to be created, which would be especially bad 
147 # if $src (and thus $dsttmp) contains '*'.
148
149         if [ -f $src -o -d $src ]
150         then
151                 true
152         else
153                 echo "install:  $src does not exist"
154                 exit 1
155         fi
156         
157         if [ x"$dst" = x ]
158         then
159                 echo "install:  no destination specified"
160                 exit 1
161         else
162                 true
163         fi
164
165 # If destination is a directory, append the input filename; if your system
166 # does not like double slashes in filenames, you may need to add some logic
167
168         if [ -d $dst ]
169         then
170                 dst="$dst"/`basename $src`
171         else
172                 true
173         fi
174 fi
175
176 ## this sed command emulates the dirname command
177 dstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
178
179 # Make sure that the destination directory exists.
180 #  this part is taken from Noah Friedman's mkinstalldirs script
181
182 # Skip lots of stat calls in the usual case.
183 if [ ! -d "$dstdir" ]; then
184 defaultIFS='    
185 '
186 IFS="${IFS-${defaultIFS}}"
187
188 oIFS="${IFS}"
189 # Some sh's can't handle IFS=/ for some reason.
190 IFS='%'
191 set - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
192 IFS="${oIFS}"
193
194 pathcomp=''
195
196 while [ $# -ne 0 ] ; do
197         pathcomp="${pathcomp}${1}"
198         shift
199
200         if [ ! -d "${pathcomp}" ] ;
201         then
202                 $mkdirprog "${pathcomp}"
203         else
204                 true
205         fi
206
207         pathcomp="${pathcomp}/"
208 done
209 fi
210
211 if [ x"$dir_arg" != x ]
212 then
213         $doit $instcmd $dst &&
214
215         if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
216         if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
217         if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
218         if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
219 else
220
221 # If we're going to rename the final executable, determine the name now.
222
223         if [ x"$transformarg" = x ] 
224         then
225                 dstfile=`basename $dst`
226         else
227                 dstfile=`basename $dst $transformbasename | 
228                         sed $transformarg`$transformbasename
229         fi
230
231 # don't allow the sed command to completely eliminate the filename
232
233         if [ x"$dstfile" = x ] 
234         then
235                 dstfile=`basename $dst`
236         else
237                 true
238         fi
239
240 # Make a temp file name in the proper directory.
241
242         dsttmp=$dstdir/#inst.$$#
243
244 # Move or copy the file name to the temp name
245
246         $doit $instcmd $src $dsttmp &&
247
248         trap "rm -f ${dsttmp}" 0 &&
249
250 # and set any options; do chmod last to preserve setuid bits
251
252 # If any of these fail, we abort the whole thing.  If we want to
253 # ignore errors from any of these, just make sure not to ignore
254 # errors from the above "$doit $instcmd $src $dsttmp" command.
255
256         if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
257         if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
258         if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
259         if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
260
261 # Now rename the file to the real destination.  If $backupsuffix
262 # is set, rename the existing file if it exists; otherwise, just
263 # remove it.
264
265         if [ x"$backupsuffix" != x ] && [ -f "$dstdir/$dstfile" ]; then
266                 $doit $mvcmd $dstdir/$dstfile $dstdir/$dstfile$backupsuffix
267         else
268                 $doit $rmcmd -f $dstdir/$dstfile
269         fi &&
270
271         $doit $mvcmd $dsttmp $dstdir/$dstfile 
272
273 fi &&
274
275
276 exit 0