install-sh revision 55682
167754Smsmith#! /bin/sh
267754Smsmith#
3193267Sjkim# install - install a program, script, or datafile
467754Smsmith# This comes from X11R5.
567754Smsmith#
667754Smsmith# Calling this script install-sh is preferred over install.sh, to prevent
767754Smsmith# `make' implicit rules from creating a file called install from it
8217365Sjkim# when there is no Makefile.
9298714Sjkim#
1070243Smsmith# This script is compatible with the BSD install script, but was written
1167754Smsmith# from scratch.
12217365Sjkim#
13217365Sjkim
14217365Sjkim
15217365Sjkim# set DOITPROG to echo to test this script
16217365Sjkim
17217365Sjkim# Don't use :- since 4.3BSD and earlier shells don't like it.
18217365Sjkimdoit="${DOITPROG-}"
19217365Sjkim
20217365Sjkim
21217365Sjkim# put in absolute paths if you don't have them in your path; or use env. vars.
22217365Sjkim
23217365Sjkimmvprog="${MVPROG-mv}"
24217365Sjkimcpprog="${CPPROG-cp}"
25217365Sjkimchmodprog="${CHMODPROG-chmod}"
2667754Smsmithchownprog="${CHOWNPROG-chown}"
27217365Sjkimchgrpprog="${CHGRPPROG-chgrp}"
28217365Sjkimstripprog="${STRIPPROG-strip}"
29217365Sjkimrmprog="${RMPROG-rm}"
3067754Smsmithmkdirprog="${MKDIRPROG-mkdir}"
31217365Sjkim
32217365Sjkimtranformbasename=""
33217365Sjkimtransform_arg=""
34217365Sjkiminstcmd="$mvprog"
35217365Sjkimchmodcmd="$chmodprog 0755"
36217365Sjkimchowncmd=""
37217365Sjkimchgrpcmd=""
38217365Sjkimstripcmd=""
39217365Sjkimrmcmd="$rmprog -f"
40217365Sjkimmvcmd="$mvprog"
41217365Sjkimsrc=""
42217365Sjkimdst=""
43217365Sjkimdir_arg=""
4467754Smsmith
45193341Sjkimwhile [ x"$1" != x ]; do
46193341Sjkim    case $1 in
47193341Sjkim	-c) instcmd="$cpprog"
48193341Sjkim	    shift
49298714Sjkim	    continue;;
50298714Sjkim
51298714Sjkim	-d) dir_arg=true
52298714Sjkim	    shift
5367754Smsmith	    continue;;
54102550Siwasaki
5591116Smsmith	-m) chmodcmd="$chmodprog $2"
5667754Smsmith	    shift
57287168Sjkim	    shift
58102550Siwasaki	    continue;;
5967754Smsmith
6067754Smsmith	-o) chowncmd="$chownprog $2"
6167754Smsmith	    shift
6267754Smsmith	    shift
6367754Smsmith	    continue;;
6467754Smsmith
65151937Sjkim	-g) chgrpcmd="$chgrpprog $2"
6667754Smsmith	    shift
6767754Smsmith	    shift
6867754Smsmith	    continue;;
6967754Smsmith
7067754Smsmith	-s) stripcmd="$stripprog"
7167754Smsmith	    shift
7267754Smsmith	    continue;;
7367754Smsmith
7467754Smsmith	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
7567754Smsmith	    shift
7667754Smsmith	    continue;;
7767754Smsmith
7883174Smsmith	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
7967754Smsmith	    shift
8083174Smsmith	    continue;;
8183174Smsmith
8283174Smsmith	*)  if [ x"$src" = x ]
83287168Sjkim	    then
84287168Sjkim		src=$1
8567754Smsmith	    else
8667754Smsmith		# this colon is to work around a 386BSD /bin/sh bug
8767754Smsmith		:
8867754Smsmith		dst=$1
8967754Smsmith	    fi
9067754Smsmith	    shift
9167754Smsmith	    continue;;
9267754Smsmith    esac
9367754Smsmithdone
9467754Smsmith
9567754Smsmithif [ x"$src" = x ]
96151937Sjkimthen
9767754Smsmith	echo "install:	no input file specified"
9867754Smsmith	exit 1
9967754Smsmithelse
10067754Smsmith	true
10167754Smsmithfi
10267754Smsmith
10367754Smsmithif [ x"$dir_arg" != x ]; then
104114237Snjl	dst=$src
10567754Smsmith	src=""
10667754Smsmith	
10767754Smsmith	if [ -d $dst ]; then
10867754Smsmith		instcmd=:
10967754Smsmith	else
11083174Smsmith		instcmd=mkdir
111243347Sjkim	fi
11267754Smsmithelse
11373561Smsmith
114243347Sjkim# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
11573561Smsmith# might cause directories to be created, which would be especially bad 
11667754Smsmith# if $src (and thus $dsttmp) contains '*'.
117243347Sjkim
118284583Sjkim	if [ -f $src -o -d $src ]
119254745Sjkim	then
120243347Sjkim		true
121243347Sjkim	else
12267754Smsmith		echo "install:  $src does not exist"
12367754Smsmith		exit 1
124100966Siwasaki	fi
12567754Smsmith	
12667754Smsmith	if [ x"$dst" = x ]
127117521Snjl	then
128117521Snjl		echo "install:	no destination specified"
129298714Sjkim		exit 1
13067754Smsmith	else
131298714Sjkim		true
13267754Smsmith	fi
13367754Smsmith
13467754Smsmith# If destination is a directory, append the input filename; if your system
135298714Sjkim# does not like double slashes in filenames, you may need to add some logic
13667754Smsmith
13767754Smsmith	if [ -d $dst ]
13867754Smsmith	then
13999146Siwasaki		dst="$dst"/`basename $src`
140298714Sjkim	else
141298714Sjkim		true
14299146Siwasaki	fi
14399146Siwasakifi
144298714Sjkim
145114237Snjl## this sed command emulates the dirname command
14699146Siwasakidstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
14799146Siwasaki
148298714Sjkim# Make sure that the destination directory exists.
14999146Siwasaki#  this part is taken from Noah Friedman's mkinstalldirs script
150298714Sjkim
151298714Sjkim# Skip lots of stat calls in the usual case.
152283092Sjkimif [ ! -d "$dstdir" ]; then
153298714SjkimdefaultIFS='	
15499146Siwasaki'
155298714SjkimIFS="${IFS-${defaultIFS}}"
156167802Sjkim
15767754SmsmithoIFS="${IFS}"
158167802Sjkim# Some sh's can't handle IFS=/ for some reason.
159167802SjkimIFS='%'
160167802Sjkimset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
161167802SjkimIFS="${oIFS}"
162167802Sjkim
163167802Sjkimpathcomp=''
164167802Sjkim
165167802Sjkimwhile [ $# -ne 0 ] ; do
166167802Sjkim	pathcomp="${pathcomp}${1}"
167167802Sjkim	shift
168167802Sjkim
169167802Sjkim	if [ ! -d "${pathcomp}" ] ;
17067754Smsmith        then
17180062Smsmith		$mkdirprog "${pathcomp}"
172167802Sjkim	else
173167802Sjkim		true
174167802Sjkim	fi
17567754Smsmith
176298714Sjkim	pathcomp="${pathcomp}/"
177114237Snjldone
17867754Smsmithfi
17967754Smsmith
18067754Smsmithif [ x"$dir_arg" != x ]
181then
182	$doit $instcmd $dst &&
183
184	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
185	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
186	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
187	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
188else
189
190# If we're going to rename the final executable, determine the name now.
191
192	if [ x"$transformarg" = x ] 
193	then
194		dstfile=`basename $dst`
195	else
196		dstfile=`basename $dst $transformbasename | 
197			sed $transformarg`$transformbasename
198	fi
199
200# don't allow the sed command to completely eliminate the filename
201
202	if [ x"$dstfile" = x ] 
203	then
204		dstfile=`basename $dst`
205	else
206		true
207	fi
208
209# Make a temp file name in the proper directory.
210
211	dsttmp=$dstdir/#inst.$$#
212
213# Move or copy the file name to the temp name
214
215	$doit $instcmd $src $dsttmp &&
216
217	trap "rm -f ${dsttmp}" 0 &&
218
219# and set any options; do chmod last to preserve setuid bits
220
221# If any of these fail, we abort the whole thing.  If we want to
222# ignore errors from any of these, just make sure not to ignore
223# errors from the above "$doit $instcmd $src $dsttmp" command.
224
225	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
226	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
227	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
228	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
229
230# Now rename the file to the real destination.
231
232	$doit $rmcmd -f $dstdir/$dstfile &&
233	$doit $mvcmd $dsttmp $dstdir/$dstfile 
234
235fi &&
236
237
238exit 0
239