xref: /aosp_15_r20/external/fec/install-sh (revision 638691a093b4f9473cd6ee8f3e0139deef159a86)
1*638691a0SAndroid Build Coastguard Worker#!/bin/sh
2*638691a0SAndroid Build Coastguard Worker#
3*638691a0SAndroid Build Coastguard Worker# install - install a program, script, or datafile
4*638691a0SAndroid Build Coastguard Worker# This comes from X11R5 (mit/util/scripts/install.sh).
5*638691a0SAndroid Build Coastguard Worker#
6*638691a0SAndroid Build Coastguard Worker# Copyright 1991 by the Massachusetts Institute of Technology
7*638691a0SAndroid Build Coastguard Worker#
8*638691a0SAndroid Build Coastguard Worker# Permission to use, copy, modify, distribute, and sell this software and its
9*638691a0SAndroid Build Coastguard Worker# documentation for any purpose is hereby granted without fee, provided that
10*638691a0SAndroid Build Coastguard Worker# the above copyright notice appear in all copies and that both that
11*638691a0SAndroid Build Coastguard Worker# copyright notice and this permission notice appear in supporting
12*638691a0SAndroid Build Coastguard Worker# documentation, and that the name of M.I.T. not be used in advertising or
13*638691a0SAndroid Build Coastguard Worker# publicity pertaining to distribution of the software without specific,
14*638691a0SAndroid Build Coastguard Worker# written prior permission.  M.I.T. makes no representations about the
15*638691a0SAndroid Build Coastguard Worker# suitability of this software for any purpose.  It is provided "as is"
16*638691a0SAndroid Build Coastguard Worker# without express or implied warranty.
17*638691a0SAndroid Build Coastguard Worker#
18*638691a0SAndroid Build Coastguard Worker# Calling this script install-sh is preferred over install.sh, to prevent
19*638691a0SAndroid Build Coastguard Worker# `make' implicit rules from creating a file called install from it
20*638691a0SAndroid Build Coastguard Worker# when there is no Makefile.
21*638691a0SAndroid Build Coastguard Worker#
22*638691a0SAndroid Build Coastguard Worker# This script is compatible with the BSD install script, but was written
23*638691a0SAndroid Build Coastguard Worker# from scratch.  It can only install one file at a time, a restriction
24*638691a0SAndroid Build Coastguard Worker# shared with many OS's install programs.
25*638691a0SAndroid Build Coastguard Worker
26*638691a0SAndroid Build Coastguard Worker
27*638691a0SAndroid Build Coastguard Worker# set DOITPROG to echo to test this script
28*638691a0SAndroid Build Coastguard Worker
29*638691a0SAndroid Build Coastguard Worker# Don't use :- since 4.3BSD and earlier shells don't like it.
30*638691a0SAndroid Build Coastguard Workerdoit="${DOITPROG-}"
31*638691a0SAndroid Build Coastguard Worker
32*638691a0SAndroid Build Coastguard Worker
33*638691a0SAndroid Build Coastguard Worker# put in absolute paths if you don't have them in your path; or use env. vars.
34*638691a0SAndroid Build Coastguard Worker
35*638691a0SAndroid Build Coastguard Workermvprog="${MVPROG-mv}"
36*638691a0SAndroid Build Coastguard Workercpprog="${CPPROG-cp}"
37*638691a0SAndroid Build Coastguard Workerchmodprog="${CHMODPROG-chmod}"
38*638691a0SAndroid Build Coastguard Workerchownprog="${CHOWNPROG-chown}"
39*638691a0SAndroid Build Coastguard Workerchgrpprog="${CHGRPPROG-chgrp}"
40*638691a0SAndroid Build Coastguard Workerstripprog="${STRIPPROG-strip}"
41*638691a0SAndroid Build Coastguard Workerrmprog="${RMPROG-rm}"
42*638691a0SAndroid Build Coastguard Workermkdirprog="${MKDIRPROG-mkdir}"
43*638691a0SAndroid Build Coastguard Worker
44*638691a0SAndroid Build Coastguard Workertransformbasename=""
45*638691a0SAndroid Build Coastguard Workertransform_arg=""
46*638691a0SAndroid Build Coastguard Workerinstcmd="$mvprog"
47*638691a0SAndroid Build Coastguard Workerchmodcmd="$chmodprog 0755"
48*638691a0SAndroid Build Coastguard Workerchowncmd=""
49*638691a0SAndroid Build Coastguard Workerchgrpcmd=""
50*638691a0SAndroid Build Coastguard Workerstripcmd=""
51*638691a0SAndroid Build Coastguard Workerrmcmd="$rmprog -f"
52*638691a0SAndroid Build Coastguard Workermvcmd="$mvprog"
53*638691a0SAndroid Build Coastguard Workersrc=""
54*638691a0SAndroid Build Coastguard Workerdst=""
55*638691a0SAndroid Build Coastguard Workerdir_arg=""
56*638691a0SAndroid Build Coastguard Worker
57*638691a0SAndroid Build Coastguard Workerwhile [ x"$1" != x ]; do
58*638691a0SAndroid Build Coastguard Worker    case $1 in
59*638691a0SAndroid Build Coastguard Worker	-c) instcmd="$cpprog"
60*638691a0SAndroid Build Coastguard Worker	    shift
61*638691a0SAndroid Build Coastguard Worker	    continue;;
62*638691a0SAndroid Build Coastguard Worker
63*638691a0SAndroid Build Coastguard Worker	-d) dir_arg=true
64*638691a0SAndroid Build Coastguard Worker	    shift
65*638691a0SAndroid Build Coastguard Worker	    continue;;
66*638691a0SAndroid Build Coastguard Worker
67*638691a0SAndroid Build Coastguard Worker	-m) chmodcmd="$chmodprog $2"
68*638691a0SAndroid Build Coastguard Worker	    shift
69*638691a0SAndroid Build Coastguard Worker	    shift
70*638691a0SAndroid Build Coastguard Worker	    continue;;
71*638691a0SAndroid Build Coastguard Worker
72*638691a0SAndroid Build Coastguard Worker	-o) chowncmd="$chownprog $2"
73*638691a0SAndroid Build Coastguard Worker	    shift
74*638691a0SAndroid Build Coastguard Worker	    shift
75*638691a0SAndroid Build Coastguard Worker	    continue;;
76*638691a0SAndroid Build Coastguard Worker
77*638691a0SAndroid Build Coastguard Worker	-g) chgrpcmd="$chgrpprog $2"
78*638691a0SAndroid Build Coastguard Worker	    shift
79*638691a0SAndroid Build Coastguard Worker	    shift
80*638691a0SAndroid Build Coastguard Worker	    continue;;
81*638691a0SAndroid Build Coastguard Worker
82*638691a0SAndroid Build Coastguard Worker	-s) stripcmd="$stripprog"
83*638691a0SAndroid Build Coastguard Worker	    shift
84*638691a0SAndroid Build Coastguard Worker	    continue;;
85*638691a0SAndroid Build Coastguard Worker
86*638691a0SAndroid Build Coastguard Worker	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
87*638691a0SAndroid Build Coastguard Worker	    shift
88*638691a0SAndroid Build Coastguard Worker	    continue;;
89*638691a0SAndroid Build Coastguard Worker
90*638691a0SAndroid Build Coastguard Worker	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
91*638691a0SAndroid Build Coastguard Worker	    shift
92*638691a0SAndroid Build Coastguard Worker	    continue;;
93*638691a0SAndroid Build Coastguard Worker
94*638691a0SAndroid Build Coastguard Worker	*)  if [ x"$src" = x ]
95*638691a0SAndroid Build Coastguard Worker	    then
96*638691a0SAndroid Build Coastguard Worker		src=$1
97*638691a0SAndroid Build Coastguard Worker	    else
98*638691a0SAndroid Build Coastguard Worker		# this colon is to work around a 386BSD /bin/sh bug
99*638691a0SAndroid Build Coastguard Worker		:
100*638691a0SAndroid Build Coastguard Worker		dst=$1
101*638691a0SAndroid Build Coastguard Worker	    fi
102*638691a0SAndroid Build Coastguard Worker	    shift
103*638691a0SAndroid Build Coastguard Worker	    continue;;
104*638691a0SAndroid Build Coastguard Worker    esac
105*638691a0SAndroid Build Coastguard Workerdone
106*638691a0SAndroid Build Coastguard Worker
107*638691a0SAndroid Build Coastguard Workerif [ x"$src" = x ]
108*638691a0SAndroid Build Coastguard Workerthen
109*638691a0SAndroid Build Coastguard Worker	echo "install:	no input file specified"
110*638691a0SAndroid Build Coastguard Worker	exit 1
111*638691a0SAndroid Build Coastguard Workerelse
112*638691a0SAndroid Build Coastguard Worker	true
113*638691a0SAndroid Build Coastguard Workerfi
114*638691a0SAndroid Build Coastguard Worker
115*638691a0SAndroid Build Coastguard Workerif [ x"$dir_arg" != x ]; then
116*638691a0SAndroid Build Coastguard Worker	dst=$src
117*638691a0SAndroid Build Coastguard Worker	src=""
118*638691a0SAndroid Build Coastguard Worker
119*638691a0SAndroid Build Coastguard Worker	if [ -d $dst ]; then
120*638691a0SAndroid Build Coastguard Worker		instcmd=:
121*638691a0SAndroid Build Coastguard Worker		chmodcmd=""
122*638691a0SAndroid Build Coastguard Worker	else
123*638691a0SAndroid Build Coastguard Worker		instcmd=mkdir
124*638691a0SAndroid Build Coastguard Worker	fi
125*638691a0SAndroid Build Coastguard Workerelse
126*638691a0SAndroid Build Coastguard Worker
127*638691a0SAndroid Build Coastguard Worker# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
128*638691a0SAndroid Build Coastguard Worker# might cause directories to be created, which would be especially bad
129*638691a0SAndroid Build Coastguard Worker# if $src (and thus $dsttmp) contains '*'.
130*638691a0SAndroid Build Coastguard Worker
131*638691a0SAndroid Build Coastguard Worker	if [ -f $src -o -d $src ]
132*638691a0SAndroid Build Coastguard Worker	then
133*638691a0SAndroid Build Coastguard Worker		true
134*638691a0SAndroid Build Coastguard Worker	else
135*638691a0SAndroid Build Coastguard Worker		echo "install:  $src does not exist"
136*638691a0SAndroid Build Coastguard Worker		exit 1
137*638691a0SAndroid Build Coastguard Worker	fi
138*638691a0SAndroid Build Coastguard Worker
139*638691a0SAndroid Build Coastguard Worker	if [ x"$dst" = x ]
140*638691a0SAndroid Build Coastguard Worker	then
141*638691a0SAndroid Build Coastguard Worker		echo "install:	no destination specified"
142*638691a0SAndroid Build Coastguard Worker		exit 1
143*638691a0SAndroid Build Coastguard Worker	else
144*638691a0SAndroid Build Coastguard Worker		true
145*638691a0SAndroid Build Coastguard Worker	fi
146*638691a0SAndroid Build Coastguard Worker
147*638691a0SAndroid Build Coastguard Worker# If destination is a directory, append the input filename; if your system
148*638691a0SAndroid Build Coastguard Worker# does not like double slashes in filenames, you may need to add some logic
149*638691a0SAndroid Build Coastguard Worker
150*638691a0SAndroid Build Coastguard Worker	if [ -d $dst ]
151*638691a0SAndroid Build Coastguard Worker	then
152*638691a0SAndroid Build Coastguard Worker		dst="$dst"/`basename $src`
153*638691a0SAndroid Build Coastguard Worker	else
154*638691a0SAndroid Build Coastguard Worker		true
155*638691a0SAndroid Build Coastguard Worker	fi
156*638691a0SAndroid Build Coastguard Workerfi
157*638691a0SAndroid Build Coastguard Worker
158*638691a0SAndroid Build Coastguard Worker## this sed command emulates the dirname command
159*638691a0SAndroid Build Coastguard Workerdstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
160*638691a0SAndroid Build Coastguard Worker
161*638691a0SAndroid Build Coastguard Worker# Make sure that the destination directory exists.
162*638691a0SAndroid Build Coastguard Worker#  this part is taken from Noah Friedman's mkinstalldirs script
163*638691a0SAndroid Build Coastguard Worker
164*638691a0SAndroid Build Coastguard Worker# Skip lots of stat calls in the usual case.
165*638691a0SAndroid Build Coastguard Workerif [ ! -d "$dstdir" ]; then
166*638691a0SAndroid Build Coastguard WorkerdefaultIFS='
167*638691a0SAndroid Build Coastguard Worker'
168*638691a0SAndroid Build Coastguard WorkerIFS="${IFS-${defaultIFS}}"
169*638691a0SAndroid Build Coastguard Worker
170*638691a0SAndroid Build Coastguard WorkeroIFS="${IFS}"
171*638691a0SAndroid Build Coastguard Worker# Some sh's can't handle IFS=/ for some reason.
172*638691a0SAndroid Build Coastguard WorkerIFS='%'
173*638691a0SAndroid Build Coastguard Workerset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
174*638691a0SAndroid Build Coastguard WorkerIFS="${oIFS}"
175*638691a0SAndroid Build Coastguard Worker
176*638691a0SAndroid Build Coastguard Workerpathcomp=''
177*638691a0SAndroid Build Coastguard Worker
178*638691a0SAndroid Build Coastguard Workerwhile [ $# -ne 0 ] ; do
179*638691a0SAndroid Build Coastguard Worker	pathcomp="${pathcomp}${1}"
180*638691a0SAndroid Build Coastguard Worker	shift
181*638691a0SAndroid Build Coastguard Worker
182*638691a0SAndroid Build Coastguard Worker	if [ ! -d "${pathcomp}" ] ;
183*638691a0SAndroid Build Coastguard Worker        then
184*638691a0SAndroid Build Coastguard Worker		$mkdirprog "${pathcomp}"
185*638691a0SAndroid Build Coastguard Worker	else
186*638691a0SAndroid Build Coastguard Worker		true
187*638691a0SAndroid Build Coastguard Worker	fi
188*638691a0SAndroid Build Coastguard Worker
189*638691a0SAndroid Build Coastguard Worker	pathcomp="${pathcomp}/"
190*638691a0SAndroid Build Coastguard Workerdone
191*638691a0SAndroid Build Coastguard Workerfi
192*638691a0SAndroid Build Coastguard Worker
193*638691a0SAndroid Build Coastguard Workerif [ x"$dir_arg" != x ]
194*638691a0SAndroid Build Coastguard Workerthen
195*638691a0SAndroid Build Coastguard Worker	$doit $instcmd $dst &&
196*638691a0SAndroid Build Coastguard Worker
197*638691a0SAndroid Build Coastguard Worker	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
198*638691a0SAndroid Build Coastguard Worker	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
199*638691a0SAndroid Build Coastguard Worker	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
200*638691a0SAndroid Build Coastguard Worker	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
201*638691a0SAndroid Build Coastguard Workerelse
202*638691a0SAndroid Build Coastguard Worker
203*638691a0SAndroid Build Coastguard Worker# If we're going to rename the final executable, determine the name now.
204*638691a0SAndroid Build Coastguard Worker
205*638691a0SAndroid Build Coastguard Worker	if [ x"$transformarg" = x ]
206*638691a0SAndroid Build Coastguard Worker	then
207*638691a0SAndroid Build Coastguard Worker		dstfile=`basename $dst`
208*638691a0SAndroid Build Coastguard Worker	else
209*638691a0SAndroid Build Coastguard Worker		dstfile=`basename $dst $transformbasename |
210*638691a0SAndroid Build Coastguard Worker			sed $transformarg`$transformbasename
211*638691a0SAndroid Build Coastguard Worker	fi
212*638691a0SAndroid Build Coastguard Worker
213*638691a0SAndroid Build Coastguard Worker# don't allow the sed command to completely eliminate the filename
214*638691a0SAndroid Build Coastguard Worker
215*638691a0SAndroid Build Coastguard Worker	if [ x"$dstfile" = x ]
216*638691a0SAndroid Build Coastguard Worker	then
217*638691a0SAndroid Build Coastguard Worker		dstfile=`basename $dst`
218*638691a0SAndroid Build Coastguard Worker	else
219*638691a0SAndroid Build Coastguard Worker		true
220*638691a0SAndroid Build Coastguard Worker	fi
221*638691a0SAndroid Build Coastguard Worker
222*638691a0SAndroid Build Coastguard Worker# Make a temp file name in the proper directory.
223*638691a0SAndroid Build Coastguard Worker
224*638691a0SAndroid Build Coastguard Worker	dsttmp=$dstdir/#inst.$$#
225*638691a0SAndroid Build Coastguard Worker
226*638691a0SAndroid Build Coastguard Worker# Move or copy the file name to the temp name
227*638691a0SAndroid Build Coastguard Worker
228*638691a0SAndroid Build Coastguard Worker	$doit $instcmd $src $dsttmp &&
229*638691a0SAndroid Build Coastguard Worker
230*638691a0SAndroid Build Coastguard Worker	trap "rm -f ${dsttmp}" 0 &&
231*638691a0SAndroid Build Coastguard Worker
232*638691a0SAndroid Build Coastguard Worker# and set any options; do chmod last to preserve setuid bits
233*638691a0SAndroid Build Coastguard Worker
234*638691a0SAndroid Build Coastguard Worker# If any of these fail, we abort the whole thing.  If we want to
235*638691a0SAndroid Build Coastguard Worker# ignore errors from any of these, just make sure not to ignore
236*638691a0SAndroid Build Coastguard Worker# errors from the above "$doit $instcmd $src $dsttmp" command.
237*638691a0SAndroid Build Coastguard Worker
238*638691a0SAndroid Build Coastguard Worker	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
239*638691a0SAndroid Build Coastguard Worker	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
240*638691a0SAndroid Build Coastguard Worker	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
241*638691a0SAndroid Build Coastguard Worker	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
242*638691a0SAndroid Build Coastguard Worker
243*638691a0SAndroid Build Coastguard Worker# Now rename the file to the real destination.
244*638691a0SAndroid Build Coastguard Worker
245*638691a0SAndroid Build Coastguard Worker	$doit $rmcmd -f $dstdir/$dstfile &&
246*638691a0SAndroid Build Coastguard Worker	$doit $mvcmd $dsttmp $dstdir/$dstfile
247*638691a0SAndroid Build Coastguard Worker
248*638691a0SAndroid Build Coastguard Workerfi &&
249*638691a0SAndroid Build Coastguard Worker
250*638691a0SAndroid Build Coastguard Worker
251*638691a0SAndroid Build Coastguard Workerexit 0
252