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