install-sh revision 102644
1178825Sdfr#!/bin/sh
2178825Sdfr#
3178825Sdfr# install - install a program, script, or datafile
4178825Sdfr# This comes from X11R5 (mit/util/scripts/install.sh).
5178825Sdfr#
6178825Sdfr# Copyright 1991 by the Massachusetts Institute of Technology
7178825Sdfr#
8178825Sdfr# Permission to use, copy, modify, distribute, and sell this software and its
9178825Sdfr# documentation for any purpose is hereby granted without fee, provided that
10178825Sdfr# the above copyright notice appear in all copies and that both that
11178825Sdfr# copyright notice and this permission notice appear in supporting
12178825Sdfr# documentation, and that the name of M.I.T. not be used in advertising or
13178825Sdfr# publicity pertaining to distribution of the software without specific,
14178825Sdfr# written prior permission.  M.I.T. makes no representations about the
15178825Sdfr# suitability of this software for any purpose.  It is provided "as is"
16178825Sdfr# without express or implied warranty.
17178825Sdfr#
18178825Sdfr# Calling this script install-sh is preferred over install.sh, to prevent
19178825Sdfr# `make' implicit rules from creating a file called install from it
20178825Sdfr# when there is no Makefile.
21178825Sdfr#
22178825Sdfr# This script is compatible with the BSD install script, but was written
23178825Sdfr# from scratch.  It can only install one file at a time, a restriction
24178825Sdfr# shared with many OS's install programs.
25178825Sdfr
26178825Sdfr
27178825Sdfr# set DOITPROG to echo to test this script
28178825Sdfr
29178825Sdfr# Don't use :- since 4.3BSD and earlier shells don't like it.
30178825Sdfrdoit="${DOITPROG-}"
31178825Sdfr
32178825Sdfr
33178825Sdfr# put in absolute paths if you don't have them in your path; or use env. vars.
34178825Sdfr
35178825Sdfrmvprog="${MVPROG-mv}"
36178825Sdfrcpprog="${CPPROG-cp}"
37178825Sdfrchmodprog="${CHMODPROG-chmod}"
38178825Sdfrchownprog="${CHOWNPROG-chown}"
39178825Sdfrchgrpprog="${CHGRPPROG-chgrp}"
40178825Sdfrstripprog="${STRIPPROG-strip}"
41178825Sdfrrmprog="${RMPROG-rm}"
42178825Sdfrmkdirprog="${MKDIRPROG-mkdir}"
43178825Sdfr
44178825Sdfrtransformbasename=""
45178825Sdfrtransform_arg=""
46178825Sdfrinstcmd="$mvprog"
47178825Sdfrchmodcmd="$chmodprog 0755"
48178825Sdfrchowncmd=""
49178825Sdfrchgrpcmd=""
50178825Sdfrstripcmd=""
51178825Sdfrrmcmd="$rmprog -f"
52178825Sdfrmvcmd="$mvprog"
53178825Sdfrsrc=""
54178825Sdfrdst=""
55178825Sdfrdir_arg=""
56178825Sdfr
57178825Sdfrwhile [ x"$1" != x ]; do
58178825Sdfr    case $1 in
59178825Sdfr	-c) instcmd="$cpprog"
60178825Sdfr	    shift
61178825Sdfr	    continue;;
62178825Sdfr
63178825Sdfr	-d) dir_arg=true
64178825Sdfr	    shift
65178825Sdfr	    continue;;
66178825Sdfr
67178825Sdfr	-m) chmodcmd="$chmodprog $2"
68178825Sdfr	    shift
69178825Sdfr	    shift
70178825Sdfr	    continue;;
71178825Sdfr
72178825Sdfr	-o) chowncmd="$chownprog $2"
73178825Sdfr	    shift
74178825Sdfr	    shift
75178825Sdfr	    continue;;
76178825Sdfr
77178825Sdfr	-g) chgrpcmd="$chgrpprog $2"
78178825Sdfr	    shift
79178825Sdfr	    shift
80178825Sdfr	    continue;;
81178825Sdfr
82178825Sdfr	-s) stripcmd="$stripprog"
83178825Sdfr	    shift
84178825Sdfr	    continue;;
85
86	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
87	    shift
88	    continue;;
89
90	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
91	    shift
92	    continue;;
93
94	*)  if [ x"$src" = x ]
95	    then
96		src=$1
97	    else
98		# this colon is to work around a 386BSD /bin/sh bug
99		:
100		dst=$1
101	    fi
102	    shift
103	    continue;;
104    esac
105done
106
107if [ x"$src" = x ]
108then
109	echo "install:	no input file specified"
110	exit 1
111else
112	:
113fi
114
115if [ x"$dir_arg" != x ]; then
116	dst=$src
117	src=""
118	
119	if [ -d $dst ]; then
120		instcmd=:
121		chmodcmd=""
122	else
123		instcmd=$mkdirprog
124	fi
125else
126
127# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
128# might cause directories to be created, which would be especially bad 
129# if $src (and thus $dsttmp) contains '*'.
130
131	if [ -f "$src" ] || [ -d "$src" ]
132	then
133		:
134	else
135		echo "install:  $src does not exist"
136		exit 1
137	fi
138	
139	if [ x"$dst" = x ]
140	then
141		echo "install:	no destination specified"
142		exit 1
143	else
144		:
145	fi
146
147# If destination is a directory, append the input filename; if your system
148# does not like double slashes in filenames, you may need to add some logic
149
150	if [ -d $dst ]
151	then
152		dst="$dst"/`basename $src`
153	else
154		:
155	fi
156fi
157
158## this sed command emulates the dirname command
159dstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
160
161# Make sure that the destination directory exists.
162#  this part is taken from Noah Friedman's mkinstalldirs script
163
164# Skip lots of stat calls in the usual case.
165if [ ! -d "$dstdir" ]; then
166defaultIFS='
167	'
168IFS="${IFS-${defaultIFS}}"
169
170oIFS="${IFS}"
171# Some sh's can't handle IFS=/ for some reason.
172IFS='%'
173set - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
174IFS="${oIFS}"
175
176pathcomp=''
177
178while [ $# -ne 0 ] ; do
179	pathcomp="${pathcomp}${1}"
180	shift
181
182	if [ ! -d "${pathcomp}" ] ;
183        then
184		$mkdirprog "${pathcomp}"
185	else
186		:
187	fi
188
189	pathcomp="${pathcomp}/"
190done
191fi
192
193if [ x"$dir_arg" != x ]
194then
195	$doit $instcmd $dst &&
196
197	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else : ; fi &&
198	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else : ; fi &&
199	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else : ; fi &&
200	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else : ; fi
201else
202
203# If we're going to rename the final executable, determine the name now.
204
205	if [ x"$transformarg" = x ] 
206	then
207		dstfile=`basename $dst`
208	else
209		dstfile=`basename $dst $transformbasename | 
210			sed $transformarg`$transformbasename
211	fi
212
213# don't allow the sed command to completely eliminate the filename
214
215	if [ x"$dstfile" = x ] 
216	then
217		dstfile=`basename $dst`
218	else
219		:
220	fi
221
222# Make a temp file name in the proper directory.
223
224	dsttmp=$dstdir/#inst.$$#
225
226# Move or copy the file name to the temp name
227
228	$doit $instcmd $src $dsttmp &&
229
230	trap "rm -f ${dsttmp}" 0 &&
231
232# and set any options; do chmod last to preserve setuid bits
233
234# If any of these fail, we abort the whole thing.  If we want to
235# ignore errors from any of these, just make sure not to ignore
236# errors from the above "$doit $instcmd $src $dsttmp" command.
237
238	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else :;fi &&
239	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else :;fi &&
240	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else :;fi &&
241	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else :;fi &&
242
243# Now rename the file to the real destination.
244
245	$doit $rmcmd -f $dstdir/$dstfile &&
246	$doit $mvcmd $dsttmp $dstdir/$dstfile 
247
248fi &&
249
250
251exit 0
252