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