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