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