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