xref: /netbsd-src/external/gpl2/rcs/dist/install-sh (revision 7bdc26784bf54dccf8e31c40b2af54b918f70f81)
1*7bdc2678Schristos#! /bin/sh
2*7bdc2678Schristos#
3*7bdc2678Schristos# install - install a program, script, or datafile
4*7bdc2678Schristos# This comes from X11R5.
5*7bdc2678Schristos#
6*7bdc2678Schristos# Calling this script install-sh is preferred over install.sh, to prevent
7*7bdc2678Schristos# `make' implicit rules from creating a file called install from it
8*7bdc2678Schristos# when there is no Makefile.
9*7bdc2678Schristos#
10*7bdc2678Schristos# This script is compatible with the BSD install script, but was written
11*7bdc2678Schristos# from scratch.
12*7bdc2678Schristos#
13*7bdc2678Schristos
14*7bdc2678Schristos
15*7bdc2678Schristos# set DOITPROG to echo to test this script
16*7bdc2678Schristos
17*7bdc2678Schristos# Don't use :- since 4.3BSD and earlier shells don't like it.
18*7bdc2678Schristosdoit="${DOITPROG-}"
19*7bdc2678Schristos
20*7bdc2678Schristos
21*7bdc2678Schristos# put in absolute paths if you don't have them in your path; or use env. vars.
22*7bdc2678Schristos
23*7bdc2678Schristosmvprog="${MVPROG-mv}"
24*7bdc2678Schristoscpprog="${CPPROG-cp}"
25*7bdc2678Schristoschmodprog="${CHMODPROG-chmod}"
26*7bdc2678Schristoschownprog="${CHOWNPROG-chown}"
27*7bdc2678Schristoschgrpprog="${CHGRPPROG-chgrp}"
28*7bdc2678Schristosstripprog="${STRIPPROG-strip}"
29*7bdc2678Schristosrmprog="${RMPROG-rm}"
30*7bdc2678Schristosmkdirprog="${MKDIRPROG-mkdir}"
31*7bdc2678Schristos
32*7bdc2678Schristostranformbasename=""
33*7bdc2678Schristostransform_arg=""
34*7bdc2678Schristosinstcmd="$mvprog"
35*7bdc2678Schristoschmodcmd="$chmodprog 0755"
36*7bdc2678Schristoschowncmd=""
37*7bdc2678Schristoschgrpcmd=""
38*7bdc2678Schristosstripcmd=""
39*7bdc2678Schristosrmcmd="$rmprog -f"
40*7bdc2678Schristosmvcmd="$mvprog"
41*7bdc2678Schristossrc=""
42*7bdc2678Schristosdst=""
43*7bdc2678Schristosdir_arg=""
44*7bdc2678Schristos
45*7bdc2678Schristoswhile [ x"$1" != x ]; do
46*7bdc2678Schristos    case $1 in
47*7bdc2678Schristos	-c) instcmd="$cpprog"
48*7bdc2678Schristos	    shift
49*7bdc2678Schristos	    continue;;
50*7bdc2678Schristos
51*7bdc2678Schristos	-d) dir_arg=true
52*7bdc2678Schristos	    shift
53*7bdc2678Schristos	    continue;;
54*7bdc2678Schristos
55*7bdc2678Schristos	-m) chmodcmd="$chmodprog $2"
56*7bdc2678Schristos	    shift
57*7bdc2678Schristos	    shift
58*7bdc2678Schristos	    continue;;
59*7bdc2678Schristos
60*7bdc2678Schristos	-o) chowncmd="$chownprog $2"
61*7bdc2678Schristos	    shift
62*7bdc2678Schristos	    shift
63*7bdc2678Schristos	    continue;;
64*7bdc2678Schristos
65*7bdc2678Schristos	-g) chgrpcmd="$chgrpprog $2"
66*7bdc2678Schristos	    shift
67*7bdc2678Schristos	    shift
68*7bdc2678Schristos	    continue;;
69*7bdc2678Schristos
70*7bdc2678Schristos	-s) stripcmd="$stripprog"
71*7bdc2678Schristos	    shift
72*7bdc2678Schristos	    continue;;
73*7bdc2678Schristos
74*7bdc2678Schristos	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
75*7bdc2678Schristos	    shift
76*7bdc2678Schristos	    continue;;
77*7bdc2678Schristos
78*7bdc2678Schristos	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
79*7bdc2678Schristos	    shift
80*7bdc2678Schristos	    continue;;
81*7bdc2678Schristos
82*7bdc2678Schristos	*)  if [ x"$src" = x ]
83*7bdc2678Schristos	    then
84*7bdc2678Schristos		src=$1
85*7bdc2678Schristos	    else
86*7bdc2678Schristos		# this colon is to work around a 386BSD /bin/sh bug
87*7bdc2678Schristos		:
88*7bdc2678Schristos		dst=$1
89*7bdc2678Schristos	    fi
90*7bdc2678Schristos	    shift
91*7bdc2678Schristos	    continue;;
92*7bdc2678Schristos    esac
93*7bdc2678Schristosdone
94*7bdc2678Schristos
95*7bdc2678Schristosif [ x"$src" = x ]
96*7bdc2678Schristosthen
97*7bdc2678Schristos	echo "install:	no input file specified"
98*7bdc2678Schristos	exit 1
99*7bdc2678Schristoselse
100*7bdc2678Schristos	true
101*7bdc2678Schristosfi
102*7bdc2678Schristos
103*7bdc2678Schristosif [ x"$dir_arg" != x ]; then
104*7bdc2678Schristos	dst=$src
105*7bdc2678Schristos	src=""
106*7bdc2678Schristos
107*7bdc2678Schristos	if [ -d $dst ]; then
108*7bdc2678Schristos		instcmd=:
109*7bdc2678Schristos	else
110*7bdc2678Schristos		instcmd=mkdir
111*7bdc2678Schristos	fi
112*7bdc2678Schristoselse
113*7bdc2678Schristos
114*7bdc2678Schristos# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
115*7bdc2678Schristos# might cause directories to be created, which would be especially bad
116*7bdc2678Schristos# if $src (and thus $dsttmp) contains '*'.
117*7bdc2678Schristos
118*7bdc2678Schristos	if [ -f $src -o -d $src ]
119*7bdc2678Schristos	then
120*7bdc2678Schristos		true
121*7bdc2678Schristos	else
122*7bdc2678Schristos		echo "install:  $src does not exist"
123*7bdc2678Schristos		exit 1
124*7bdc2678Schristos	fi
125*7bdc2678Schristos
126*7bdc2678Schristos	if [ x"$dst" = x ]
127*7bdc2678Schristos	then
128*7bdc2678Schristos		echo "install:	no destination specified"
129*7bdc2678Schristos		exit 1
130*7bdc2678Schristos	else
131*7bdc2678Schristos		true
132*7bdc2678Schristos	fi
133*7bdc2678Schristos
134*7bdc2678Schristos# If destination is a directory, append the input filename; if your system
135*7bdc2678Schristos# does not like double slashes in filenames, you may need to add some logic
136*7bdc2678Schristos
137*7bdc2678Schristos	if [ -d $dst ]
138*7bdc2678Schristos	then
139*7bdc2678Schristos		dst="$dst"/`basename $src`
140*7bdc2678Schristos	else
141*7bdc2678Schristos		true
142*7bdc2678Schristos	fi
143*7bdc2678Schristosfi
144*7bdc2678Schristos
145*7bdc2678Schristos## this sed command emulates the dirname command
146*7bdc2678Schristosdstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
147*7bdc2678Schristos
148*7bdc2678Schristos# Make sure that the destination directory exists.
149*7bdc2678Schristos#  this part is taken from Noah Friedman's mkinstalldirs script
150*7bdc2678Schristos
151*7bdc2678Schristos# Skip lots of stat calls in the usual case.
152*7bdc2678Schristosif [ ! -d "$dstdir" ]; then
153*7bdc2678SchristosdefaultIFS='
154*7bdc2678Schristos'
155*7bdc2678SchristosIFS="${IFS-${defaultIFS}}"
156*7bdc2678Schristos
157*7bdc2678SchristosoIFS="${IFS}"
158*7bdc2678Schristos# Some sh's can't handle IFS=/ for some reason.
159*7bdc2678SchristosIFS='%'
160*7bdc2678Schristosset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
161*7bdc2678SchristosIFS="${oIFS}"
162*7bdc2678Schristos
163*7bdc2678Schristospathcomp=''
164*7bdc2678Schristos
165*7bdc2678Schristoswhile [ $# -ne 0 ] ; do
166*7bdc2678Schristos	pathcomp="${pathcomp}${1}"
167*7bdc2678Schristos	shift
168*7bdc2678Schristos
169*7bdc2678Schristos	if [ ! -d "${pathcomp}" ] ;
170*7bdc2678Schristos        then
171*7bdc2678Schristos		$mkdirprog "${pathcomp}"
172*7bdc2678Schristos	else
173*7bdc2678Schristos		true
174*7bdc2678Schristos	fi
175*7bdc2678Schristos
176*7bdc2678Schristos	pathcomp="${pathcomp}/"
177*7bdc2678Schristosdone
178*7bdc2678Schristosfi
179*7bdc2678Schristos
180*7bdc2678Schristosif [ x"$dir_arg" != x ]
181*7bdc2678Schristosthen
182*7bdc2678Schristos	$doit $instcmd $dst &&
183*7bdc2678Schristos
184*7bdc2678Schristos	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
185*7bdc2678Schristos	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
186*7bdc2678Schristos	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
187*7bdc2678Schristos	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
188*7bdc2678Schristoselse
189*7bdc2678Schristos
190*7bdc2678Schristos# If we're going to rename the final executable, determine the name now.
191*7bdc2678Schristos
192*7bdc2678Schristos	if [ x"$transformarg" = x ]
193*7bdc2678Schristos	then
194*7bdc2678Schristos		dstfile=`basename $dst`
195*7bdc2678Schristos	else
196*7bdc2678Schristos		dstfile=`basename $dst $transformbasename |
197*7bdc2678Schristos			sed $transformarg`$transformbasename
198*7bdc2678Schristos	fi
199*7bdc2678Schristos
200*7bdc2678Schristos# don't allow the sed command to completely eliminate the filename
201*7bdc2678Schristos
202*7bdc2678Schristos	if [ x"$dstfile" = x ]
203*7bdc2678Schristos	then
204*7bdc2678Schristos		dstfile=`basename $dst`
205*7bdc2678Schristos	else
206*7bdc2678Schristos		true
207*7bdc2678Schristos	fi
208*7bdc2678Schristos
209*7bdc2678Schristos# Make a temp file name in the proper directory.
210*7bdc2678Schristos
211*7bdc2678Schristos	dsttmp=$dstdir/#inst.$$#
212*7bdc2678Schristos
213*7bdc2678Schristos# Move or copy the file name to the temp name
214*7bdc2678Schristos
215*7bdc2678Schristos	$doit $instcmd $src $dsttmp &&
216*7bdc2678Schristos
217*7bdc2678Schristos	trap "rm -f ${dsttmp}" 0 &&
218*7bdc2678Schristos
219*7bdc2678Schristos# and set any options; do chmod last to preserve setuid bits
220*7bdc2678Schristos
221*7bdc2678Schristos# If any of these fail, we abort the whole thing.  If we want to
222*7bdc2678Schristos# ignore errors from any of these, just make sure not to ignore
223*7bdc2678Schristos# errors from the above "$doit $instcmd $src $dsttmp" command.
224*7bdc2678Schristos
225*7bdc2678Schristos	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
226*7bdc2678Schristos	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
227*7bdc2678Schristos	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
228*7bdc2678Schristos	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
229*7bdc2678Schristos
230*7bdc2678Schristos# Now rename the file to the real destination.
231*7bdc2678Schristos
232*7bdc2678Schristos	$doit $rmcmd -f $dstdir/$dstfile &&
233*7bdc2678Schristos	$doit $mvcmd $dsttmp $dstdir/$dstfile
234*7bdc2678Schristos
235*7bdc2678Schristosfi &&
236*7bdc2678Schristos
237*7bdc2678Schristos
238*7bdc2678Schristosexit 0
239