1#!/bin/sh
2#
3# "$Id: install-sh 11093 2013-07-03 20:48:42Z msweet $"
4#
5# Install a program, script, or datafile.
6#
7# Copyright 2008-2012 by Apple Inc.
8#
9# This script is not compatible with BSD (or any other) install program, as it
10# allows owner and group changes to fail with a warning and makes sure that the
11# destination directory permissions are as specified - BSD install and the
12# original X11 install script did not change permissions of existing
13# directories.  It also does not support the transform options since CUPS does
14# not use them...
15#
16# Original script from X11R5 (mit/util/scripts/install.sh)
17# Copyright 1991 by the Massachusetts Institute of Technology
18#
19# Permission to use, copy, modify, distribute, and sell this software and its
20# documentation for any purpose is hereby granted without fee, provided that
21# the above copyright notice appear in all copies and that both that
22# copyright notice and this permission notice appear in supporting
23# documentation, and that the name of M.I.T. not be used in advertising or
24# publicity pertaining to distribution of the software without specific,
25# written prior permission.  M.I.T. makes no representations about the
26# suitability of this software for any purpose.  It is provided "as is"
27# without express or implied warranty.
28#
29# Calling this script install-sh is preferred over install.sh, to prevent
30# `make' implicit rules from creating a file called install from it
31# when there is no Makefile.
32
33# set DOITPROG to echo to test this script
34# Don't use :- since 4.3BSD and earlier shells don't like it.
35doit="${DOITPROG-}"
36
37# Force umask to 022...
38umask 022
39
40# put in absolute paths if you don't have them in your path; or use env. vars.
41mvprog="${MVPROG-mv}"
42cpprog="${CPPROG-cp}"
43chmodprog="${CHMODPROG-chmod}"
44chownprog="${CHOWNPROG-chown}"
45chgrpprog="${CHGRPPROG-chgrp}"
46stripprog="${STRIPPROG-strip}"
47rmprog="${RMPROG-rm}"
48mkdirprog="${MKDIRPROG-mkdir}"
49gzipprog="${GZIPPROG-gzip}"
50
51transformbasename=""
52transform_arg=""
53instcmd="$mvprog"
54chmodcmd="$chmodprog 0755"
55chowncmd=""
56chgrpcmd=""
57stripcmd=""
58rmcmd="$rmprog -f"
59mvcmd="$mvprog"
60src=""
61dst=""
62dir_arg=""
63
64gzipcp() {
65	# gzipcp from to
66	$gzipprog -9 <"$1" >"$2"
67}
68
69while [ x"$1" != x ]; do
70	case $1 in
71		-c)
72		instcmd="$cpprog"
73		shift
74		continue
75		;;
76
77		-d)
78		dir_arg=true
79		shift
80		continue
81		;;
82
83		-m)
84		chmodcmd="$chmodprog $2"
85		shift
86		shift
87		continue
88		;;
89
90		-o)
91		chowncmd="$chownprog $2"
92		shift
93		shift
94		continue
95		;;
96
97		-g)
98		chgrpcmd="$chgrpprog $2"
99		shift
100		shift
101		continue
102		;;
103
104		-s)
105		stripcmd="$stripprog"
106		shift
107		continue
108		;;
109
110		-z)
111		instcmd="gzipcp"
112		shift
113		continue
114		;;
115
116		*)
117		if [ x"$src" = x ]; then
118			src="$1"
119		else
120			dst="$1"
121		fi
122		shift
123		continue
124		;;
125	esac
126done
127
128if [ x"$src" = x ]; then
129	echo "install-sh: No input file specified"
130	exit 1
131fi
132
133if [ x"$dir_arg" != x ]; then
134	dst="$src"
135	src=""
136
137	if [ -d "$dst" ]; then
138		instcmd=:
139	else
140		instcmd=$mkdirprog
141	fi
142else
143	# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
144	# might cause directories to be created, which would be especially bad
145	# if $src (and thus $dsttmp) contains '*'.
146	if [ ! -f "$src" -a ! -d "$src" ]; then
147		echo "install: $src does not exist"
148		exit 1
149	fi
150
151	if [ x"$dst" = x ]; then
152		echo "install: No destination specified"
153		exit 1
154	fi
155
156	# If destination is a directory, append the input filename.
157	if [ -d "$dst" ]; then
158		dst="$dst/`basename $src`"
159	fi
160fi
161
162## this sed command emulates the dirname command
163dstdir="`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`"
164
165# Make sure that the destination directory exists.
166# This part is taken from Noah Friedman's mkinstalldirs script
167
168# Skip lots of stat calls in the usual case.
169if [ ! -d "$dstdir" ]; then
170	defaultIFS='
171	'
172	IFS="${IFS-${defaultIFS}}"
173
174	oIFS="${IFS}"
175	# Some sh's can't handle IFS=/ for some reason.
176	IFS='%'
177	set - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
178	IFS="${oIFS}"
179
180	pathcomp=''
181
182	while [ $# -ne 0 ] ; do
183		pathcomp="${pathcomp}${1}"
184		shift
185
186		if [ ! -d "${pathcomp}" ]; then $doit $mkdirprog "${pathcomp}"; fi
187
188		pathcomp="${pathcomp}/"
189	done
190fi
191
192if [ x"$dir_arg" != x ]; then
193	# Make a directory...
194	$doit $instcmd $dst || exit 1
195
196	# Allow chown/chgrp to fail, but log a warning
197	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst || echo "warning: Unable to change owner of $dst!"; fi
198	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst || echo "warning: Unable to change group of $dst!"; fi
199	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst || exit 1; fi
200else
201	# Install a file...
202	dstfile="`basename $dst`"
203
204	# Check the destination file - for libraries just use the "-x" option
205	# to strip...
206	case "$dstfile" in
207		*.a | *.dylib | *.sl | *.sl.* | *.so | *.so.*)
208			stripopt="-x"
209			;;
210		*)
211			stripopt=""
212			;;
213	esac
214
215	# Make a temp file name in the proper directory.
216	dsttmp="$dstdir/#inst.$$#"
217
218	# Move or copy the file name to the temp name
219	$doit $instcmd $src $dsttmp || exit 1
220
221	# Update permissions and strip as needed, then move to the final name.
222	# If the chmod, strip, rm, or mv commands fail, remove the installed
223	# file...
224	if [ x"$stripcmd" != x ]; then $doit $stripcmd $stripopt "$dsttmp" || echo "warning: Unable to strip $dst!"; fi
225	if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp" || echo "warning: Unable to change owner of $dst!"; fi
226	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp" || echo "warning: Unable to change group of $dst!"; fi
227
228	trap "rm -f ${dsttmp}" 0 &&
229	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; fi &&
230	$doit $rmcmd -f "$dstdir/$dstfile" &&
231	$doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
232fi
233
234exit 0
235