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