install-sh revision 0cc6efb6
10cc6efb6Smrg#!/bin/sh
20cc6efb6Smrg#
30cc6efb6Smrg# install - install a program, script, or datafile
40cc6efb6Smrg#
50cc6efb6Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
60cc6efb6Smrg# later released in X11R6 (xc/config/util/install.sh) with the
70cc6efb6Smrg# following copyright and license.
80cc6efb6Smrg#
90cc6efb6Smrg# Copyright (C) 1994 X Consortium
100cc6efb6Smrg#
110cc6efb6Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
120cc6efb6Smrg# of this software and associated documentation files (the "Software"), to
130cc6efb6Smrg# deal in the Software without restriction, including without limitation the
140cc6efb6Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
150cc6efb6Smrg# sell copies of the Software, and to permit persons to whom the Software is
160cc6efb6Smrg# furnished to do so, subject to the following conditions:
170cc6efb6Smrg#
180cc6efb6Smrg# The above copyright notice and this permission notice shall be included in
190cc6efb6Smrg# all copies or substantial portions of the Software.
200cc6efb6Smrg#
210cc6efb6Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
220cc6efb6Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
230cc6efb6Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
240cc6efb6Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
250cc6efb6Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
260cc6efb6Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
270cc6efb6Smrg#
280cc6efb6Smrg# Except as contained in this notice, the name of the X Consortium shall not
290cc6efb6Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
300cc6efb6Smrg# ings in this Software without prior written authorization from the X Consor-
310cc6efb6Smrg# tium.
320cc6efb6Smrg#
330cc6efb6Smrg#
340cc6efb6Smrg# FSF changes to this file are in the public domain.
350cc6efb6Smrg#
360cc6efb6Smrg# Calling this script install-sh is preferred over install.sh, to prevent
370cc6efb6Smrg# `make' implicit rules from creating a file called install from it
380cc6efb6Smrg# when there is no Makefile.
390cc6efb6Smrg#
400cc6efb6Smrg# This script is compatible with the BSD install script, but was written
410cc6efb6Smrg# from scratch.  It can only install one file at a time, a restriction
420cc6efb6Smrg# shared with many OS's install programs.
430cc6efb6Smrg
440cc6efb6Smrg
450cc6efb6Smrg# set DOITPROG to echo to test this script
460cc6efb6Smrg
470cc6efb6Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
480cc6efb6Smrgdoit="${DOITPROG-}"
490cc6efb6Smrg
500cc6efb6Smrg
510cc6efb6Smrg# put in absolute paths if you don't have them in your path; or use env. vars.
520cc6efb6Smrg
530cc6efb6Smrgmvprog="${MVPROG-mv}"
540cc6efb6Smrgcpprog="${CPPROG-cp}"
550cc6efb6Smrgchmodprog="${CHMODPROG-chmod}"
560cc6efb6Smrgchownprog="${CHOWNPROG-chown}"
570cc6efb6Smrgchgrpprog="${CHGRPPROG-chgrp}"
580cc6efb6Smrgstripprog="${STRIPPROG-strip}"
590cc6efb6Smrgrmprog="${RMPROG-rm}"
600cc6efb6Smrgmkdirprog="${MKDIRPROG-mkdir}"
610cc6efb6Smrg
620cc6efb6Smrgtransformbasename=""
630cc6efb6Smrgtransform_arg=""
640cc6efb6Smrginstcmd="$mvprog"
650cc6efb6Smrgchmodcmd="$chmodprog 0755"
660cc6efb6Smrgchowncmd=""
670cc6efb6Smrgchgrpcmd=""
680cc6efb6Smrgstripcmd=""
690cc6efb6Smrgrmcmd="$rmprog -f"
700cc6efb6Smrgmvcmd="$mvprog"
710cc6efb6Smrgsrc=""
720cc6efb6Smrgdst=""
730cc6efb6Smrgdir_arg=""
740cc6efb6Smrg
750cc6efb6Smrgwhile [ x"$1" != x ]; do
760cc6efb6Smrg    case $1 in
770cc6efb6Smrg	-c) instcmd=$cpprog
780cc6efb6Smrg	    shift
790cc6efb6Smrg	    continue;;
800cc6efb6Smrg
810cc6efb6Smrg	-d) dir_arg=true
820cc6efb6Smrg	    shift
830cc6efb6Smrg	    continue;;
840cc6efb6Smrg
850cc6efb6Smrg	-m) chmodcmd="$chmodprog $2"
860cc6efb6Smrg	    shift
870cc6efb6Smrg	    shift
880cc6efb6Smrg	    continue;;
890cc6efb6Smrg
900cc6efb6Smrg	-o) chowncmd="$chownprog $2"
910cc6efb6Smrg	    shift
920cc6efb6Smrg	    shift
930cc6efb6Smrg	    continue;;
940cc6efb6Smrg
950cc6efb6Smrg	-g) chgrpcmd="$chgrpprog $2"
960cc6efb6Smrg	    shift
970cc6efb6Smrg	    shift
980cc6efb6Smrg	    continue;;
990cc6efb6Smrg
1000cc6efb6Smrg	-s) stripcmd=$stripprog
1010cc6efb6Smrg	    shift
1020cc6efb6Smrg	    continue;;
1030cc6efb6Smrg
1040cc6efb6Smrg	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
1050cc6efb6Smrg	    shift
1060cc6efb6Smrg	    continue;;
1070cc6efb6Smrg
1080cc6efb6Smrg	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
1090cc6efb6Smrg	    shift
1100cc6efb6Smrg	    continue;;
1110cc6efb6Smrg
1120cc6efb6Smrg	*)  if [ x"$src" = x ]
1130cc6efb6Smrg	    then
1140cc6efb6Smrg		src=$1
1150cc6efb6Smrg	    else
1160cc6efb6Smrg		# this colon is to work around a 386BSD /bin/sh bug
1170cc6efb6Smrg		:
1180cc6efb6Smrg		dst=$1
1190cc6efb6Smrg	    fi
1200cc6efb6Smrg	    shift
1210cc6efb6Smrg	    continue;;
1220cc6efb6Smrg    esac
1230cc6efb6Smrgdone
1240cc6efb6Smrg
1250cc6efb6Smrgif [ x"$src" = x ]
1260cc6efb6Smrgthen
1270cc6efb6Smrg	echo "$0: no input file specified" >&2
1280cc6efb6Smrg	exit 1
1290cc6efb6Smrgelse
1300cc6efb6Smrg	:
1310cc6efb6Smrgfi
1320cc6efb6Smrg
1330cc6efb6Smrgif [ x"$dir_arg" != x ]; then
1340cc6efb6Smrg	dst=$src
1350cc6efb6Smrg	src=""
1360cc6efb6Smrg
1370cc6efb6Smrg	if [ -d "$dst" ]; then
1380cc6efb6Smrg		instcmd=:
1390cc6efb6Smrg		chmodcmd=""
1400cc6efb6Smrg	else
1410cc6efb6Smrg		instcmd=$mkdirprog
1420cc6efb6Smrg	fi
1430cc6efb6Smrgelse
1440cc6efb6Smrg
1450cc6efb6Smrg# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
1460cc6efb6Smrg# might cause directories to be created, which would be especially bad
1470cc6efb6Smrg# if $src (and thus $dsttmp) contains '*'.
1480cc6efb6Smrg
1490cc6efb6Smrg	if [ -f "$src" ] || [ -d "$src" ]
1500cc6efb6Smrg	then
1510cc6efb6Smrg		:
1520cc6efb6Smrg	else
1530cc6efb6Smrg		echo "$0: $src does not exist" >&2
1540cc6efb6Smrg		exit 1
1550cc6efb6Smrg	fi
1560cc6efb6Smrg
1570cc6efb6Smrg	if [ x"$dst" = x ]
1580cc6efb6Smrg	then
1590cc6efb6Smrg		echo "$0: no destination specified" >&2
1600cc6efb6Smrg		exit 1
1610cc6efb6Smrg	else
1620cc6efb6Smrg		:
1630cc6efb6Smrg	fi
1640cc6efb6Smrg
1650cc6efb6Smrg# If destination is a directory, append the input filename; if your system
1660cc6efb6Smrg# does not like double slashes in filenames, you may need to add some logic
1670cc6efb6Smrg
1680cc6efb6Smrg	if [ -d "$dst" ]
1690cc6efb6Smrg	then
1700cc6efb6Smrg		dst=$dst/`basename "$src"`
1710cc6efb6Smrg	else
1720cc6efb6Smrg		:
1730cc6efb6Smrg	fi
1740cc6efb6Smrgfi
1750cc6efb6Smrg
1760cc6efb6Smrg## this sed command emulates the dirname command
1770cc6efb6Smrgdstdir=`echo "$dst" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
1780cc6efb6Smrg
1790cc6efb6Smrg# Make sure that the destination directory exists.
1800cc6efb6Smrg#  this part is taken from Noah Friedman's mkinstalldirs script
1810cc6efb6Smrg
1820cc6efb6Smrg# Skip lots of stat calls in the usual case.
1830cc6efb6Smrgif [ ! -d "$dstdir" ]; then
1840cc6efb6SmrgdefaultIFS='
1850cc6efb6Smrg	'
1860cc6efb6SmrgIFS="${IFS-$defaultIFS}"
1870cc6efb6Smrg
1880cc6efb6SmrgoIFS=$IFS
1890cc6efb6Smrg# Some sh's can't handle IFS=/ for some reason.
1900cc6efb6SmrgIFS='%'
1910cc6efb6Smrgset - `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
1920cc6efb6SmrgIFS=$oIFS
1930cc6efb6Smrg
1940cc6efb6Smrgpathcomp=''
1950cc6efb6Smrg
1960cc6efb6Smrgwhile [ $# -ne 0 ] ; do
1970cc6efb6Smrg	pathcomp=$pathcomp$1
1980cc6efb6Smrg	shift
1990cc6efb6Smrg
2000cc6efb6Smrg	if [ ! -d "$pathcomp" ] ;
2010cc6efb6Smrg        then
2020cc6efb6Smrg		$mkdirprog "$pathcomp"
2030cc6efb6Smrg	else
2040cc6efb6Smrg		:
2050cc6efb6Smrg	fi
2060cc6efb6Smrg
2070cc6efb6Smrg	pathcomp=$pathcomp/
2080cc6efb6Smrgdone
2090cc6efb6Smrgfi
2100cc6efb6Smrg
2110cc6efb6Smrgif [ x"$dir_arg" != x ]
2120cc6efb6Smrgthen
2130cc6efb6Smrg	$doit $instcmd "$dst" &&
2140cc6efb6Smrg
2150cc6efb6Smrg	if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dst"; else : ; fi &&
2160cc6efb6Smrg	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dst"; else : ; fi &&
2170cc6efb6Smrg	if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dst"; else : ; fi &&
2180cc6efb6Smrg	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dst"; else : ; fi
2190cc6efb6Smrgelse
2200cc6efb6Smrg
2210cc6efb6Smrg# If we're going to rename the final executable, determine the name now.
2220cc6efb6Smrg
2230cc6efb6Smrg	if [ x"$transformarg" = x ]
2240cc6efb6Smrg	then
2250cc6efb6Smrg		dstfile=`basename "$dst"`
2260cc6efb6Smrg	else
2270cc6efb6Smrg		dstfile=`basename "$dst" $transformbasename |
2280cc6efb6Smrg			sed $transformarg`$transformbasename
2290cc6efb6Smrg	fi
2300cc6efb6Smrg
2310cc6efb6Smrg# don't allow the sed command to completely eliminate the filename
2320cc6efb6Smrg
2330cc6efb6Smrg	if [ x"$dstfile" = x ]
2340cc6efb6Smrg	then
2350cc6efb6Smrg		dstfile=`basename "$dst"`
2360cc6efb6Smrg	else
2370cc6efb6Smrg		:
2380cc6efb6Smrg	fi
2390cc6efb6Smrg
2400cc6efb6Smrg# Make a couple of temp file names in the proper directory.
2410cc6efb6Smrg
2420cc6efb6Smrg	dsttmp=$dstdir/_inst.$$_
2430cc6efb6Smrg	rmtmp=$dstdir/_rm.$$_
2440cc6efb6Smrg
2450cc6efb6Smrg# Trap to clean up temp files at exit.
2460cc6efb6Smrg
2470cc6efb6Smrg	trap 'status=$?; rm -f "$dsttmp" "$rmtmp" && exit $status' 0
2480cc6efb6Smrg	trap '(exit $?); exit' 1 2 13 15
2490cc6efb6Smrg
2500cc6efb6Smrg# Move or copy the file name to the temp name
2510cc6efb6Smrg
2520cc6efb6Smrg	$doit $instcmd "$src" "$dsttmp" &&
2530cc6efb6Smrg
2540cc6efb6Smrg# and set any options; do chmod last to preserve setuid bits
2550cc6efb6Smrg
2560cc6efb6Smrg# If any of these fail, we abort the whole thing.  If we want to
2570cc6efb6Smrg# ignore errors from any of these, just make sure not to ignore
2580cc6efb6Smrg# errors from the above "$doit $instcmd $src $dsttmp" command.
2590cc6efb6Smrg
2600cc6efb6Smrg	if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp"; else :;fi &&
2610cc6efb6Smrg	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp"; else :;fi &&
2620cc6efb6Smrg	if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dsttmp"; else :;fi &&
2630cc6efb6Smrg	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; else :;fi &&
2640cc6efb6Smrg
2650cc6efb6Smrg# Now remove or move aside any old file at destination location.  We try this
2660cc6efb6Smrg# two ways since rm can't unlink itself on some systems and the destination
2670cc6efb6Smrg# file might be busy for other reasons.  In this case, the final cleanup
2680cc6efb6Smrg# might fail but the new file should still install successfully.
2690cc6efb6Smrg
2700cc6efb6Smrg{
2710cc6efb6Smrg	if [ -f "$dstdir/$dstfile" ]
2720cc6efb6Smrg	then
2730cc6efb6Smrg		$doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null ||
2740cc6efb6Smrg		$doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null ||
2750cc6efb6Smrg		{
2760cc6efb6Smrg		  echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
2770cc6efb6Smrg		  (exit 1); exit
2780cc6efb6Smrg		}
2790cc6efb6Smrg	else
2800cc6efb6Smrg		:
2810cc6efb6Smrg	fi
2820cc6efb6Smrg} &&
2830cc6efb6Smrg
2840cc6efb6Smrg# Now rename the file to the real destination.
2850cc6efb6Smrg
2860cc6efb6Smrg	$doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
2870cc6efb6Smrg
2880cc6efb6Smrgfi &&
2890cc6efb6Smrg
2900cc6efb6Smrg# The final little trick to "correctly" pass the exit status to the exit trap.
2910cc6efb6Smrg
2920cc6efb6Smrg{
2930cc6efb6Smrg	(exit 0); exit
2940cc6efb6Smrg}
295