install-sh revision 010cdda0
1010cdda0Smrg#!/bin/sh
2010cdda0Smrg#
3010cdda0Smrg# install - install a program, script, or datafile
4010cdda0Smrg#
5010cdda0Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
6010cdda0Smrg# later released in X11R6 (xc/config/util/install.sh) with the
7010cdda0Smrg# following copyright and license.
8010cdda0Smrg#
9010cdda0Smrg# Copyright (C) 1994 X Consortium
10010cdda0Smrg#
11010cdda0Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
12010cdda0Smrg# of this software and associated documentation files (the "Software"), to
13010cdda0Smrg# deal in the Software without restriction, including without limitation the
14010cdda0Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
15010cdda0Smrg# sell copies of the Software, and to permit persons to whom the Software is
16010cdda0Smrg# furnished to do so, subject to the following conditions:
17010cdda0Smrg#
18010cdda0Smrg# The above copyright notice and this permission notice shall be included in
19010cdda0Smrg# all copies or substantial portions of the Software.
20010cdda0Smrg#
21010cdda0Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22010cdda0Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23010cdda0Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
24010cdda0Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
25010cdda0Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
26010cdda0Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27010cdda0Smrg#
28010cdda0Smrg# Except as contained in this notice, the name of the X Consortium shall not
29010cdda0Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
30010cdda0Smrg# ings in this Software without prior written authorization from the X Consor-
31010cdda0Smrg# tium.
32010cdda0Smrg#
33010cdda0Smrg#
34010cdda0Smrg# FSF changes to this file are in the public domain.
35010cdda0Smrg#
36010cdda0Smrg# Calling this script install-sh is preferred over install.sh, to prevent
37010cdda0Smrg# `make' implicit rules from creating a file called install from it
38010cdda0Smrg# when there is no Makefile.
39010cdda0Smrg#
40010cdda0Smrg# This script is compatible with the BSD install script, but was written
41010cdda0Smrg# from scratch.  It can only install one file at a time, a restriction
42010cdda0Smrg# shared with many OS's install programs.
43010cdda0Smrg
44010cdda0Smrg
45010cdda0Smrg# set DOITPROG to echo to test this script
46010cdda0Smrg
47010cdda0Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
48010cdda0Smrgdoit="${DOITPROG-}"
49010cdda0Smrg
50010cdda0Smrg
51010cdda0Smrg# put in absolute paths if you don't have them in your path; or use env. vars.
52010cdda0Smrg
53010cdda0Smrgmvprog="${MVPROG-mv}"
54010cdda0Smrgcpprog="${CPPROG-cp}"
55010cdda0Smrgchmodprog="${CHMODPROG-chmod}"
56010cdda0Smrgchownprog="${CHOWNPROG-chown}"
57010cdda0Smrgchgrpprog="${CHGRPPROG-chgrp}"
58010cdda0Smrgstripprog="${STRIPPROG-strip}"
59010cdda0Smrgrmprog="${RMPROG-rm}"
60010cdda0Smrgmkdirprog="${MKDIRPROG-mkdir}"
61010cdda0Smrg
62010cdda0Smrgtransformbasename=""
63010cdda0Smrgtransform_arg=""
64010cdda0Smrginstcmd="$mvprog"
65010cdda0Smrgchmodcmd="$chmodprog 0755"
66010cdda0Smrgchowncmd=""
67010cdda0Smrgchgrpcmd=""
68010cdda0Smrgstripcmd=""
69010cdda0Smrgrmcmd="$rmprog -f"
70010cdda0Smrgmvcmd="$mvprog"
71010cdda0Smrgsrc=""
72010cdda0Smrgdst=""
73010cdda0Smrgdir_arg=""
74010cdda0Smrg
75010cdda0Smrgwhile [ x"$1" != x ]; do
76010cdda0Smrg    case $1 in
77010cdda0Smrg	-c) instcmd=$cpprog
78010cdda0Smrg	    shift
79010cdda0Smrg	    continue;;
80010cdda0Smrg
81010cdda0Smrg	-d) dir_arg=true
82010cdda0Smrg	    shift
83010cdda0Smrg	    continue;;
84010cdda0Smrg
85010cdda0Smrg	-m) chmodcmd="$chmodprog $2"
86010cdda0Smrg	    shift
87010cdda0Smrg	    shift
88010cdda0Smrg	    continue;;
89010cdda0Smrg
90010cdda0Smrg	-o) chowncmd="$chownprog $2"
91010cdda0Smrg	    shift
92010cdda0Smrg	    shift
93010cdda0Smrg	    continue;;
94010cdda0Smrg
95010cdda0Smrg	-g) chgrpcmd="$chgrpprog $2"
96010cdda0Smrg	    shift
97010cdda0Smrg	    shift
98010cdda0Smrg	    continue;;
99010cdda0Smrg
100010cdda0Smrg	-s) stripcmd=$stripprog
101010cdda0Smrg	    shift
102010cdda0Smrg	    continue;;
103010cdda0Smrg
104010cdda0Smrg	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
105010cdda0Smrg	    shift
106010cdda0Smrg	    continue;;
107010cdda0Smrg
108010cdda0Smrg	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
109010cdda0Smrg	    shift
110010cdda0Smrg	    continue;;
111010cdda0Smrg
112010cdda0Smrg	*)  if [ x"$src" = x ]
113010cdda0Smrg	    then
114010cdda0Smrg		src=$1
115010cdda0Smrg	    else
116010cdda0Smrg		# this colon is to work around a 386BSD /bin/sh bug
117010cdda0Smrg		:
118010cdda0Smrg		dst=$1
119010cdda0Smrg	    fi
120010cdda0Smrg	    shift
121010cdda0Smrg	    continue;;
122010cdda0Smrg    esac
123010cdda0Smrgdone
124010cdda0Smrg
125010cdda0Smrgif [ x"$src" = x ]
126010cdda0Smrgthen
127010cdda0Smrg	echo "$0: no input file specified" >&2
128010cdda0Smrg	exit 1
129010cdda0Smrgelse
130010cdda0Smrg	:
131010cdda0Smrgfi
132010cdda0Smrg
133010cdda0Smrgif [ x"$dir_arg" != x ]; then
134010cdda0Smrg	dst=$src
135010cdda0Smrg	src=""
136010cdda0Smrg
137010cdda0Smrg	if [ -d "$dst" ]; then
138010cdda0Smrg		instcmd=:
139010cdda0Smrg		chmodcmd=""
140010cdda0Smrg	else
141010cdda0Smrg		instcmd=$mkdirprog
142010cdda0Smrg	fi
143010cdda0Smrgelse
144010cdda0Smrg
145010cdda0Smrg# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
146010cdda0Smrg# might cause directories to be created, which would be especially bad
147010cdda0Smrg# if $src (and thus $dsttmp) contains '*'.
148010cdda0Smrg
149010cdda0Smrg	if [ -f "$src" ] || [ -d "$src" ]
150010cdda0Smrg	then
151010cdda0Smrg		:
152010cdda0Smrg	else
153010cdda0Smrg		echo "$0: $src does not exist" >&2
154010cdda0Smrg		exit 1
155010cdda0Smrg	fi
156010cdda0Smrg
157010cdda0Smrg	if [ x"$dst" = x ]
158010cdda0Smrg	then
159010cdda0Smrg		echo "$0: no destination specified" >&2
160010cdda0Smrg		exit 1
161010cdda0Smrg	else
162010cdda0Smrg		:
163010cdda0Smrg	fi
164010cdda0Smrg
165010cdda0Smrg# If destination is a directory, append the input filename; if your system
166010cdda0Smrg# does not like double slashes in filenames, you may need to add some logic
167010cdda0Smrg
168010cdda0Smrg	if [ -d "$dst" ]
169010cdda0Smrg	then
170010cdda0Smrg		dst=$dst/`basename "$src"`
171010cdda0Smrg	else
172010cdda0Smrg		:
173010cdda0Smrg	fi
174010cdda0Smrgfi
175010cdda0Smrg
176010cdda0Smrg## this sed command emulates the dirname command
177010cdda0Smrgdstdir=`echo "$dst" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
178010cdda0Smrg
179010cdda0Smrg# Make sure that the destination directory exists.
180010cdda0Smrg#  this part is taken from Noah Friedman's mkinstalldirs script
181010cdda0Smrg
182010cdda0Smrg# Skip lots of stat calls in the usual case.
183010cdda0Smrgif [ ! -d "$dstdir" ]; then
184010cdda0SmrgdefaultIFS='
185010cdda0Smrg	'
186010cdda0SmrgIFS="${IFS-$defaultIFS}"
187010cdda0Smrg
188010cdda0SmrgoIFS=$IFS
189010cdda0Smrg# Some sh's can't handle IFS=/ for some reason.
190010cdda0SmrgIFS='%'
191010cdda0Smrgset - `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
192010cdda0SmrgIFS=$oIFS
193010cdda0Smrg
194010cdda0Smrgpathcomp=''
195010cdda0Smrg
196010cdda0Smrgwhile [ $# -ne 0 ] ; do
197010cdda0Smrg	pathcomp=$pathcomp$1
198010cdda0Smrg	shift
199010cdda0Smrg
200010cdda0Smrg	if [ ! -d "$pathcomp" ] ;
201010cdda0Smrg        then
202010cdda0Smrg		$mkdirprog "$pathcomp"
203010cdda0Smrg	else
204010cdda0Smrg		:
205010cdda0Smrg	fi
206010cdda0Smrg
207010cdda0Smrg	pathcomp=$pathcomp/
208010cdda0Smrgdone
209010cdda0Smrgfi
210010cdda0Smrg
211010cdda0Smrgif [ x"$dir_arg" != x ]
212010cdda0Smrgthen
213010cdda0Smrg	$doit $instcmd "$dst" &&
214010cdda0Smrg
215010cdda0Smrg	if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dst"; else : ; fi &&
216010cdda0Smrg	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dst"; else : ; fi &&
217010cdda0Smrg	if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dst"; else : ; fi &&
218010cdda0Smrg	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dst"; else : ; fi
219010cdda0Smrgelse
220010cdda0Smrg
221010cdda0Smrg# If we're going to rename the final executable, determine the name now.
222010cdda0Smrg
223010cdda0Smrg	if [ x"$transformarg" = x ]
224010cdda0Smrg	then
225010cdda0Smrg		dstfile=`basename "$dst"`
226010cdda0Smrg	else
227010cdda0Smrg		dstfile=`basename "$dst" $transformbasename |
228010cdda0Smrg			sed $transformarg`$transformbasename
229010cdda0Smrg	fi
230010cdda0Smrg
231010cdda0Smrg# don't allow the sed command to completely eliminate the filename
232010cdda0Smrg
233010cdda0Smrg	if [ x"$dstfile" = x ]
234010cdda0Smrg	then
235010cdda0Smrg		dstfile=`basename "$dst"`
236010cdda0Smrg	else
237010cdda0Smrg		:
238010cdda0Smrg	fi
239010cdda0Smrg
240010cdda0Smrg# Make a couple of temp file names in the proper directory.
241010cdda0Smrg
242010cdda0Smrg	dsttmp=$dstdir/_inst.$$_
243010cdda0Smrg	rmtmp=$dstdir/_rm.$$_
244010cdda0Smrg
245010cdda0Smrg# Trap to clean up temp files at exit.
246010cdda0Smrg
247010cdda0Smrg	trap 'status=$?; rm -f "$dsttmp" "$rmtmp" && exit $status' 0
248010cdda0Smrg	trap '(exit $?); exit' 1 2 13 15
249010cdda0Smrg
250010cdda0Smrg# Move or copy the file name to the temp name
251010cdda0Smrg
252010cdda0Smrg	$doit $instcmd "$src" "$dsttmp" &&
253010cdda0Smrg
254010cdda0Smrg# and set any options; do chmod last to preserve setuid bits
255010cdda0Smrg
256010cdda0Smrg# If any of these fail, we abort the whole thing.  If we want to
257010cdda0Smrg# ignore errors from any of these, just make sure not to ignore
258010cdda0Smrg# errors from the above "$doit $instcmd $src $dsttmp" command.
259010cdda0Smrg
260010cdda0Smrg	if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp"; else :;fi &&
261010cdda0Smrg	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp"; else :;fi &&
262010cdda0Smrg	if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dsttmp"; else :;fi &&
263010cdda0Smrg	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; else :;fi &&
264010cdda0Smrg
265010cdda0Smrg# Now remove or move aside any old file at destination location.  We try this
266010cdda0Smrg# two ways since rm can't unlink itself on some systems and the destination
267010cdda0Smrg# file might be busy for other reasons.  In this case, the final cleanup
268010cdda0Smrg# might fail but the new file should still install successfully.
269010cdda0Smrg
270010cdda0Smrg{
271010cdda0Smrg	if [ -f "$dstdir/$dstfile" ]
272010cdda0Smrg	then
273010cdda0Smrg		$doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null ||
274010cdda0Smrg		$doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null ||
275010cdda0Smrg		{
276010cdda0Smrg		  echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
277010cdda0Smrg		  (exit 1); exit
278010cdda0Smrg		}
279010cdda0Smrg	else
280010cdda0Smrg		:
281010cdda0Smrg	fi
282010cdda0Smrg} &&
283010cdda0Smrg
284010cdda0Smrg# Now rename the file to the real destination.
285010cdda0Smrg
286010cdda0Smrg	$doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
287010cdda0Smrg
288010cdda0Smrgfi &&
289010cdda0Smrg
290010cdda0Smrg# The final little trick to "correctly" pass the exit status to the exit trap.
291010cdda0Smrg
292010cdda0Smrg{
293010cdda0Smrg	(exit 0); exit
294010cdda0Smrg}
295