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