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