xref: /freebsd/crypto/heimdal/install-sh (revision 8373020d34ceb1ac55d8f43333c1ca3680185b39)
1b528cefcSMark Murray#!/bin/sh
2b528cefcSMark Murray#
3b528cefcSMark Murray# install - install a program, script, or datafile
48373020dSJacques Vidrine# This comes from X11R5 (mit/util/scripts/install.sh).
58373020dSJacques Vidrine#
68373020dSJacques Vidrine# Copyright 1991 by the Massachusetts Institute of Technology
78373020dSJacques Vidrine#
88373020dSJacques Vidrine# Permission to use, copy, modify, distribute, and sell this software and its
98373020dSJacques Vidrine# documentation for any purpose is hereby granted without fee, provided that
108373020dSJacques Vidrine# the above copyright notice appear in all copies and that both that
118373020dSJacques Vidrine# copyright notice and this permission notice appear in supporting
128373020dSJacques Vidrine# documentation, and that the name of M.I.T. not be used in advertising or
138373020dSJacques Vidrine# publicity pertaining to distribution of the software without specific,
148373020dSJacques Vidrine# written prior permission.  M.I.T. makes no representations about the
158373020dSJacques Vidrine# suitability of this software for any purpose.  It is provided "as is"
168373020dSJacques Vidrine# without express or implied warranty.
17b528cefcSMark Murray#
18b528cefcSMark Murray# Calling this script install-sh is preferred over install.sh, to prevent
19b528cefcSMark Murray# `make' implicit rules from creating a file called install from it
20b528cefcSMark Murray# when there is no Makefile.
21b528cefcSMark Murray#
22b528cefcSMark Murray# This script is compatible with the BSD install script, but was written
238373020dSJacques Vidrine# from scratch.  It can only install one file at a time, a restriction
248373020dSJacques Vidrine# shared with many OS's install programs.
25b528cefcSMark Murray
26b528cefcSMark Murray
27b528cefcSMark Murray# set DOITPROG to echo to test this script
28b528cefcSMark Murray
29b528cefcSMark Murray# Don't use :- since 4.3BSD and earlier shells don't like it.
30b528cefcSMark Murraydoit="${DOITPROG-}"
31b528cefcSMark Murray
32b528cefcSMark Murray
33b528cefcSMark Murray# put in absolute paths if you don't have them in your path; or use env. vars.
34b528cefcSMark Murray
35b528cefcSMark Murraymvprog="${MVPROG-mv}"
36b528cefcSMark Murraycpprog="${CPPROG-cp}"
37b528cefcSMark Murraychmodprog="${CHMODPROG-chmod}"
38b528cefcSMark Murraychownprog="${CHOWNPROG-chown}"
39b528cefcSMark Murraychgrpprog="${CHGRPPROG-chgrp}"
40b528cefcSMark Murraystripprog="${STRIPPROG-strip}"
41b528cefcSMark Murrayrmprog="${RMPROG-rm}"
42b528cefcSMark Murraymkdirprog="${MKDIRPROG-mkdir}"
43b528cefcSMark Murray
448373020dSJacques Vidrinetransformbasename=""
45b528cefcSMark Murraytransform_arg=""
46b528cefcSMark Murrayinstcmd="$mvprog"
47b528cefcSMark Murraychmodcmd="$chmodprog 0755"
48b528cefcSMark Murraychowncmd=""
49b528cefcSMark Murraychgrpcmd=""
50b528cefcSMark Murraystripcmd=""
51b528cefcSMark Murrayrmcmd="$rmprog -f"
52b528cefcSMark Murraymvcmd="$mvprog"
53b528cefcSMark Murraysrc=""
54b528cefcSMark Murraydst=""
55b528cefcSMark Murraydir_arg=""
56b528cefcSMark Murray
57b528cefcSMark Murraywhile [ x"$1" != x ]; do
58b528cefcSMark Murray    case $1 in
59b528cefcSMark Murray	-c) instcmd="$cpprog"
60b528cefcSMark Murray	    shift
61b528cefcSMark Murray	    continue;;
62b528cefcSMark Murray
63b528cefcSMark Murray	-d) dir_arg=true
64b528cefcSMark Murray	    shift
65b528cefcSMark Murray	    continue;;
66b528cefcSMark Murray
67b528cefcSMark Murray	-m) chmodcmd="$chmodprog $2"
68b528cefcSMark Murray	    shift
69b528cefcSMark Murray	    shift
70b528cefcSMark Murray	    continue;;
71b528cefcSMark Murray
72b528cefcSMark Murray	-o) chowncmd="$chownprog $2"
73b528cefcSMark Murray	    shift
74b528cefcSMark Murray	    shift
75b528cefcSMark Murray	    continue;;
76b528cefcSMark Murray
77b528cefcSMark Murray	-g) chgrpcmd="$chgrpprog $2"
78b528cefcSMark Murray	    shift
79b528cefcSMark Murray	    shift
80b528cefcSMark Murray	    continue;;
81b528cefcSMark Murray
82b528cefcSMark Murray	-s) stripcmd="$stripprog"
83b528cefcSMark Murray	    shift
84b528cefcSMark Murray	    continue;;
85b528cefcSMark Murray
86b528cefcSMark Murray	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
87b528cefcSMark Murray	    shift
88b528cefcSMark Murray	    continue;;
89b528cefcSMark Murray
90b528cefcSMark Murray	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
91b528cefcSMark Murray	    shift
92b528cefcSMark Murray	    continue;;
93b528cefcSMark Murray
94b528cefcSMark Murray	*)  if [ x"$src" = x ]
95b528cefcSMark Murray	    then
96b528cefcSMark Murray		src=$1
97b528cefcSMark Murray	    else
98b528cefcSMark Murray		# this colon is to work around a 386BSD /bin/sh bug
99b528cefcSMark Murray		:
100b528cefcSMark Murray		dst=$1
101b528cefcSMark Murray	    fi
102b528cefcSMark Murray	    shift
103b528cefcSMark Murray	    continue;;
104b528cefcSMark Murray    esac
105b528cefcSMark Murraydone
106b528cefcSMark Murray
107b528cefcSMark Murrayif [ x"$src" = x ]
108b528cefcSMark Murraythen
109b528cefcSMark Murray	echo "install:	no input file specified"
110b528cefcSMark Murray	exit 1
111b528cefcSMark Murrayelse
1128373020dSJacques Vidrine	:
113b528cefcSMark Murrayfi
114b528cefcSMark Murray
115b528cefcSMark Murrayif [ x"$dir_arg" != x ]; then
116b528cefcSMark Murray	dst=$src
117b528cefcSMark Murray	src=""
118b528cefcSMark Murray
119b528cefcSMark Murray	if [ -d $dst ]; then
120b528cefcSMark Murray		instcmd=:
1218373020dSJacques Vidrine		chmodcmd=""
122b528cefcSMark Murray	else
1238373020dSJacques Vidrine		instcmd=$mkdirprog
124b528cefcSMark Murray	fi
125b528cefcSMark Murrayelse
126b528cefcSMark Murray
127b528cefcSMark Murray# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
128b528cefcSMark Murray# might cause directories to be created, which would be especially bad
129b528cefcSMark Murray# if $src (and thus $dsttmp) contains '*'.
130b528cefcSMark Murray
1318373020dSJacques Vidrine	if [ -f "$src" ] || [ -d "$src" ]
132b528cefcSMark Murray	then
1338373020dSJacques Vidrine		:
134b528cefcSMark Murray	else
135b528cefcSMark Murray		echo "install:  $src does not exist"
136b528cefcSMark Murray		exit 1
137b528cefcSMark Murray	fi
138b528cefcSMark Murray
139b528cefcSMark Murray	if [ x"$dst" = x ]
140b528cefcSMark Murray	then
141b528cefcSMark Murray		echo "install:	no destination specified"
142b528cefcSMark Murray		exit 1
143b528cefcSMark Murray	else
1448373020dSJacques Vidrine		:
145b528cefcSMark Murray	fi
146b528cefcSMark Murray
147b528cefcSMark Murray# If destination is a directory, append the input filename; if your system
148b528cefcSMark Murray# does not like double slashes in filenames, you may need to add some logic
149b528cefcSMark Murray
150b528cefcSMark Murray	if [ -d $dst ]
151b528cefcSMark Murray	then
152b528cefcSMark Murray		dst="$dst"/`basename $src`
153b528cefcSMark Murray	else
1548373020dSJacques Vidrine		:
155b528cefcSMark Murray	fi
156b528cefcSMark Murrayfi
157b528cefcSMark Murray
158b528cefcSMark Murray## this sed command emulates the dirname command
159b528cefcSMark Murraydstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
160b528cefcSMark Murray
161b528cefcSMark Murray# Make sure that the destination directory exists.
162b528cefcSMark Murray#  this part is taken from Noah Friedman's mkinstalldirs script
163b528cefcSMark Murray
164b528cefcSMark Murray# Skip lots of stat calls in the usual case.
165b528cefcSMark Murrayif [ ! -d "$dstdir" ]; then
166b528cefcSMark MurraydefaultIFS='
167b528cefcSMark Murray	'
168b528cefcSMark MurrayIFS="${IFS-${defaultIFS}}"
169b528cefcSMark Murray
170b528cefcSMark MurrayoIFS="${IFS}"
171b528cefcSMark Murray# Some sh's can't handle IFS=/ for some reason.
172b528cefcSMark MurrayIFS='%'
173b528cefcSMark Murrayset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
174b528cefcSMark MurrayIFS="${oIFS}"
175b528cefcSMark Murray
176b528cefcSMark Murraypathcomp=''
177b528cefcSMark Murray
178b528cefcSMark Murraywhile [ $# -ne 0 ] ; do
179b528cefcSMark Murray	pathcomp="${pathcomp}${1}"
180b528cefcSMark Murray	shift
181b528cefcSMark Murray
182b528cefcSMark Murray	if [ ! -d "${pathcomp}" ] ;
183b528cefcSMark Murray        then
184b528cefcSMark Murray		$mkdirprog "${pathcomp}"
185b528cefcSMark Murray	else
1868373020dSJacques Vidrine		:
187b528cefcSMark Murray	fi
188b528cefcSMark Murray
189b528cefcSMark Murray	pathcomp="${pathcomp}/"
190b528cefcSMark Murraydone
191b528cefcSMark Murrayfi
192b528cefcSMark Murray
193b528cefcSMark Murrayif [ x"$dir_arg" != x ]
194b528cefcSMark Murraythen
195b528cefcSMark Murray	$doit $instcmd $dst &&
196b528cefcSMark Murray
1978373020dSJacques Vidrine	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else : ; fi &&
1988373020dSJacques Vidrine	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else : ; fi &&
1998373020dSJacques Vidrine	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else : ; fi &&
2008373020dSJacques Vidrine	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else : ; fi
201b528cefcSMark Murrayelse
202b528cefcSMark Murray
203b528cefcSMark Murray# If we're going to rename the final executable, determine the name now.
204b528cefcSMark Murray
205b528cefcSMark Murray	if [ x"$transformarg" = x ]
206b528cefcSMark Murray	then
207b528cefcSMark Murray		dstfile=`basename $dst`
208b528cefcSMark Murray	else
209b528cefcSMark Murray		dstfile=`basename $dst $transformbasename |
210b528cefcSMark Murray			sed $transformarg`$transformbasename
211b528cefcSMark Murray	fi
212b528cefcSMark Murray
213b528cefcSMark Murray# don't allow the sed command to completely eliminate the filename
214b528cefcSMark Murray
215b528cefcSMark Murray	if [ x"$dstfile" = x ]
216b528cefcSMark Murray	then
217b528cefcSMark Murray		dstfile=`basename $dst`
218b528cefcSMark Murray	else
2198373020dSJacques Vidrine		:
220b528cefcSMark Murray	fi
221b528cefcSMark Murray
222b528cefcSMark Murray# Make a temp file name in the proper directory.
223b528cefcSMark Murray
224b528cefcSMark Murray	dsttmp=$dstdir/#inst.$$#
225b528cefcSMark Murray
226b528cefcSMark Murray# Move or copy the file name to the temp name
227b528cefcSMark Murray
228b528cefcSMark Murray	$doit $instcmd $src $dsttmp &&
229b528cefcSMark Murray
230b528cefcSMark Murray	trap "rm -f ${dsttmp}" 0 &&
231b528cefcSMark Murray
232b528cefcSMark Murray# and set any options; do chmod last to preserve setuid bits
233b528cefcSMark Murray
234b528cefcSMark Murray# If any of these fail, we abort the whole thing.  If we want to
235b528cefcSMark Murray# ignore errors from any of these, just make sure not to ignore
236b528cefcSMark Murray# errors from the above "$doit $instcmd $src $dsttmp" command.
237b528cefcSMark Murray
2388373020dSJacques Vidrine	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else :;fi &&
2398373020dSJacques Vidrine	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else :;fi &&
2408373020dSJacques Vidrine	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else :;fi &&
2418373020dSJacques Vidrine	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else :;fi &&
242b528cefcSMark Murray
243b528cefcSMark Murray# Now rename the file to the real destination.
244b528cefcSMark Murray
245b528cefcSMark Murray	$doit $rmcmd -f $dstdir/$dstfile &&
246b528cefcSMark Murray	$doit $mvcmd $dsttmp $dstdir/$dstfile
247b528cefcSMark Murray
248b528cefcSMark Murrayfi &&
249b528cefcSMark Murray
250b528cefcSMark Murray
251b528cefcSMark Murrayexit 0
252