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