xref: /minix3/crypto/external/bsd/netpgp/dist/buildaux/install-sh (revision ebfedea0ce5bbe81e252ddf32d732e40fb633fae)
1*ebfedea0SLionel Sambuc#!/bin/sh
2*ebfedea0SLionel Sambuc#
3*ebfedea0SLionel Sambuc# $NetBSD: install-sh,v 1.1 2009/05/26 04:27:29 lukem Exp $
4*ebfedea0SLionel Sambuc# This script now also installs multiple files, but might choke on installing
5*ebfedea0SLionel Sambuc# multiple files with spaces in the file names.
6*ebfedea0SLionel Sambuc#
7*ebfedea0SLionel Sambuc# install - install a program, script, or datafile
8*ebfedea0SLionel Sambuc# This comes from X11R5 (mit/util/scripts/install.sh).
9*ebfedea0SLionel Sambuc#
10*ebfedea0SLionel Sambuc# Copyright 1991 by the Massachusetts Institute of Technology
11*ebfedea0SLionel Sambuc#
12*ebfedea0SLionel Sambuc# Permission to use, copy, modify, distribute, and sell this software and its
13*ebfedea0SLionel Sambuc# documentation for any purpose is hereby granted without fee, provided that
14*ebfedea0SLionel Sambuc# the above copyright notice appear in all copies and that both that
15*ebfedea0SLionel Sambuc# copyright notice and this permission notice appear in supporting
16*ebfedea0SLionel Sambuc# documentation, and that the name of M.I.T. not be used in advertising or
17*ebfedea0SLionel Sambuc# publicity pertaining to distribution of the software without specific,
18*ebfedea0SLionel Sambuc# written prior permission.  M.I.T. makes no representations about the
19*ebfedea0SLionel Sambuc# suitability of this software for any purpose.  It is provided "as is"
20*ebfedea0SLionel Sambuc# without express or implied warranty.
21*ebfedea0SLionel Sambuc#
22*ebfedea0SLionel Sambuc# Calling this script install-sh is preferred over install.sh, to prevent
23*ebfedea0SLionel Sambuc# `make' implicit rules from creating a file called install from it
24*ebfedea0SLionel Sambuc# when there is no Makefile.
25*ebfedea0SLionel Sambuc#
26*ebfedea0SLionel Sambuc# This script is compatible with the BSD install script, but was written
27*ebfedea0SLionel Sambuc# from scratch.
28*ebfedea0SLionel Sambuc
29*ebfedea0SLionel Sambuc# set DOITPROG to echo to test this script
30*ebfedea0SLionel Sambuc
31*ebfedea0SLionel Sambuc# Don't use :- since 4.3BSD and earlier shells don't like it.
32*ebfedea0SLionel Sambucdoit="${DOITPROG-}"
33*ebfedea0SLionel Sambuc
34*ebfedea0SLionel Sambuc
35*ebfedea0SLionel Sambuc# put in absolute paths if you don't have them in your path; or use env. vars.
36*ebfedea0SLionel Sambuc
37*ebfedea0SLionel Sambucawkprog="${AWKPROG-awk}"
38*ebfedea0SLionel Sambucmvprog="${MVPROG-mv}"
39*ebfedea0SLionel Sambuccpprog="${CPPROG-cp}"
40*ebfedea0SLionel Sambucchmodprog="${CHMODPROG-chmod}"
41*ebfedea0SLionel Sambucchownprog="${CHOWNPROG-chown}"
42*ebfedea0SLionel Sambucchgrpprog="${CHGRPPROG-chgrp}"
43*ebfedea0SLionel Sambucstripprog="${STRIPPROG-strip}"
44*ebfedea0SLionel Sambucrmprog="${RMPROG-rm}"
45*ebfedea0SLionel Sambucmkdirprog="${MKDIRPROG-mkdir}"
46*ebfedea0SLionel Sambuc
47*ebfedea0SLionel Sambucinstcmd="$mvprog"
48*ebfedea0SLionel Sambucpathcompchmodcmd="$chmodprog 755"
49*ebfedea0SLionel Sambucchmodcmd="$chmodprog 755"
50*ebfedea0SLionel Sambucchowncmd=""
51*ebfedea0SLionel Sambucchgrpcmd=""
52*ebfedea0SLionel Sambucstripcmd=""
53*ebfedea0SLionel Sambucstripflags=""
54*ebfedea0SLionel Sambucrmcmd="$rmprog -f"
55*ebfedea0SLionel Sambucmvcmd="$mvprog"
56*ebfedea0SLionel Sambucsrc=""
57*ebfedea0SLionel Sambucmsrc=""
58*ebfedea0SLionel Sambucdst=""
59*ebfedea0SLionel Sambucdir_arg=""
60*ebfedea0SLionel Sambucsuffix=""
61*ebfedea0SLionel Sambucsuffixfmt=""
62*ebfedea0SLionel Sambuc
63*ebfedea0SLionel Sambucwhile [ x"$1" != x ]; do
64*ebfedea0SLionel Sambuc    case $1 in
65*ebfedea0SLionel Sambuc	-b) suffix=".old"
66*ebfedea0SLionel Sambuc	    shift
67*ebfedea0SLionel Sambuc	    continue;;
68*ebfedea0SLionel Sambuc
69*ebfedea0SLionel Sambuc	-B) suffixfmt="$2"
70*ebfedea0SLionel Sambuc	    shift
71*ebfedea0SLionel Sambuc	    shift
72*ebfedea0SLionel Sambuc	    continue;;
73*ebfedea0SLionel Sambuc
74*ebfedea0SLionel Sambuc	-c) instcmd="$cpprog"
75*ebfedea0SLionel Sambuc	    shift
76*ebfedea0SLionel Sambuc	    continue;;
77*ebfedea0SLionel Sambuc
78*ebfedea0SLionel Sambuc	-d) dir_arg=true
79*ebfedea0SLionel Sambuc	    shift
80*ebfedea0SLionel Sambuc	    continue;;
81*ebfedea0SLionel Sambuc
82*ebfedea0SLionel Sambuc	-m) chmodcmd="$chmodprog $2"
83*ebfedea0SLionel Sambuc	    shift
84*ebfedea0SLionel Sambuc	    shift
85*ebfedea0SLionel Sambuc	    continue;;
86*ebfedea0SLionel Sambuc
87*ebfedea0SLionel Sambuc	-o) chowncmd="$chownprog $2"
88*ebfedea0SLionel Sambuc	    shift
89*ebfedea0SLionel Sambuc	    shift
90*ebfedea0SLionel Sambuc	    continue;;
91*ebfedea0SLionel Sambuc
92*ebfedea0SLionel Sambuc	-g) chgrpcmd="$chgrpprog $2"
93*ebfedea0SLionel Sambuc	    shift
94*ebfedea0SLionel Sambuc	    shift
95*ebfedea0SLionel Sambuc	    continue;;
96*ebfedea0SLionel Sambuc
97*ebfedea0SLionel Sambuc	-s) stripcmd="$stripprog"
98*ebfedea0SLionel Sambuc	    shift
99*ebfedea0SLionel Sambuc	    continue;;
100*ebfedea0SLionel Sambuc
101*ebfedea0SLionel Sambuc	-S) stripcmd="$stripprog"
102*ebfedea0SLionel Sambuc	    stripflags="-S $2 $stripflags"
103*ebfedea0SLionel Sambuc	    shift
104*ebfedea0SLionel Sambuc	    shift
105*ebfedea0SLionel Sambuc	    continue;;
106*ebfedea0SLionel Sambuc
107*ebfedea0SLionel Sambuc	*)  if [ x"$msrc" = x ]
108*ebfedea0SLionel Sambuc	    then
109*ebfedea0SLionel Sambuc		msrc="$dst"
110*ebfedea0SLionel Sambuc	    else
111*ebfedea0SLionel Sambuc		msrc="$msrc $dst"
112*ebfedea0SLionel Sambuc	    fi
113*ebfedea0SLionel Sambuc	    src="$dst"
114*ebfedea0SLionel Sambuc	    dst="$1"
115*ebfedea0SLionel Sambuc	    shift
116*ebfedea0SLionel Sambuc	    continue;;
117*ebfedea0SLionel Sambuc    esac
118*ebfedea0SLionel Sambucdone
119*ebfedea0SLionel Sambuc
120*ebfedea0SLionel Sambucif [ x"$dir_arg" = x ]
121*ebfedea0SLionel Sambucthen
122*ebfedea0SLionel Sambuc	dstisfile=""
123*ebfedea0SLionel Sambuc	if [ ! -d "$dst" ]
124*ebfedea0SLionel Sambuc	then
125*ebfedea0SLionel Sambuc		if [ x"$msrc" = x"$src" ]
126*ebfedea0SLionel Sambuc		then
127*ebfedea0SLionel Sambuc			dstisfile=true
128*ebfedea0SLionel Sambuc		else
129*ebfedea0SLionel Sambuc			echo "install: destination is not a directory"
130*ebfedea0SLionel Sambuc			exit 1
131*ebfedea0SLionel Sambuc		fi
132*ebfedea0SLionel Sambuc	fi
133*ebfedea0SLionel Sambucelse
134*ebfedea0SLionel Sambuc	msrc="$msrc $dst"
135*ebfedea0SLionel Sambucfi
136*ebfedea0SLionel Sambuc
137*ebfedea0SLionel Sambucif [ x"$msrc" = x ]
138*ebfedea0SLionel Sambucthen
139*ebfedea0SLionel Sambuc	echo "install: no destination specified"
140*ebfedea0SLionel Sambuc	exit 1
141*ebfedea0SLionel Sambucfi
142*ebfedea0SLionel Sambuc
143*ebfedea0SLionel Sambucfor srcarg in $msrc; do
144*ebfedea0SLionel Sambuc
145*ebfedea0SLionel Sambucif [ x"$dir_arg" != x ]; then
146*ebfedea0SLionel Sambuc
147*ebfedea0SLionel Sambuc	dstarg="$srcarg"
148*ebfedea0SLionel Sambucelse
149*ebfedea0SLionel Sambuc	dstarg="$dst"
150*ebfedea0SLionel Sambuc
151*ebfedea0SLionel Sambuc# Waiting for this to be detected by the "$instcmd $srcarg $dsttmp" command
152*ebfedea0SLionel Sambuc# might cause directories to be created, which would be especially bad
153*ebfedea0SLionel Sambuc# if $src (and thus $dsttmp) contains '*'.
154*ebfedea0SLionel Sambuc
155*ebfedea0SLionel Sambuc	if [ -f "$srcarg" ]
156*ebfedea0SLionel Sambuc	then
157*ebfedea0SLionel Sambuc		doinst="$instcmd"
158*ebfedea0SLionel Sambuc	elif [ -d "$srcarg" ]
159*ebfedea0SLionel Sambuc	then
160*ebfedea0SLionel Sambuc		echo "install: $srcarg: not a regular file"
161*ebfedea0SLionel Sambuc		exit 1
162*ebfedea0SLionel Sambuc	elif [ "$srcarg" = "/dev/null" ]
163*ebfedea0SLionel Sambuc	then
164*ebfedea0SLionel Sambuc		doinst="$cpprog"
165*ebfedea0SLionel Sambuc	else
166*ebfedea0SLionel Sambuc		echo "install:  $srcarg does not exist"
167*ebfedea0SLionel Sambuc		exit 1
168*ebfedea0SLionel Sambuc	fi
169*ebfedea0SLionel Sambuc
170*ebfedea0SLionel Sambuc# If destination is a directory, append the input filename; if your system
171*ebfedea0SLionel Sambuc# does not like double slashes in filenames, you may need to add some logic
172*ebfedea0SLionel Sambuc
173*ebfedea0SLionel Sambuc	if [ -d "$dstarg" ]
174*ebfedea0SLionel Sambuc	then
175*ebfedea0SLionel Sambuc		dstarg="$dstarg"/`basename "$srcarg"`
176*ebfedea0SLionel Sambuc	fi
177*ebfedea0SLionel Sambucfi
178*ebfedea0SLionel Sambuc
179*ebfedea0SLionel Sambuc## this sed command emulates the dirname command
180*ebfedea0SLionel Sambucdstdir=`echo "$dstarg" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
181*ebfedea0SLionel Sambuc
182*ebfedea0SLionel Sambuc# Make sure that the destination directory exists.
183*ebfedea0SLionel Sambuc#  this part is taken from Noah Friedman's mkinstalldirs script
184*ebfedea0SLionel Sambuc
185*ebfedea0SLionel Sambuc# Skip lots of stat calls in the usual case.
186*ebfedea0SLionel Sambucif [ ! -d "$dstdir" ]; then
187*ebfedea0SLionel SambucdefaultIFS='
188*ebfedea0SLionel Sambuc'
189*ebfedea0SLionel SambucIFS="${IFS-${defaultIFS}}"
190*ebfedea0SLionel Sambuc
191*ebfedea0SLionel SambucoIFS="${IFS}"
192*ebfedea0SLionel Sambuc# Some sh's can't handle IFS=/ for some reason.
193*ebfedea0SLionel SambucIFS='%'
194*ebfedea0SLionel Sambucset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
195*ebfedea0SLionel SambucIFS="${oIFS}"
196*ebfedea0SLionel Sambuc
197*ebfedea0SLionel Sambucpathcomp=''
198*ebfedea0SLionel Sambuc
199*ebfedea0SLionel Sambucwhile [ $# -ne 0 ] ; do
200*ebfedea0SLionel Sambuc	pathcomp="${pathcomp}${1}"
201*ebfedea0SLionel Sambuc	shift
202*ebfedea0SLionel Sambuc
203*ebfedea0SLionel Sambuc	if [ ! -d "${pathcomp}" ] ;
204*ebfedea0SLionel Sambuc        then
205*ebfedea0SLionel Sambuc		$doit $mkdirprog "${pathcomp}"
206*ebfedea0SLionel Sambuc        	if [ x"$chowncmd" != x ]; then $doit $chowncmd "${pathcomp}"; else true ; fi &&
207*ebfedea0SLionel Sambuc        	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "${pathcomp}"; else true ; fi &&
208*ebfedea0SLionel Sambuc        	if [ x"$pathcompchmodcmd" != x ]; then $doit $pathcompchmodcmd "${pathcomp}"; else true ; fi
209*ebfedea0SLionel Sambuc
210*ebfedea0SLionel Sambuc	else
211*ebfedea0SLionel Sambuc		true
212*ebfedea0SLionel Sambuc	fi
213*ebfedea0SLionel Sambuc
214*ebfedea0SLionel Sambuc	pathcomp="${pathcomp}/"
215*ebfedea0SLionel Sambucdone
216*ebfedea0SLionel Sambucfi
217*ebfedea0SLionel Sambuc
218*ebfedea0SLionel Sambuc	if [ x"$dir_arg" != x ]
219*ebfedea0SLionel Sambuc	then
220*ebfedea0SLionel Sambuc		if [ -d "$dstarg" ]; then
221*ebfedea0SLionel Sambuc			true
222*ebfedea0SLionel Sambuc		else
223*ebfedea0SLionel Sambuc			$doit $mkdirprog "$dstarg" &&
224*ebfedea0SLionel Sambuc
225*ebfedea0SLionel Sambuc			if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dstarg"; else true ; fi &&
226*ebfedea0SLionel Sambuc			if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dstarg"; else true ; fi &&
227*ebfedea0SLionel Sambuc			if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dstarg"; else true ; fi
228*ebfedea0SLionel Sambuc		fi
229*ebfedea0SLionel Sambuc	else
230*ebfedea0SLionel Sambuc
231*ebfedea0SLionel Sambuc		if [ x"$dstisfile" = x ]
232*ebfedea0SLionel Sambuc		then
233*ebfedea0SLionel Sambuc			file=$srcarg
234*ebfedea0SLionel Sambuc		else
235*ebfedea0SLionel Sambuc			file=$dst
236*ebfedea0SLionel Sambuc		fi
237*ebfedea0SLionel Sambuc
238*ebfedea0SLionel Sambuc		dstfile=`basename "$file"`
239*ebfedea0SLionel Sambuc		dstfinal="$dstdir/$dstfile"
240*ebfedea0SLionel Sambuc
241*ebfedea0SLionel Sambuc# Make a temp file name in the proper directory.
242*ebfedea0SLionel Sambuc
243*ebfedea0SLionel Sambuc		dsttmp=$dstdir/#inst.$$#
244*ebfedea0SLionel Sambuc
245*ebfedea0SLionel Sambuc# Make a backup file name in the proper directory.
246*ebfedea0SLionel Sambuc		case x$suffixfmt in
247*ebfedea0SLionel Sambuc		*%*)	suffix=`echo x |
248*ebfedea0SLionel Sambuc			$awkprog -v bname="$dstfinal" -v fmt="$suffixfmt" '
249*ebfedea0SLionel Sambuc			{ cnt = 0;
250*ebfedea0SLionel Sambuc			  do {
251*ebfedea0SLionel Sambuc				sfx = sprintf(fmt, cnt++);
252*ebfedea0SLionel Sambuc				name = bname sfx;
253*ebfedea0SLionel Sambuc			  } while (system("test -f " name) == 0);
254*ebfedea0SLionel Sambuc			  print sfx; }' -`;;
255*ebfedea0SLionel Sambuc		x)	;;
256*ebfedea0SLionel Sambuc		*)	suffix="$suffixfmt";;
257*ebfedea0SLionel Sambuc		esac
258*ebfedea0SLionel Sambuc		dstbackup="$dstfinal$suffix"
259*ebfedea0SLionel Sambuc
260*ebfedea0SLionel Sambuc# Move or copy the file name to the temp name
261*ebfedea0SLionel Sambuc
262*ebfedea0SLionel Sambuc		$doit $doinst $srcarg "$dsttmp" &&
263*ebfedea0SLionel Sambuc
264*ebfedea0SLionel Sambuc		trap "rm -f ${dsttmp}" 0 &&
265*ebfedea0SLionel Sambuc
266*ebfedea0SLionel Sambuc# and set any options; do chmod last to preserve setuid bits
267*ebfedea0SLionel Sambuc
268*ebfedea0SLionel Sambuc# If any of these fail, we abort the whole thing.  If we want to
269*ebfedea0SLionel Sambuc# ignore errors from any of these, just make sure not to ignore
270*ebfedea0SLionel Sambuc# errors from the above "$doit $instcmd $src $dsttmp" command.
271*ebfedea0SLionel Sambuc
272*ebfedea0SLionel Sambuc		if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp"; else true;fi &&
273*ebfedea0SLionel Sambuc		if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp"; else true;fi &&
274*ebfedea0SLionel Sambuc		if [ x"$stripcmd" != x ]; then $doit $stripcmd $stripflags "$dsttmp"; else true;fi &&
275*ebfedea0SLionel Sambuc		if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; else true;fi &&
276*ebfedea0SLionel Sambuc
277*ebfedea0SLionel Sambuc# Now rename the file to the real destination.
278*ebfedea0SLionel Sambuc
279*ebfedea0SLionel Sambuc		if [ x"$suffix" != x ] && [ -f "$dstfinal" ]
280*ebfedea0SLionel Sambuc		then
281*ebfedea0SLionel Sambuc			$doit $mvcmd "$dstfinal" "$dstbackup"
282*ebfedea0SLionel Sambuc		else
283*ebfedea0SLionel Sambuc			$doit $rmcmd -f "$dstfinal"
284*ebfedea0SLionel Sambuc		fi &&
285*ebfedea0SLionel Sambuc		$doit $mvcmd "$dsttmp" "$dstfinal"
286*ebfedea0SLionel Sambuc	fi
287*ebfedea0SLionel Sambuc
288*ebfedea0SLionel Sambucdone &&
289*ebfedea0SLionel Sambuc
290*ebfedea0SLionel Sambuc
291*ebfedea0SLionel Sambucexit 0
292