1217309Snwhitehorn#! /bin/sh
2217309Snwhitehorn#
3217309Snwhitehorn# install - install a program, script, or datafile
4217309Snwhitehorn#
5217309Snwhitehorn# This originates from X11R5 (mit/util/scripts/install.sh), which was
6217309Snwhitehorn# later released in X11R6 (xc/config/util/install.sh) with the
7217309Snwhitehorn# following copyright and license.
8217309Snwhitehorn#
9217309Snwhitehorn# Copyright (C) 1994 X Consortium
10217309Snwhitehorn#
11217309Snwhitehorn# Permission is hereby granted, free of charge, to any person obtaining a copy
12217309Snwhitehorn# of this software and associated documentation files (the "Software"), to
13217309Snwhitehorn# deal in the Software without restriction, including without limitation the
14217309Snwhitehorn# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
15217309Snwhitehorn# sell copies of the Software, and to permit persons to whom the Software is
16217309Snwhitehorn# furnished to do so, subject to the following conditions:
17217309Snwhitehorn#
18217309Snwhitehorn# The above copyright notice and this permission notice shall be included in
19217309Snwhitehorn# all copies or substantial portions of the Software.
20217309Snwhitehorn#
21217309Snwhitehorn# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22217309Snwhitehorn# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23217309Snwhitehorn# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
24217309Snwhitehorn# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
25217309Snwhitehorn# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
26217309Snwhitehorn# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27217309Snwhitehorn#
28217309Snwhitehorn# Except as contained in this notice, the name of the X Consortium shall not
29217309Snwhitehorn# be used in advertising or otherwise to promote the sale, use or other deal-
30217309Snwhitehorn# ings in this Software without prior written authorization from the X Consor-
31217309Snwhitehorn# tium.
32217309Snwhitehorn#
33217309Snwhitehorn#
34217309Snwhitehorn# FSF changes to this file are in the public domain.
35217309Snwhitehorn#
36217309Snwhitehorn# Calling this script install-sh is preferred over install.sh, to prevent
37217309Snwhitehorn# `make' implicit rules from creating a file called install from it
38217309Snwhitehorn# when there is no Makefile.
39217309Snwhitehorn#
40217309Snwhitehorn# This script is compatible with the BSD install script, but was written
41217309Snwhitehorn# from scratch.  It can only install one file at a time, a restriction
42217309Snwhitehorn# shared with many OS's install programs.
43217309Snwhitehorn
44217309Snwhitehorn
45217309Snwhitehorn# set DOITPROG to echo to test this script
46217309Snwhitehorn
47217309Snwhitehorn# Don't use :- since 4.3BSD and earlier shells don't like it.
48217309Snwhitehorndoit="${DOITPROG-}"
49217309Snwhitehorn
50217309Snwhitehorn
51217309Snwhitehorn# put in absolute paths if you don't have them in your path; or use env. vars.
52217309Snwhitehorn
53217309Snwhitehornmvprog="${MVPROG-mv}"
54217309Snwhitehorncpprog="${CPPROG-cp}"
55217309Snwhitehornchmodprog="${CHMODPROG-chmod}"
56217309Snwhitehornchownprog="${CHOWNPROG-chown}"
57217309Snwhitehornchgrpprog="${CHGRPPROG-chgrp}"
58217309Snwhitehornstripprog="${STRIPPROG-strip}"
59217309Snwhitehornrmprog="${RMPROG-rm}"
60217309Snwhitehornmkdirprog="${MKDIRPROG-mkdir}"
61217309Snwhitehorn
62217309Snwhitehorntransformbasename=""
63217309Snwhitehorntransform_arg=""
64217309Snwhitehorninstcmd="$mvprog"
65217309Snwhitehornchmodcmd="$chmodprog 0755"
66217309Snwhitehornchowncmd=""
67217309Snwhitehornchgrpcmd=""
68217309Snwhitehornstripcmd=""
69217309Snwhitehornrmcmd="$rmprog -f"
70217309Snwhitehornmvcmd="$mvprog"
71217309Snwhitehornsrc=""
72217309Snwhitehorndst=""
73217309Snwhitehorndir_arg=""
74217309Snwhitehorn
75217309Snwhitehornwhile [ x"$1" != x ]; do
76217309Snwhitehorn    case $1 in
77217309Snwhitehorn	-c) instcmd=$cpprog
78217309Snwhitehorn	    shift
79217309Snwhitehorn	    continue;;
80217309Snwhitehorn
81217309Snwhitehorn	-d) dir_arg=true
82217309Snwhitehorn	    shift
83217309Snwhitehorn	    continue;;
84217309Snwhitehorn
85217309Snwhitehorn	-m) chmodcmd="$chmodprog $2"
86217309Snwhitehorn	    shift
87217309Snwhitehorn	    shift
88217309Snwhitehorn	    continue;;
89217309Snwhitehorn
90217309Snwhitehorn	-o) chowncmd="$chownprog $2"
91217309Snwhitehorn	    shift
92217309Snwhitehorn	    shift
93217309Snwhitehorn	    continue;;
94217309Snwhitehorn
95217309Snwhitehorn	-g) chgrpcmd="$chgrpprog $2"
96217309Snwhitehorn	    shift
97217309Snwhitehorn	    shift
98217309Snwhitehorn	    continue;;
99217309Snwhitehorn
100217309Snwhitehorn	-s) stripcmd=$stripprog
101217309Snwhitehorn	    shift
102217309Snwhitehorn	    continue;;
103217309Snwhitehorn
104217309Snwhitehorn	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
105217309Snwhitehorn	    shift
106217309Snwhitehorn	    continue;;
107217309Snwhitehorn
108217309Snwhitehorn	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
109217309Snwhitehorn	    shift
110217309Snwhitehorn	    continue;;
111217309Snwhitehorn
112217309Snwhitehorn	*)  if [ x"$src" = x ]
113217309Snwhitehorn	    then
114217309Snwhitehorn		src=$1
115217309Snwhitehorn	    else
116217309Snwhitehorn		# this colon is to work around a 386BSD /bin/sh bug
117217309Snwhitehorn		:
118217309Snwhitehorn		dst=$1
119217309Snwhitehorn	    fi
120217309Snwhitehorn	    shift
121217309Snwhitehorn	    continue;;
122217309Snwhitehorn    esac
123217309Snwhitehorndone
124217309Snwhitehorn
125217309Snwhitehornif [ x"$src" = x ]
126217309Snwhitehornthen
127217309Snwhitehorn	echo "$0: no input file specified" >&2
128217309Snwhitehorn	exit 1
129217309Snwhitehornelse
130217309Snwhitehorn	:
131217309Snwhitehornfi
132217309Snwhitehorn
133217309Snwhitehornif [ x"$dir_arg" != x ]; then
134217309Snwhitehorn	dst=$src
135217309Snwhitehorn	src=""
136217309Snwhitehorn
137217309Snwhitehorn	if [ -d "$dst" ]; then
138217309Snwhitehorn		instcmd=:
139217309Snwhitehorn		chmodcmd=""
140217309Snwhitehorn	else
141217309Snwhitehorn		instcmd=$mkdirprog
142217309Snwhitehorn	fi
143217309Snwhitehornelse
144217309Snwhitehorn
145217309Snwhitehorn# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
146217309Snwhitehorn# might cause directories to be created, which would be especially bad
147217309Snwhitehorn# if $src (and thus $dsttmp) contains '*'.
148217309Snwhitehorn
149217309Snwhitehorn	if [ -f "$src" ] || [ -d "$src" ]
150217309Snwhitehorn	then
151217309Snwhitehorn		:
152217309Snwhitehorn	else
153217309Snwhitehorn		echo "$0: $src does not exist" >&2
154217309Snwhitehorn		exit 1
155217309Snwhitehorn	fi
156217309Snwhitehorn
157217309Snwhitehorn	if [ x"$dst" = x ]
158217309Snwhitehorn	then
159217309Snwhitehorn		echo "$0: no destination specified" >&2
160217309Snwhitehorn		exit 1
161217309Snwhitehorn	else
162217309Snwhitehorn		:
163217309Snwhitehorn	fi
164217309Snwhitehorn
165217309Snwhitehorn# If destination is a directory, append the input filename; if your system
166217309Snwhitehorn# does not like double slashes in filenames, you may need to add some logic
167217309Snwhitehorn
168217309Snwhitehorn	if [ -d "$dst" ]
169217309Snwhitehorn	then
170217309Snwhitehorn		dst=$dst/`basename "$src"`
171217309Snwhitehorn	else
172217309Snwhitehorn		:
173217309Snwhitehorn	fi
174217309Snwhitehornfi
175217309Snwhitehorn
176217309Snwhitehorn## this sed command emulates the dirname command
177217309Snwhitehorndstdir=`echo "$dst" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
178217309Snwhitehorn
179217309Snwhitehorn# Make sure that the destination directory exists.
180217309Snwhitehorn#  this part is taken from Noah Friedman's mkinstalldirs script
181217309Snwhitehorn
182217309Snwhitehorn# Skip lots of stat calls in the usual case.
183217309Snwhitehornif [ ! -d "$dstdir" ]; then
184217309SnwhitehorndefaultIFS='
185217309Snwhitehorn	'
186217309SnwhitehornIFS="${IFS-$defaultIFS}"
187217309Snwhitehorn
188217309SnwhitehornoIFS=$IFS
189217309Snwhitehorn# Some sh's can't handle IFS=/ for some reason.
190217309SnwhitehornIFS='%'
191217309Snwhitehornset - `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
192217309SnwhitehornIFS=$oIFS
193217309Snwhitehorn
194217309Snwhitehornpathcomp=''
195217309Snwhitehorn
196217309Snwhitehornwhile [ $# -ne 0 ] ; do
197217309Snwhitehorn	pathcomp=$pathcomp$1
198217309Snwhitehorn	shift
199217309Snwhitehorn
200217309Snwhitehorn	if [ ! -d "$pathcomp" ] ;
201217309Snwhitehorn        then
202217309Snwhitehorn		$mkdirprog "$pathcomp"
203217309Snwhitehorn	else
204217309Snwhitehorn		:
205217309Snwhitehorn	fi
206217309Snwhitehorn
207217309Snwhitehorn	pathcomp=$pathcomp/
208217309Snwhitehorndone
209217309Snwhitehornfi
210217309Snwhitehorn
211217309Snwhitehornif [ x"$dir_arg" != x ]
212217309Snwhitehornthen
213217309Snwhitehorn	$doit $instcmd "$dst" &&
214217309Snwhitehorn
215217309Snwhitehorn	if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dst"; else : ; fi &&
216217309Snwhitehorn	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dst"; else : ; fi &&
217217309Snwhitehorn	if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dst"; else : ; fi &&
218217309Snwhitehorn	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dst"; else : ; fi
219217309Snwhitehornelse
220217309Snwhitehorn
221217309Snwhitehorn# If we're going to rename the final executable, determine the name now.
222217309Snwhitehorn
223217309Snwhitehorn	if [ x"$transformarg" = x ]
224217309Snwhitehorn	then
225217309Snwhitehorn		dstfile=`basename "$dst"`
226217309Snwhitehorn	else
227217309Snwhitehorn		dstfile=`basename "$dst" $transformbasename |
228217309Snwhitehorn			sed $transformarg`$transformbasename
229217309Snwhitehorn	fi
230217309Snwhitehorn
231217309Snwhitehorn# don't allow the sed command to completely eliminate the filename
232217309Snwhitehorn
233217309Snwhitehorn	if [ x"$dstfile" = x ]
234217309Snwhitehorn	then
235217309Snwhitehorn		dstfile=`basename "$dst"`
236217309Snwhitehorn	else
237217309Snwhitehorn		:
238217309Snwhitehorn	fi
239217309Snwhitehorn
240217309Snwhitehorn# Make a couple of temp file names in the proper directory.
241217309Snwhitehorn
242217309Snwhitehorn	dsttmp=$dstdir/#inst.$$#
243217309Snwhitehorn	rmtmp=$dstdir/#rm.$$#
244217309Snwhitehorn
245217309Snwhitehorn# Trap to clean up temp files at exit.
246217309Snwhitehorn
247217309Snwhitehorn	trap 'status=$?; rm -f "$dsttmp" "$rmtmp" && exit $status' 0
248217309Snwhitehorn	trap '(exit $?); exit' 1 2 13 15
249217309Snwhitehorn
250217309Snwhitehorn# Move or copy the file name to the temp name
251217309Snwhitehorn
252217309Snwhitehorn	$doit $instcmd "$src" "$dsttmp" &&
253217309Snwhitehorn
254217309Snwhitehorn# and set any options; do chmod last to preserve setuid bits
255217309Snwhitehorn
256217309Snwhitehorn# If any of these fail, we abort the whole thing.  If we want to
257217309Snwhitehorn# ignore errors from any of these, just make sure not to ignore
258217309Snwhitehorn# errors from the above "$doit $instcmd $src $dsttmp" command.
259217309Snwhitehorn
260217309Snwhitehorn	if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp"; else :;fi &&
261217309Snwhitehorn	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp"; else :;fi &&
262217309Snwhitehorn	if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dsttmp"; else :;fi &&
263217309Snwhitehorn	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; else :;fi &&
264217309Snwhitehorn
265217309Snwhitehorn# Now remove or move aside any old file at destination location.  We try this
266217309Snwhitehorn# two ways since rm can't unlink itself on some systems and the destination
267217309Snwhitehorn# file might be busy for other reasons.  In this case, the final cleanup
268217309Snwhitehorn# might fail but the new file should still install successfully.
269217309Snwhitehorn
270217309Snwhitehorn{
271217309Snwhitehorn	if [ -f "$dstdir/$dstfile" ]
272217309Snwhitehorn	then
273217309Snwhitehorn		$doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null ||
274217309Snwhitehorn		$doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null ||
275217309Snwhitehorn		{
276217309Snwhitehorn		  echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
277217309Snwhitehorn		  (exit 1); exit
278217309Snwhitehorn		}
279217309Snwhitehorn	else
280217309Snwhitehorn		:
281217309Snwhitehorn	fi
282217309Snwhitehorn} &&
283217309Snwhitehorn
284217309Snwhitehorn# Now rename the file to the real destination.
285217309Snwhitehorn
286217309Snwhitehorn	$doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
287217309Snwhitehorn
288217309Snwhitehornfi &&
289217309Snwhitehorn
290217309Snwhitehorn# The final little trick to "correctly" pass the exit status to the exit trap.
291217309Snwhitehorn
292217309Snwhitehorn{
293217309Snwhitehorn	(exit 0); exit
294217309Snwhitehorn}
295