Statistics
| Revision:

root / install-sh

History | View | Annotate | Download (7 kB)

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