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