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