14963b7b6Smrg#!/bin/sh
24963b7b6Smrg#
34963b7b6Smrg# install - install a program, script, or datafile
44963b7b6Smrg#
54963b7b6Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
64963b7b6Smrg# later released in X11R6 (xc/config/util/install.sh) with the
74963b7b6Smrg# following copyright and license.
84963b7b6Smrg#
94963b7b6Smrg# Copyright (C) 1994 X Consortium
104963b7b6Smrg#
114963b7b6Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
124963b7b6Smrg# of this software and associated documentation files (the "Software"), to
134963b7b6Smrg# deal in the Software without restriction, including without limitation the
144963b7b6Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
154963b7b6Smrg# sell copies of the Software, and to permit persons to whom the Software is
164963b7b6Smrg# furnished to do so, subject to the following conditions:
174963b7b6Smrg#
184963b7b6Smrg# The above copyright notice and this permission notice shall be included in
194963b7b6Smrg# all copies or substantial portions of the Software.
204963b7b6Smrg#
214963b7b6Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
224963b7b6Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
234963b7b6Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
244963b7b6Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
254963b7b6Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
264963b7b6Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
274963b7b6Smrg#
284963b7b6Smrg# Except as contained in this notice, the name of the X Consortium shall not
294963b7b6Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
304963b7b6Smrg# ings in this Software without prior written authorization from the X Consor-
314963b7b6Smrg# tium.
324963b7b6Smrg#
334963b7b6Smrg#
344963b7b6Smrg# FSF changes to this file are in the public domain.
354963b7b6Smrg#
364963b7b6Smrg# Calling this script install-sh is preferred over install.sh, to prevent
374963b7b6Smrg# `make' implicit rules from creating a file called install from it
384963b7b6Smrg# when there is no Makefile.
394963b7b6Smrg#
404963b7b6Smrg# This script is compatible with the BSD install script, but was written
414963b7b6Smrg# from scratch.  It can only install one file at a time, a restriction
424963b7b6Smrg# shared with many OS's install programs.
434963b7b6Smrg
444963b7b6Smrg
454963b7b6Smrg# set DOITPROG to echo to test this script
464963b7b6Smrg
474963b7b6Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
484963b7b6Smrgdoit="${DOITPROG-}"
494963b7b6Smrg
504963b7b6Smrg
514963b7b6Smrg# put in absolute paths if you don't have them in your path; or use env. vars.
524963b7b6Smrg
534963b7b6Smrgmvprog="${MVPROG-mv}"
544963b7b6Smrgcpprog="${CPPROG-cp}"
554963b7b6Smrgchmodprog="${CHMODPROG-chmod}"
564963b7b6Smrgchownprog="${CHOWNPROG-chown}"
574963b7b6Smrgchgrpprog="${CHGRPPROG-chgrp}"
584963b7b6Smrgstripprog="${STRIPPROG-strip}"
594963b7b6Smrgrmprog="${RMPROG-rm}"
604963b7b6Smrgmkdirprog="${MKDIRPROG-mkdir}"
614963b7b6Smrg
624963b7b6Smrgtransformbasename=""
634963b7b6Smrgtransform_arg=""
644963b7b6Smrginstcmd="$mvprog"
654963b7b6Smrgchmodcmd="$chmodprog 0755"
664963b7b6Smrgchowncmd=""
674963b7b6Smrgchgrpcmd=""
684963b7b6Smrgstripcmd=""
694963b7b6Smrgrmcmd="$rmprog -f"
704963b7b6Smrgmvcmd="$mvprog"
714963b7b6Smrgsrc=""
724963b7b6Smrgdst=""
734963b7b6Smrgdir_arg=""
744963b7b6Smrg
754963b7b6Smrgwhile [ x"$1" != x ]; do
764963b7b6Smrg    case $1 in
774963b7b6Smrg	-c) instcmd=$cpprog
784963b7b6Smrg	    shift
794963b7b6Smrg	    continue;;
804963b7b6Smrg
814963b7b6Smrg	-d) dir_arg=true
824963b7b6Smrg	    shift
834963b7b6Smrg	    continue;;
844963b7b6Smrg
854963b7b6Smrg	-m) chmodcmd="$chmodprog $2"
864963b7b6Smrg	    shift
874963b7b6Smrg	    shift
884963b7b6Smrg	    continue;;
894963b7b6Smrg
904963b7b6Smrg	-o) chowncmd="$chownprog $2"
914963b7b6Smrg	    shift
924963b7b6Smrg	    shift
934963b7b6Smrg	    continue;;
944963b7b6Smrg
954963b7b6Smrg	-g) chgrpcmd="$chgrpprog $2"
964963b7b6Smrg	    shift
974963b7b6Smrg	    shift
984963b7b6Smrg	    continue;;
994963b7b6Smrg
1004963b7b6Smrg	-s) stripcmd=$stripprog
1014963b7b6Smrg	    shift
1024963b7b6Smrg	    continue;;
1034963b7b6Smrg
1044963b7b6Smrg	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
1054963b7b6Smrg	    shift
1064963b7b6Smrg	    continue;;
1074963b7b6Smrg
1084963b7b6Smrg	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
1094963b7b6Smrg	    shift
1104963b7b6Smrg	    continue;;
1114963b7b6Smrg
1124963b7b6Smrg	*)  if [ x"$src" = x ]
1134963b7b6Smrg	    then
1144963b7b6Smrg		src=$1
1154963b7b6Smrg	    else
1164963b7b6Smrg		# this colon is to work around a 386BSD /bin/sh bug
1174963b7b6Smrg		:
1184963b7b6Smrg		dst=$1
1194963b7b6Smrg	    fi
1204963b7b6Smrg	    shift
1214963b7b6Smrg	    continue;;
1224963b7b6Smrg    esac
1234963b7b6Smrgdone
1244963b7b6Smrg
1254963b7b6Smrgif [ x"$src" = x ]
1264963b7b6Smrgthen
1274963b7b6Smrg	echo "$0: no input file specified" >&2
1284963b7b6Smrg	exit 1
1294963b7b6Smrgelse
1304963b7b6Smrg	:
1314963b7b6Smrgfi
1324963b7b6Smrg
1334963b7b6Smrgif [ x"$dir_arg" != x ]; then
1344963b7b6Smrg	dst=$src
1354963b7b6Smrg	src=""
1364963b7b6Smrg
1374963b7b6Smrg	if [ -d "$dst" ]; then
1384963b7b6Smrg		instcmd=:
1394963b7b6Smrg		chmodcmd=""
1404963b7b6Smrg	else
1414963b7b6Smrg		instcmd=$mkdirprog
1424963b7b6Smrg	fi
1434963b7b6Smrgelse
1444963b7b6Smrg
1454963b7b6Smrg# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
1464963b7b6Smrg# might cause directories to be created, which would be especially bad
1474963b7b6Smrg# if $src (and thus $dsttmp) contains '*'.
1484963b7b6Smrg
1494963b7b6Smrg	if [ -f "$src" ] || [ -d "$src" ]
1504963b7b6Smrg	then
1514963b7b6Smrg		:
1524963b7b6Smrg	else
1534963b7b6Smrg		echo "$0: $src does not exist" >&2
1544963b7b6Smrg		exit 1
1554963b7b6Smrg	fi
1564963b7b6Smrg
1574963b7b6Smrg	if [ x"$dst" = x ]
1584963b7b6Smrg	then
1594963b7b6Smrg		echo "$0: no destination specified" >&2
1604963b7b6Smrg		exit 1
1614963b7b6Smrg	else
1624963b7b6Smrg		:
1634963b7b6Smrg	fi
1644963b7b6Smrg
1654963b7b6Smrg# If destination is a directory, append the input filename; if your system
1664963b7b6Smrg# does not like double slashes in filenames, you may need to add some logic
1674963b7b6Smrg
1684963b7b6Smrg	if [ -d "$dst" ]
1694963b7b6Smrg	then
1704963b7b6Smrg		dst=$dst/`basename "$src"`
1714963b7b6Smrg	else
1724963b7b6Smrg		:
1734963b7b6Smrg	fi
1744963b7b6Smrgfi
1754963b7b6Smrg
1764963b7b6Smrg## this sed command emulates the dirname command
1774963b7b6Smrgdstdir=`echo "$dst" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
1784963b7b6Smrg
1794963b7b6Smrg# Make sure that the destination directory exists.
1804963b7b6Smrg#  this part is taken from Noah Friedman's mkinstalldirs script
1814963b7b6Smrg
1824963b7b6Smrg# Skip lots of stat calls in the usual case.
1834963b7b6Smrgif [ ! -d "$dstdir" ]; then
1844963b7b6SmrgdefaultIFS='
1854963b7b6Smrg	'
1864963b7b6SmrgIFS="${IFS-$defaultIFS}"
1874963b7b6Smrg
1884963b7b6SmrgoIFS=$IFS
1894963b7b6Smrg# Some sh's can't handle IFS=/ for some reason.
1904963b7b6SmrgIFS='%'
1914963b7b6Smrgset - `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
1924963b7b6SmrgIFS=$oIFS
1934963b7b6Smrg
1944963b7b6Smrgpathcomp=''
1954963b7b6Smrg
1964963b7b6Smrgwhile [ $# -ne 0 ] ; do
1974963b7b6Smrg	pathcomp=$pathcomp$1
1984963b7b6Smrg	shift
1994963b7b6Smrg
2004963b7b6Smrg	if [ ! -d "$pathcomp" ] ;
2014963b7b6Smrg        then
2024963b7b6Smrg		$mkdirprog "$pathcomp"
2034963b7b6Smrg	else
2044963b7b6Smrg		:
2054963b7b6Smrg	fi
2064963b7b6Smrg
2074963b7b6Smrg	pathcomp=$pathcomp/
2084963b7b6Smrgdone
2094963b7b6Smrgfi
2104963b7b6Smrg
2114963b7b6Smrgif [ x"$dir_arg" != x ]
2124963b7b6Smrgthen
2134963b7b6Smrg	$doit $instcmd "$dst" &&
2144963b7b6Smrg
2154963b7b6Smrg	if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dst"; else : ; fi &&
2164963b7b6Smrg	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dst"; else : ; fi &&
2174963b7b6Smrg	if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dst"; else : ; fi &&
2184963b7b6Smrg	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dst"; else : ; fi
2194963b7b6Smrgelse
2204963b7b6Smrg
2214963b7b6Smrg# If we're going to rename the final executable, determine the name now.
2224963b7b6Smrg
2234963b7b6Smrg	if [ x"$transformarg" = x ]
2244963b7b6Smrg	then
2254963b7b6Smrg		dstfile=`basename "$dst"`
2264963b7b6Smrg	else
2274963b7b6Smrg		dstfile=`basename "$dst" $transformbasename |
2284963b7b6Smrg			sed $transformarg`$transformbasename
2294963b7b6Smrg	fi
2304963b7b6Smrg
2314963b7b6Smrg# don't allow the sed command to completely eliminate the filename
2324963b7b6Smrg
2334963b7b6Smrg	if [ x"$dstfile" = x ]
2344963b7b6Smrg	then
2354963b7b6Smrg		dstfile=`basename "$dst"`
2364963b7b6Smrg	else
2374963b7b6Smrg		:
2384963b7b6Smrg	fi
2394963b7b6Smrg
2404963b7b6Smrg# Make a couple of temp file names in the proper directory.
2414963b7b6Smrg
2424963b7b6Smrg	dsttmp=$dstdir/_inst.$$_
2434963b7b6Smrg	rmtmp=$dstdir/_rm.$$_
2444963b7b6Smrg
2454963b7b6Smrg# Trap to clean up temp files at exit.
2464963b7b6Smrg
2474963b7b6Smrg	trap 'status=$?; rm -f "$dsttmp" "$rmtmp" && exit $status' 0
2484963b7b6Smrg	trap '(exit $?); exit' 1 2 13 15
2494963b7b6Smrg
2504963b7b6Smrg# Move or copy the file name to the temp name
2514963b7b6Smrg
2524963b7b6Smrg	$doit $instcmd "$src" "$dsttmp" &&
2534963b7b6Smrg
2544963b7b6Smrg# and set any options; do chmod last to preserve setuid bits
2554963b7b6Smrg
2564963b7b6Smrg# If any of these fail, we abort the whole thing.  If we want to
2574963b7b6Smrg# ignore errors from any of these, just make sure not to ignore
2584963b7b6Smrg# errors from the above "$doit $instcmd $src $dsttmp" command.
2594963b7b6Smrg
2604963b7b6Smrg	if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp"; else :;fi &&
2614963b7b6Smrg	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp"; else :;fi &&
2624963b7b6Smrg	if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dsttmp"; else :;fi &&
2634963b7b6Smrg	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; else :;fi &&
2644963b7b6Smrg
2654963b7b6Smrg# Now remove or move aside any old file at destination location.  We try this
2664963b7b6Smrg# two ways since rm can't unlink itself on some systems and the destination
2674963b7b6Smrg# file might be busy for other reasons.  In this case, the final cleanup
2684963b7b6Smrg# might fail but the new file should still install successfully.
2694963b7b6Smrg
2704963b7b6Smrg{
2714963b7b6Smrg	if [ -f "$dstdir/$dstfile" ]
2724963b7b6Smrg	then
2734963b7b6Smrg		$doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null ||
2744963b7b6Smrg		$doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null ||
2754963b7b6Smrg		{
2764963b7b6Smrg		  echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
2774963b7b6Smrg		  (exit 1); exit
2784963b7b6Smrg		}
2794963b7b6Smrg	else
2804963b7b6Smrg		:
2814963b7b6Smrg	fi
2824963b7b6Smrg} &&
2834963b7b6Smrg
2844963b7b6Smrg# Now rename the file to the real destination.
2854963b7b6Smrg
2864963b7b6Smrg	$doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
2874963b7b6Smrg
2884963b7b6Smrgfi &&
2894963b7b6Smrg
2904963b7b6Smrg# The final little trick to "correctly" pass the exit status to the exit trap.
2914963b7b6Smrg
2924963b7b6Smrg{
2934963b7b6Smrg	(exit 0); exit
2944963b7b6Smrg}
295