install-sh revision 4ecf19b2
14ecf19b2Smrg#!/bin/sh
24ecf19b2Smrg#
34ecf19b2Smrg# install - install a program, script, or datafile
44ecf19b2Smrg#
54ecf19b2Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
64ecf19b2Smrg# later released in X11R6 (xc/config/util/install.sh) with the
74ecf19b2Smrg# following copyright and license.
84ecf19b2Smrg#
94ecf19b2Smrg# Copyright (C) 1994 X Consortium
104ecf19b2Smrg#
114ecf19b2Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
124ecf19b2Smrg# of this software and associated documentation files (the "Software"), to
134ecf19b2Smrg# deal in the Software without restriction, including without limitation the
144ecf19b2Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
154ecf19b2Smrg# sell copies of the Software, and to permit persons to whom the Software is
164ecf19b2Smrg# furnished to do so, subject to the following conditions:
174ecf19b2Smrg#
184ecf19b2Smrg# The above copyright notice and this permission notice shall be included in
194ecf19b2Smrg# all copies or substantial portions of the Software.
204ecf19b2Smrg#
214ecf19b2Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
224ecf19b2Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
234ecf19b2Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
244ecf19b2Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
254ecf19b2Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
264ecf19b2Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
274ecf19b2Smrg#
284ecf19b2Smrg# Except as contained in this notice, the name of the X Consortium shall not
294ecf19b2Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
304ecf19b2Smrg# ings in this Software without prior written authorization from the X Consor-
314ecf19b2Smrg# tium.
324ecf19b2Smrg#
334ecf19b2Smrg#
344ecf19b2Smrg# FSF changes to this file are in the public domain.
354ecf19b2Smrg#
364ecf19b2Smrg# Calling this script install-sh is preferred over install.sh, to prevent
374ecf19b2Smrg# `make' implicit rules from creating a file called install from it
384ecf19b2Smrg# when there is no Makefile.
394ecf19b2Smrg#
404ecf19b2Smrg# This script is compatible with the BSD install script, but was written
414ecf19b2Smrg# from scratch.  It can only install one file at a time, a restriction
424ecf19b2Smrg# shared with many OS's install programs.
434ecf19b2Smrg
444ecf19b2Smrg
454ecf19b2Smrg# set DOITPROG to echo to test this script
464ecf19b2Smrg
474ecf19b2Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
484ecf19b2Smrgdoit="${DOITPROG-}"
494ecf19b2Smrg
504ecf19b2Smrg
514ecf19b2Smrg# put in absolute paths if you don't have them in your path; or use env. vars.
524ecf19b2Smrg
534ecf19b2Smrgmvprog="${MVPROG-mv}"
544ecf19b2Smrgcpprog="${CPPROG-cp}"
554ecf19b2Smrgchmodprog="${CHMODPROG-chmod}"
564ecf19b2Smrgchownprog="${CHOWNPROG-chown}"
574ecf19b2Smrgchgrpprog="${CHGRPPROG-chgrp}"
584ecf19b2Smrgstripprog="${STRIPPROG-strip}"
594ecf19b2Smrgrmprog="${RMPROG-rm}"
604ecf19b2Smrgmkdirprog="${MKDIRPROG-mkdir}"
614ecf19b2Smrg
624ecf19b2Smrgtransformbasename=""
634ecf19b2Smrgtransform_arg=""
644ecf19b2Smrginstcmd="$mvprog"
654ecf19b2Smrgchmodcmd="$chmodprog 0755"
664ecf19b2Smrgchowncmd=""
674ecf19b2Smrgchgrpcmd=""
684ecf19b2Smrgstripcmd=""
694ecf19b2Smrgrmcmd="$rmprog -f"
704ecf19b2Smrgmvcmd="$mvprog"
714ecf19b2Smrgsrc=""
724ecf19b2Smrgdst=""
734ecf19b2Smrgdir_arg=""
744ecf19b2Smrg
754ecf19b2Smrgwhile [ x"$1" != x ]; do
764ecf19b2Smrg    case $1 in
774ecf19b2Smrg	-c) instcmd=$cpprog
784ecf19b2Smrg	    shift
794ecf19b2Smrg	    continue;;
804ecf19b2Smrg
814ecf19b2Smrg	-d) dir_arg=true
824ecf19b2Smrg	    shift
834ecf19b2Smrg	    continue;;
844ecf19b2Smrg
854ecf19b2Smrg	-m) chmodcmd="$chmodprog $2"
864ecf19b2Smrg	    shift
874ecf19b2Smrg	    shift
884ecf19b2Smrg	    continue;;
894ecf19b2Smrg
904ecf19b2Smrg	-o) chowncmd="$chownprog $2"
914ecf19b2Smrg	    shift
924ecf19b2Smrg	    shift
934ecf19b2Smrg	    continue;;
944ecf19b2Smrg
954ecf19b2Smrg	-g) chgrpcmd="$chgrpprog $2"
964ecf19b2Smrg	    shift
974ecf19b2Smrg	    shift
984ecf19b2Smrg	    continue;;
994ecf19b2Smrg
1004ecf19b2Smrg	-s) stripcmd=$stripprog
1014ecf19b2Smrg	    shift
1024ecf19b2Smrg	    continue;;
1034ecf19b2Smrg
1044ecf19b2Smrg	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
1054ecf19b2Smrg	    shift
1064ecf19b2Smrg	    continue;;
1074ecf19b2Smrg
1084ecf19b2Smrg	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
1094ecf19b2Smrg	    shift
1104ecf19b2Smrg	    continue;;
1114ecf19b2Smrg
1124ecf19b2Smrg	*)  if [ x"$src" = x ]
1134ecf19b2Smrg	    then
1144ecf19b2Smrg		src=$1
1154ecf19b2Smrg	    else
1164ecf19b2Smrg		# this colon is to work around a 386BSD /bin/sh bug
1174ecf19b2Smrg		:
1184ecf19b2Smrg		dst=$1
1194ecf19b2Smrg	    fi
1204ecf19b2Smrg	    shift
1214ecf19b2Smrg	    continue;;
1224ecf19b2Smrg    esac
1234ecf19b2Smrgdone
1244ecf19b2Smrg
1254ecf19b2Smrgif [ x"$src" = x ]
1264ecf19b2Smrgthen
1274ecf19b2Smrg	echo "$0: no input file specified" >&2
1284ecf19b2Smrg	exit 1
1294ecf19b2Smrgelse
1304ecf19b2Smrg	:
1314ecf19b2Smrgfi
1324ecf19b2Smrg
1334ecf19b2Smrgif [ x"$dir_arg" != x ]; then
1344ecf19b2Smrg	dst=$src
1354ecf19b2Smrg	src=""
1364ecf19b2Smrg
1374ecf19b2Smrg	if [ -d "$dst" ]; then
1384ecf19b2Smrg		instcmd=:
1394ecf19b2Smrg		chmodcmd=""
1404ecf19b2Smrg	else
1414ecf19b2Smrg		instcmd=$mkdirprog
1424ecf19b2Smrg	fi
1434ecf19b2Smrgelse
1444ecf19b2Smrg
1454ecf19b2Smrg# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
1464ecf19b2Smrg# might cause directories to be created, which would be especially bad
1474ecf19b2Smrg# if $src (and thus $dsttmp) contains '*'.
1484ecf19b2Smrg
1494ecf19b2Smrg	if [ -f "$src" ] || [ -d "$src" ]
1504ecf19b2Smrg	then
1514ecf19b2Smrg		:
1524ecf19b2Smrg	else
1534ecf19b2Smrg		echo "$0: $src does not exist" >&2
1544ecf19b2Smrg		exit 1
1554ecf19b2Smrg	fi
1564ecf19b2Smrg
1574ecf19b2Smrg	if [ x"$dst" = x ]
1584ecf19b2Smrg	then
1594ecf19b2Smrg		echo "$0: no destination specified" >&2
1604ecf19b2Smrg		exit 1
1614ecf19b2Smrg	else
1624ecf19b2Smrg		:
1634ecf19b2Smrg	fi
1644ecf19b2Smrg
1654ecf19b2Smrg# If destination is a directory, append the input filename; if your system
1664ecf19b2Smrg# does not like double slashes in filenames, you may need to add some logic
1674ecf19b2Smrg
1684ecf19b2Smrg	if [ -d "$dst" ]
1694ecf19b2Smrg	then
1704ecf19b2Smrg		dst=$dst/`basename "$src"`
1714ecf19b2Smrg	else
1724ecf19b2Smrg		:
1734ecf19b2Smrg	fi
1744ecf19b2Smrgfi
1754ecf19b2Smrg
1764ecf19b2Smrg## this sed command emulates the dirname command
1774ecf19b2Smrgdstdir=`echo "$dst" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
1784ecf19b2Smrg
1794ecf19b2Smrg# Make sure that the destination directory exists.
1804ecf19b2Smrg#  this part is taken from Noah Friedman's mkinstalldirs script
1814ecf19b2Smrg
1824ecf19b2Smrg# Skip lots of stat calls in the usual case.
1834ecf19b2Smrgif [ ! -d "$dstdir" ]; then
1844ecf19b2SmrgdefaultIFS='
1854ecf19b2Smrg	'
1864ecf19b2SmrgIFS="${IFS-$defaultIFS}"
1874ecf19b2Smrg
1884ecf19b2SmrgoIFS=$IFS
1894ecf19b2Smrg# Some sh's can't handle IFS=/ for some reason.
1904ecf19b2SmrgIFS='%'
1914ecf19b2Smrgset - `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
1924ecf19b2SmrgIFS=$oIFS
1934ecf19b2Smrg
1944ecf19b2Smrgpathcomp=''
1954ecf19b2Smrg
1964ecf19b2Smrgwhile [ $# -ne 0 ] ; do
1974ecf19b2Smrg	pathcomp=$pathcomp$1
1984ecf19b2Smrg	shift
1994ecf19b2Smrg
2004ecf19b2Smrg	if [ ! -d "$pathcomp" ] ;
2014ecf19b2Smrg        then
2024ecf19b2Smrg		$mkdirprog "$pathcomp"
2034ecf19b2Smrg	else
2044ecf19b2Smrg		:
2054ecf19b2Smrg	fi
2064ecf19b2Smrg
2074ecf19b2Smrg	pathcomp=$pathcomp/
2084ecf19b2Smrgdone
2094ecf19b2Smrgfi
2104ecf19b2Smrg
2114ecf19b2Smrgif [ x"$dir_arg" != x ]
2124ecf19b2Smrgthen
2134ecf19b2Smrg	$doit $instcmd "$dst" &&
2144ecf19b2Smrg
2154ecf19b2Smrg	if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dst"; else : ; fi &&
2164ecf19b2Smrg	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dst"; else : ; fi &&
2174ecf19b2Smrg	if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dst"; else : ; fi &&
2184ecf19b2Smrg	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dst"; else : ; fi
2194ecf19b2Smrgelse
2204ecf19b2Smrg
2214ecf19b2Smrg# If we're going to rename the final executable, determine the name now.
2224ecf19b2Smrg
2234ecf19b2Smrg	if [ x"$transformarg" = x ]
2244ecf19b2Smrg	then
2254ecf19b2Smrg		dstfile=`basename "$dst"`
2264ecf19b2Smrg	else
2274ecf19b2Smrg		dstfile=`basename "$dst" $transformbasename |
2284ecf19b2Smrg			sed $transformarg`$transformbasename
2294ecf19b2Smrg	fi
2304ecf19b2Smrg
2314ecf19b2Smrg# don't allow the sed command to completely eliminate the filename
2324ecf19b2Smrg
2334ecf19b2Smrg	if [ x"$dstfile" = x ]
2344ecf19b2Smrg	then
2354ecf19b2Smrg		dstfile=`basename "$dst"`
2364ecf19b2Smrg	else
2374ecf19b2Smrg		:
2384ecf19b2Smrg	fi
2394ecf19b2Smrg
2404ecf19b2Smrg# Make a couple of temp file names in the proper directory.
2414ecf19b2Smrg
2424ecf19b2Smrg	dsttmp=$dstdir/_inst.$$_
2434ecf19b2Smrg	rmtmp=$dstdir/_rm.$$_
2444ecf19b2Smrg
2454ecf19b2Smrg# Trap to clean up temp files at exit.
2464ecf19b2Smrg
2474ecf19b2Smrg	trap 'status=$?; rm -f "$dsttmp" "$rmtmp" && exit $status' 0
2484ecf19b2Smrg	trap '(exit $?); exit' 1 2 13 15
2494ecf19b2Smrg
2504ecf19b2Smrg# Move or copy the file name to the temp name
2514ecf19b2Smrg
2524ecf19b2Smrg	$doit $instcmd "$src" "$dsttmp" &&
2534ecf19b2Smrg
2544ecf19b2Smrg# and set any options; do chmod last to preserve setuid bits
2554ecf19b2Smrg
2564ecf19b2Smrg# If any of these fail, we abort the whole thing.  If we want to
2574ecf19b2Smrg# ignore errors from any of these, just make sure not to ignore
2584ecf19b2Smrg# errors from the above "$doit $instcmd $src $dsttmp" command.
2594ecf19b2Smrg
2604ecf19b2Smrg	if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp"; else :;fi &&
2614ecf19b2Smrg	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp"; else :;fi &&
2624ecf19b2Smrg	if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dsttmp"; else :;fi &&
2634ecf19b2Smrg	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; else :;fi &&
2644ecf19b2Smrg
2654ecf19b2Smrg# Now remove or move aside any old file at destination location.  We try this
2664ecf19b2Smrg# two ways since rm can't unlink itself on some systems and the destination
2674ecf19b2Smrg# file might be busy for other reasons.  In this case, the final cleanup
2684ecf19b2Smrg# might fail but the new file should still install successfully.
2694ecf19b2Smrg
2704ecf19b2Smrg{
2714ecf19b2Smrg	if [ -f "$dstdir/$dstfile" ]
2724ecf19b2Smrg	then
2734ecf19b2Smrg		$doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null ||
2744ecf19b2Smrg		$doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null ||
2754ecf19b2Smrg		{
2764ecf19b2Smrg		  echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
2774ecf19b2Smrg		  (exit 1); exit
2784ecf19b2Smrg		}
2794ecf19b2Smrg	else
2804ecf19b2Smrg		:
2814ecf19b2Smrg	fi
2824ecf19b2Smrg} &&
2834ecf19b2Smrg
2844ecf19b2Smrg# Now rename the file to the real destination.
2854ecf19b2Smrg
2864ecf19b2Smrg	$doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
2874ecf19b2Smrg
2884ecf19b2Smrgfi &&
2894ecf19b2Smrg
2904ecf19b2Smrg# The final little trick to "correctly" pass the exit status to the exit trap.
2914ecf19b2Smrg
2924ecf19b2Smrg{
2934ecf19b2Smrg	(exit 0); exit
2944ecf19b2Smrg}
295