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