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