install-sh revision 1.1 1 1.1 mrg #!/bin/sh
2 1.1 mrg #
3 1.1 mrg # install - install a program, script, or datafile
4 1.1 mrg #
5 1.1 mrg # This originates from X11R5 (mit/util/scripts/install.sh), which was
6 1.1 mrg # later released in X11R6 (xc/config/util/install.sh) with the
7 1.1 mrg # following copyright and license.
8 1.1 mrg #
9 1.1 mrg # Copyright (C) 1994 X Consortium
10 1.1 mrg #
11 1.1 mrg # Permission is hereby granted, free of charge, to any person obtaining a copy
12 1.1 mrg # of this software and associated documentation files (the "Software"), to
13 1.1 mrg # deal in the Software without restriction, including without limitation the
14 1.1 mrg # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
15 1.1 mrg # sell copies of the Software, and to permit persons to whom the Software is
16 1.1 mrg # furnished to do so, subject to the following conditions:
17 1.1 mrg #
18 1.1 mrg # The above copyright notice and this permission notice shall be included in
19 1.1 mrg # all copies or substantial portions of the Software.
20 1.1 mrg #
21 1.1 mrg # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 1.1 mrg # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 1.1 mrg # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 1.1 mrg # X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
25 1.1 mrg # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
26 1.1 mrg # TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 1.1 mrg #
28 1.1 mrg # Except as contained in this notice, the name of the X Consortium shall not
29 1.1 mrg # be used in advertising or otherwise to promote the sale, use or other deal-
30 1.1 mrg # ings in this Software without prior written authorization from the X Consor-
31 1.1 mrg # tium.
32 1.1 mrg #
33 1.1 mrg #
34 1.1 mrg # FSF changes to this file are in the public domain.
35 1.1 mrg #
36 1.1 mrg # Calling this script install-sh is preferred over install.sh, to prevent
37 1.1 mrg # `make' implicit rules from creating a file called install from it
38 1.1 mrg # when there is no Makefile.
39 1.1 mrg #
40 1.1 mrg # This script is compatible with the BSD install script, but was written
41 1.1 mrg # from scratch. It can only install one file at a time, a restriction
42 1.1 mrg # shared with many OS's install programs.
43 1.1 mrg
44 1.1 mrg
45 1.1 mrg # set DOITPROG to echo to test this script
46 1.1 mrg
47 1.1 mrg # Don't use :- since 4.3BSD and earlier shells don't like it.
48 1.1 mrg doit="${DOITPROG-}"
49 1.1 mrg
50 1.1 mrg
51 1.1 mrg # put in absolute paths if you don't have them in your path; or use env. vars.
52 1.1 mrg
53 1.1 mrg mvprog="${MVPROG-mv}"
54 1.1 mrg cpprog="${CPPROG-cp}"
55 1.1 mrg chmodprog="${CHMODPROG-chmod}"
56 1.1 mrg chownprog="${CHOWNPROG-chown}"
57 1.1 mrg chgrpprog="${CHGRPPROG-chgrp}"
58 1.1 mrg stripprog="${STRIPPROG-strip}"
59 1.1 mrg rmprog="${RMPROG-rm}"
60 1.1 mrg mkdirprog="${MKDIRPROG-mkdir}"
61 1.1 mrg
62 1.1 mrg transformbasename=""
63 1.1 mrg transform_arg=""
64 1.1 mrg instcmd="$mvprog"
65 1.1 mrg chmodcmd="$chmodprog 0755"
66 1.1 mrg chowncmd=""
67 1.1 mrg chgrpcmd=""
68 1.1 mrg stripcmd=""
69 1.1 mrg rmcmd="$rmprog -f"
70 1.1 mrg mvcmd="$mvprog"
71 1.1 mrg src=""
72 1.1 mrg dst=""
73 1.1 mrg dir_arg=""
74 1.1 mrg
75 1.1 mrg while [ x"$1" != x ]; do
76 1.1 mrg case $1 in
77 1.1 mrg -c) instcmd=$cpprog
78 1.1 mrg shift
79 1.1 mrg continue;;
80 1.1 mrg
81 1.1 mrg -d) dir_arg=true
82 1.1 mrg shift
83 1.1 mrg continue;;
84 1.1 mrg
85 1.1 mrg -m) chmodcmd="$chmodprog $2"
86 1.1 mrg shift
87 1.1 mrg shift
88 1.1 mrg continue;;
89 1.1 mrg
90 1.1 mrg -o) chowncmd="$chownprog $2"
91 1.1 mrg shift
92 1.1 mrg shift
93 1.1 mrg continue;;
94 1.1 mrg
95 1.1 mrg -g) chgrpcmd="$chgrpprog $2"
96 1.1 mrg shift
97 1.1 mrg shift
98 1.1 mrg continue;;
99 1.1 mrg
100 1.1 mrg -s) stripcmd=$stripprog
101 1.1 mrg shift
102 1.1 mrg continue;;
103 1.1 mrg
104 1.1 mrg -t=*) transformarg=`echo $1 | sed 's/-t=//'`
105 1.1 mrg shift
106 1.1 mrg continue;;
107 1.1 mrg
108 1.1 mrg -b=*) transformbasename=`echo $1 | sed 's/-b=//'`
109 1.1 mrg shift
110 1.1 mrg continue;;
111 1.1 mrg
112 1.1 mrg *) if [ x"$src" = x ]
113 1.1 mrg then
114 1.1 mrg src=$1
115 1.1 mrg else
116 1.1 mrg # this colon is to work around a 386BSD /bin/sh bug
117 1.1 mrg :
118 1.1 mrg dst=$1
119 1.1 mrg fi
120 1.1 mrg shift
121 1.1 mrg continue;;
122 1.1 mrg esac
123 1.1 mrg done
124 1.1 mrg
125 1.1 mrg if [ x"$src" = x ]
126 1.1 mrg then
127 1.1 mrg echo "$0: no input file specified" >&2
128 1.1 mrg exit 1
129 1.1 mrg else
130 1.1 mrg :
131 1.1 mrg fi
132 1.1 mrg
133 1.1 mrg if [ x"$dir_arg" != x ]; then
134 1.1 mrg dst=$src
135 1.1 mrg src=""
136 1.1 mrg
137 1.1 mrg if [ -d "$dst" ]; then
138 1.1 mrg instcmd=:
139 1.1 mrg chmodcmd=""
140 1.1 mrg else
141 1.1 mrg instcmd=$mkdirprog
142 1.1 mrg fi
143 1.1 mrg else
144 1.1 mrg
145 1.1 mrg # Waiting for this to be detected by the "$instcmd $src $dsttmp" command
146 1.1 mrg # might cause directories to be created, which would be especially bad
147 1.1 mrg # if $src (and thus $dsttmp) contains '*'.
148 1.1 mrg
149 1.1 mrg if [ -f "$src" ] || [ -d "$src" ]
150 1.1 mrg then
151 1.1 mrg :
152 1.1 mrg else
153 1.1 mrg echo "$0: $src does not exist" >&2
154 1.1 mrg exit 1
155 1.1 mrg fi
156 1.1 mrg
157 1.1 mrg if [ x"$dst" = x ]
158 1.1 mrg then
159 1.1 mrg echo "$0: no destination specified" >&2
160 1.1 mrg exit 1
161 1.1 mrg else
162 1.1 mrg :
163 1.1 mrg fi
164 1.1 mrg
165 1.1 mrg # If destination is a directory, append the input filename; if your system
166 1.1 mrg # does not like double slashes in filenames, you may need to add some logic
167 1.1 mrg
168 1.1 mrg if [ -d "$dst" ]
169 1.1 mrg then
170 1.1 mrg dst=$dst/`basename "$src"`
171 1.1 mrg else
172 1.1 mrg :
173 1.1 mrg fi
174 1.1 mrg fi
175 1.1 mrg
176 1.1 mrg ## this sed command emulates the dirname command
177 1.1 mrg dstdir=`echo "$dst" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
178 1.1 mrg
179 1.1 mrg # Make sure that the destination directory exists.
180 1.1 mrg # this part is taken from Noah Friedman's mkinstalldirs script
181 1.1 mrg
182 1.1 mrg # Skip lots of stat calls in the usual case.
183 1.1 mrg if [ ! -d "$dstdir" ]; then
184 1.1 mrg defaultIFS='
185 1.1 mrg '
186 1.1 mrg IFS="${IFS-$defaultIFS}"
187 1.1 mrg
188 1.1 mrg oIFS=$IFS
189 1.1 mrg # Some sh's can't handle IFS=/ for some reason.
190 1.1 mrg IFS='%'
191 1.1 mrg set - `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
192 1.1 mrg IFS=$oIFS
193 1.1 mrg
194 1.1 mrg pathcomp=''
195 1.1 mrg
196 1.1 mrg while [ $# -ne 0 ] ; do
197 1.1 mrg pathcomp=$pathcomp$1
198 1.1 mrg shift
199 1.1 mrg
200 1.1 mrg if [ ! -d "$pathcomp" ] ;
201 1.1 mrg then
202 1.1 mrg $mkdirprog "$pathcomp"
203 1.1 mrg else
204 1.1 mrg :
205 1.1 mrg fi
206 1.1 mrg
207 1.1 mrg pathcomp=$pathcomp/
208 1.1 mrg done
209 1.1 mrg fi
210 1.1 mrg
211 1.1 mrg if [ x"$dir_arg" != x ]
212 1.1 mrg then
213 1.1 mrg $doit $instcmd "$dst" &&
214 1.1 mrg
215 1.1 mrg if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dst"; else : ; fi &&
216 1.1 mrg if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dst"; else : ; fi &&
217 1.1 mrg if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dst"; else : ; fi &&
218 1.1 mrg if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dst"; else : ; fi
219 1.1 mrg else
220 1.1 mrg
221 1.1 mrg # If we're going to rename the final executable, determine the name now.
222 1.1 mrg
223 1.1 mrg if [ x"$transformarg" = x ]
224 1.1 mrg then
225 1.1 mrg dstfile=`basename "$dst"`
226 1.1 mrg else
227 1.1 mrg dstfile=`basename "$dst" $transformbasename |
228 1.1 mrg sed $transformarg`$transformbasename
229 1.1 mrg fi
230 1.1 mrg
231 1.1 mrg # don't allow the sed command to completely eliminate the filename
232 1.1 mrg
233 1.1 mrg if [ x"$dstfile" = x ]
234 1.1 mrg then
235 1.1 mrg dstfile=`basename "$dst"`
236 1.1 mrg else
237 1.1 mrg :
238 1.1 mrg fi
239 1.1 mrg
240 1.1 mrg # Make a couple of temp file names in the proper directory.
241 1.1 mrg
242 1.1 mrg dsttmp=$dstdir/_inst.$$_
243 1.1 mrg rmtmp=$dstdir/_rm.$$_
244 1.1 mrg
245 1.1 mrg # Trap to clean up temp files at exit.
246 1.1 mrg
247 1.1 mrg trap 'status=$?; rm -f "$dsttmp" "$rmtmp" && exit $status' 0
248 1.1 mrg trap '(exit $?); exit' 1 2 13 15
249 1.1 mrg
250 1.1 mrg # Move or copy the file name to the temp name
251 1.1 mrg
252 1.1 mrg $doit $instcmd "$src" "$dsttmp" &&
253 1.1 mrg
254 1.1 mrg # and set any options; do chmod last to preserve setuid bits
255 1.1 mrg
256 1.1 mrg # If any of these fail, we abort the whole thing. If we want to
257 1.1 mrg # ignore errors from any of these, just make sure not to ignore
258 1.1 mrg # errors from the above "$doit $instcmd $src $dsttmp" command.
259 1.1 mrg
260 1.1 mrg if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp"; else :;fi &&
261 1.1 mrg if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp"; else :;fi &&
262 1.1 mrg if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dsttmp"; else :;fi &&
263 1.1 mrg if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; else :;fi &&
264 1.1 mrg
265 1.1 mrg # Now remove or move aside any old file at destination location. We try this
266 1.1 mrg # two ways since rm can't unlink itself on some systems and the destination
267 1.1 mrg # file might be busy for other reasons. In this case, the final cleanup
268 1.1 mrg # might fail but the new file should still install successfully.
269 1.1 mrg
270 1.1 mrg {
271 1.1 mrg if [ -f "$dstdir/$dstfile" ]
272 1.1 mrg then
273 1.1 mrg $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null ||
274 1.1 mrg $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null ||
275 1.1 mrg {
276 1.1 mrg echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
277 1.1 mrg (exit 1); exit
278 1.1 mrg }
279 1.1 mrg else
280 1.1 mrg :
281 1.1 mrg fi
282 1.1 mrg } &&
283 1.1 mrg
284 1.1 mrg # Now rename the file to the real destination.
285 1.1 mrg
286 1.1 mrg $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
287 1.1 mrg
288 1.1 mrg fi &&
289 1.1 mrg
290 1.1 mrg # The final little trick to "correctly" pass the exit status to the exit trap.
291 1.1 mrg
292 1.1 mrg {
293 1.1 mrg (exit 0); exit
294 1.1 mrg }
295