install.sh revision 119610
11638Srgrimes#!/bin/sh
21638Srgrimes#
31638Srgrimes# install - install a program, script, or datafile
41638Srgrimes# This comes from X11R5.
51638Srgrimes#
61638Srgrimes# $XConsortium: install.sh,v 1.2 89/12/18 14:47:22 jim Exp $
71638Srgrimes#
81638Srgrimes# Copyright 1991 by the Massachusetts Institute of Technology
91638Srgrimes#
101638Srgrimes# Permission to use, copy, modify, distribute, and sell this software and its
111638Srgrimes# documentation for any purpose is hereby granted without fee, provided that
121638Srgrimes# the above copyright notice appear in all copies and that both that
131638Srgrimes# copyright notice and this permission notice appear in supporting
141638Srgrimes# documentation, and that the name of M.I.T. not be used in advertising or
151638Srgrimes# publicity pertaining to distribution of the software without specific,
161638Srgrimes# written prior permission.  M.I.T. makes no representations about the
171638Srgrimes# suitability of this software for any purpose.  It is provided "as is"
181638Srgrimes# without express or implied warranty.
191638Srgrimes#
201638Srgrimes# This script is compatible with the BSD install script, but was written
211638Srgrimes# from scratch.
221638Srgrimes#
231638Srgrimes
241638Srgrimes# set DOITPROG to echo to test this script
251638Srgrimes
261638Srgrimes# Don't use :- since 4.3BSD and earlier shells don't like it.
271638Srgrimesdoit="${DOITPROG-}"
281638Srgrimes
291638Srgrimes
301638Srgrimes# put in absolute paths if you don't have them in your path; or use env. vars.
311638Srgrimes
321638Srgrimesmvprog="${MVPROG-mv}"
331638Srgrimescpprog="${CPPROG-cp}"
341638Srgrimeschmodprog="${CHMODPROG-chmod}"
351638Srgrimeschownprog="${CHOWNPROG-chown}"
361638Srgrimeschgrpprog="${CHGRPPROG-chgrp}"
371638Srgrimesstripprog="${STRIPPROG-strip}"
381638Srgrimesrmprog="${RMPROG-rm}"
391638Srgrimesmkdirprog="${MKDIRPROG-mkdir}"
401638Srgrimes
411638Srgrimestranformbasename=""
421638Srgrimestransform_arg=""
431638Srgrimesinstcmd="$mvprog"
441638Srgrimeschmodcmd="$chmodprog 0755"
451638Srgrimeschowncmd=""
461638Srgrimeschgrpcmd=""
471638Srgrimesstripcmd=""
481638Srgrimesrmcmd="$rmprog -f"
491638Srgrimesmvcmd="$mvprog"
501638Srgrimessrc=""
511638Srgrimesdst=""
521638Srgrimesdir_arg=""
531638Srgrimes
541638Srgrimeswhile [ x"$1" != x ]; do
551638Srgrimes    case $1 in
561638Srgrimes	-c) instcmd="$cpprog"
571638Srgrimes	    shift
581638Srgrimes	    continue;;
591638Srgrimes
601638Srgrimes	-d) dir_arg=true
611638Srgrimes	    shift
621638Srgrimes	    continue;;
631638Srgrimes
641638Srgrimes	-m) chmodcmd="$chmodprog $2"
651638Srgrimes	    shift
661638Srgrimes	    shift
671638Srgrimes	    continue;;
681638Srgrimes
691638Srgrimes	-o) chowncmd="$chownprog $2"
701638Srgrimes	    shift
711638Srgrimes	    shift
721638Srgrimes	    continue;;
731638Srgrimes
741638Srgrimes	-g) chgrpcmd="$chgrpprog $2"
751638Srgrimes	    shift
76	    shift
77	    continue;;
78
79	-s) stripcmd="$stripprog"
80	    shift
81	    continue;;
82
83	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
84	    shift
85	    continue;;
86
87	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
88	    shift
89	    continue;;
90
91	*)  if [ x"$src" = x ]
92	    then
93		src=$1
94	    else
95		# this colon is to work around a 386BSD /bin/sh bug
96		:
97		dst=$1
98	    fi
99	    shift
100	    continue;;
101    esac
102done
103
104if [ x"$src" = x ]
105then
106	echo "install:	no input file specified"
107	exit 1
108else
109	true
110fi
111
112if [ x"$dir_arg" != x ]; then
113	dst=$src
114	src=""
115	
116	if [ -d $dst ]; then
117		instcmd=:
118	else
119		instcmd=mkdir
120	fi
121else
122
123# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
124# might cause directories to be created, which would be especially bad 
125# if $src (and thus $dsttmp) contains '*'.
126
127	if [ -f $src -o -d $src ]
128	then
129		true
130	else
131		echo "install:  $src does not exist"
132		exit 1
133	fi
134	
135	if [ x"$dst" = x ]
136	then
137		echo "install:	no destination specified"
138		exit 1
139	else
140		true
141	fi
142
143# If destination is a directory, append the input filename; if your system
144# does not like double slashes in filenames, you may need to add some logic
145
146	if [ -d $dst ]
147	then
148		dst="$dst"/`basename $src`
149	else
150		true
151	fi
152fi
153
154## this sed command emulates the dirname command
155dstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
156
157# Make sure that the destination directory exists.
158#  this part is taken from Noah Friedman's mkinstalldirs script
159
160# Skip lots of stat calls in the usual case.
161if [ ! -d "$dstdir" ]; then
162defaultIFS='	
163'
164IFS="${IFS-${defaultIFS}}"
165
166oIFS="${IFS}"
167# Some sh's can't handle IFS=/ for some reason.
168IFS='%'
169set - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
170IFS="${oIFS}"
171
172pathcomp=''
173
174while [ $# -ne 0 ] ; do
175	pathcomp="${pathcomp}${1}"
176	shift
177
178	if [ ! -d "${pathcomp}" ] ;
179        then
180		$mkdirprog "${pathcomp}"
181	else
182		true
183	fi
184
185	pathcomp="${pathcomp}/"
186done
187fi
188
189if [ x"$dir_arg" != x ]
190then
191	$doit $instcmd $dst &&
192
193	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
194	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
195	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
196	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
197else
198
199# If we're going to rename the final executable, determine the name now.
200
201	if [ x"$transformarg" = x ] 
202	then
203		dstfile=`basename $dst`
204	else
205		dstfile=`basename $dst $transformbasename | 
206			sed $transformarg`$transformbasename
207	fi
208
209# don't allow the sed command to completely eliminate the filename
210
211	if [ x"$dstfile" = x ] 
212	then
213		dstfile=`basename $dst`
214	else
215		true
216	fi
217
218# Make a temp file name in the proper directory.
219
220	dsttmp=$dstdir/#inst.$$#
221
222# Move or copy the file name to the temp name
223
224	$doit $instcmd $src $dsttmp &&
225
226	trap "rm -f ${dsttmp}" 0 &&
227
228# and set any options; do chmod last to preserve setuid bits
229
230# If any of these fail, we abort the whole thing.  If we want to
231# ignore errors from any of these, just make sure not to ignore
232# errors from the above "$doit $instcmd $src $dsttmp" command.
233
234	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
235	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
236	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
237	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
238
239# Now rename the file to the real destination.
240
241	$doit $rmcmd -f $dstdir/$dstfile &&
242	$doit $mvcmd $dsttmp $dstdir/$dstfile 
243
244fi &&
245
246
247exit 0
248