Blame install-sh

Packit f574b8
#! /bin/sh
Packit f574b8
#
Packit f574b8
# install - install a program, script, or datafile
Packit f574b8
#
Packit f574b8
# This originates from X11R5 (mit/util/scripts/install.sh), which was
Packit f574b8
# later released in X11R6 (xc/config/util/install.sh) with the
Packit f574b8
# following copyright and license.
Packit f574b8
#
Packit f574b8
# Copyright (C) 1994 X Consortium
Packit f574b8
#
Packit f574b8
# Permission is hereby granted, free of charge, to any person obtaining a copy
Packit f574b8
# of this software and associated documentation files (the "Software"), to
Packit f574b8
# deal in the Software without restriction, including without limitation the
Packit f574b8
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
Packit f574b8
# sell copies of the Software, and to permit persons to whom the Software is
Packit f574b8
# furnished to do so, subject to the following conditions:
Packit f574b8
#
Packit f574b8
# The above copyright notice and this permission notice shall be included in
Packit f574b8
# all copies or substantial portions of the Software.
Packit f574b8
#
Packit f574b8
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit f574b8
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit f574b8
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
Packit f574b8
# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
Packit f574b8
# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
Packit f574b8
# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Packit f574b8
#
Packit f574b8
# Except as contained in this notice, the name of the X Consortium shall not
Packit f574b8
# be used in advertising or otherwise to promote the sale, use or other deal-
Packit f574b8
# ings in this Software without prior written authorization from the X Consor-
Packit f574b8
# tium.
Packit f574b8
#
Packit f574b8
#
Packit f574b8
# FSF changes to this file are in the public domain.
Packit f574b8
#
Packit f574b8
# Calling this script install-sh is preferred over install.sh, to prevent
Packit f574b8
# `make' implicit rules from creating a file called install from it
Packit f574b8
# when there is no Makefile.
Packit f574b8
#
Packit f574b8
# This script is compatible with the BSD install script, but was written
Packit f574b8
# from scratch.  It can only install one file at a time, a restriction
Packit f574b8
# shared with many OS's install programs.
Packit f574b8
Packit f574b8
Packit f574b8
# set DOITPROG to echo to test this script
Packit f574b8
Packit f574b8
# Don't use :- since 4.3BSD and earlier shells don't like it.
Packit f574b8
doit="${DOITPROG-}"
Packit f574b8
Packit f574b8
Packit f574b8
# put in absolute paths if you don't have them in your path; or use env. vars.
Packit f574b8
Packit f574b8
mvprog="${MVPROG-mv}"
Packit f574b8
cpprog="${CPPROG-cp}"
Packit f574b8
chmodprog="${CHMODPROG-chmod}"
Packit f574b8
chownprog="${CHOWNPROG-chown}"
Packit f574b8
chgrpprog="${CHGRPPROG-chgrp}"
Packit f574b8
stripprog="${STRIPPROG-strip}"
Packit f574b8
rmprog="${RMPROG-rm}"
Packit f574b8
mkdirprog="${MKDIRPROG-mkdir}"
Packit f574b8
Packit f574b8
transformbasename=""
Packit f574b8
transform_arg=""
Packit f574b8
instcmd="$mvprog"
Packit f574b8
chmodcmd="$chmodprog 0755"
Packit f574b8
chowncmd=""
Packit f574b8
chgrpcmd=""
Packit f574b8
stripcmd=""
Packit f574b8
rmcmd="$rmprog -f"
Packit f574b8
mvcmd="$mvprog"
Packit f574b8
src=""
Packit f574b8
dst=""
Packit f574b8
dir_arg=""
Packit f574b8
Packit f574b8
while [ x"$1" != x ]; do
Packit f574b8
    case $1 in
Packit f574b8
	-c) instcmd=$cpprog
Packit f574b8
	    shift
Packit f574b8
	    continue;;
Packit f574b8
Packit f574b8
	-d) dir_arg=true
Packit f574b8
	    shift
Packit f574b8
	    continue;;
Packit f574b8
Packit f574b8
	-m) chmodcmd="$chmodprog $2"
Packit f574b8
	    shift
Packit f574b8
	    shift
Packit f574b8
	    continue;;
Packit f574b8
Packit f574b8
	-o) chowncmd="$chownprog $2"
Packit f574b8
	    shift
Packit f574b8
	    shift
Packit f574b8
	    continue;;
Packit f574b8
Packit f574b8
	-g) chgrpcmd="$chgrpprog $2"
Packit f574b8
	    shift
Packit f574b8
	    shift
Packit f574b8
	    continue;;
Packit f574b8
Packit f574b8
	-s) stripcmd=$stripprog
Packit f574b8
	    shift
Packit f574b8
	    continue;;
Packit f574b8
Packit f574b8
	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
Packit f574b8
	    shift
Packit f574b8
	    continue;;
Packit f574b8
Packit f574b8
	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
Packit f574b8
	    shift
Packit f574b8
	    continue;;
Packit f574b8
Packit f574b8
	*)  if [ x"$src" = x ]
Packit f574b8
	    then
Packit f574b8
		src=$1
Packit f574b8
	    else
Packit f574b8
		# this colon is to work around a 386BSD /bin/sh bug
Packit f574b8
		:
Packit f574b8
		dst=$1
Packit f574b8
	    fi
Packit f574b8
	    shift
Packit f574b8
	    continue;;
Packit f574b8
    esac
Packit f574b8
done
Packit f574b8
Packit f574b8
if [ x"$src" = x ]
Packit f574b8
then
Packit f574b8
	echo "$0: no input file specified" >&2
Packit f574b8
	exit 1
Packit f574b8
else
Packit f574b8
	:
Packit f574b8
fi
Packit f574b8
Packit f574b8
if [ x"$dir_arg" != x ]; then
Packit f574b8
	dst=$src
Packit f574b8
	src=""
Packit f574b8
Packit f574b8
	if [ -d "$dst" ]; then
Packit f574b8
		instcmd=:
Packit f574b8
		chmodcmd=""
Packit f574b8
	else
Packit f574b8
		instcmd=$mkdirprog
Packit f574b8
	fi
Packit f574b8
else
Packit f574b8
Packit f574b8
# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
Packit f574b8
# might cause directories to be created, which would be especially bad
Packit f574b8
# if $src (and thus $dsttmp) contains '*'.
Packit f574b8
Packit f574b8
	if [ -f "$src" ] || [ -d "$src" ]
Packit f574b8
	then
Packit f574b8
		:
Packit f574b8
	else
Packit f574b8
		echo "$0: $src does not exist" >&2
Packit f574b8
		exit 1
Packit f574b8
	fi
Packit f574b8
Packit f574b8
	if [ x"$dst" = x ]
Packit f574b8
	then
Packit f574b8
		echo "$0: no destination specified" >&2
Packit f574b8
		exit 1
Packit f574b8
	else
Packit f574b8
		:
Packit f574b8
	fi
Packit f574b8
Packit f574b8
# If destination is a directory, append the input filename; if your system
Packit f574b8
# does not like double slashes in filenames, you may need to add some logic
Packit f574b8
Packit f574b8
	if [ -d "$dst" ]
Packit f574b8
	then
Packit f574b8
		dst=$dst/`basename "$src"`
Packit f574b8
	else
Packit f574b8
		:
Packit f574b8
	fi
Packit f574b8
fi
Packit f574b8
Packit f574b8
## this sed command emulates the dirname command
Packit f574b8
dstdir=`echo "$dst" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
Packit f574b8
Packit f574b8
# Make sure that the destination directory exists.
Packit f574b8
#  this part is taken from Noah Friedman's mkinstalldirs script
Packit f574b8
Packit f574b8
# Skip lots of stat calls in the usual case.
Packit f574b8
if [ ! -d "$dstdir" ]; then
Packit f574b8
defaultIFS='
Packit f574b8
	'
Packit f574b8
IFS="${IFS-$defaultIFS}"
Packit f574b8
Packit f574b8
oIFS=$IFS
Packit f574b8
# Some sh's can't handle IFS=/ for some reason.
Packit f574b8
IFS='%'
Packit f574b8
set - `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
Packit f574b8
IFS=$oIFS
Packit f574b8
Packit f574b8
pathcomp=''
Packit f574b8
Packit f574b8
while [ $# -ne 0 ] ; do
Packit f574b8
	pathcomp=$pathcomp$1
Packit f574b8
	shift
Packit f574b8
Packit f574b8
	if [ ! -d "$pathcomp" ] ;
Packit f574b8
        then
Packit f574b8
		$mkdirprog "$pathcomp"
Packit f574b8
	else
Packit f574b8
		:
Packit f574b8
	fi
Packit f574b8
Packit f574b8
	pathcomp=$pathcomp/
Packit f574b8
done
Packit f574b8
fi
Packit f574b8
Packit f574b8
if [ x"$dir_arg" != x ]
Packit f574b8
then
Packit f574b8
	$doit $instcmd "$dst" &&
Packit f574b8
Packit f574b8
	if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dst"; else : ; fi &&
Packit f574b8
	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dst"; else : ; fi &&
Packit f574b8
	if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dst"; else : ; fi &&
Packit f574b8
	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dst"; else : ; fi
Packit f574b8
else
Packit f574b8
Packit f574b8
# If we're going to rename the final executable, determine the name now.
Packit f574b8
Packit f574b8
	if [ x"$transformarg" = x ]
Packit f574b8
	then
Packit f574b8
		dstfile=`basename "$dst"`
Packit f574b8
	else
Packit f574b8
		dstfile=`basename "$dst" $transformbasename |
Packit f574b8
			sed $transformarg`$transformbasename
Packit f574b8
	fi
Packit f574b8
Packit f574b8
# don't allow the sed command to completely eliminate the filename
Packit f574b8
Packit f574b8
	if [ x"$dstfile" = x ]
Packit f574b8
	then
Packit f574b8
		dstfile=`basename "$dst"`
Packit f574b8
	else
Packit f574b8
		:
Packit f574b8
	fi
Packit f574b8
Packit f574b8
# Make a couple of temp file names in the proper directory.
Packit f574b8
Packit f574b8
	dsttmp=$dstdir/#inst.$$#
Packit f574b8
	rmtmp=$dstdir/#rm.$$#
Packit f574b8
Packit f574b8
# Trap to clean up temp files at exit.
Packit f574b8
Packit f574b8
	trap 'status=$?; rm -f "$dsttmp" "$rmtmp" && exit $status' 0
Packit f574b8
	trap '(exit $?); exit' 1 2 13 15
Packit f574b8
Packit f574b8
# Move or copy the file name to the temp name
Packit f574b8
Packit f574b8
	$doit $instcmd "$src" "$dsttmp" &&
Packit f574b8
Packit f574b8
# and set any options; do chmod last to preserve setuid bits
Packit f574b8
Packit f574b8
# If any of these fail, we abort the whole thing.  If we want to
Packit f574b8
# ignore errors from any of these, just make sure not to ignore
Packit f574b8
# errors from the above "$doit $instcmd $src $dsttmp" command.
Packit f574b8
Packit f574b8
	if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp"; else :;fi &&
Packit f574b8
	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp"; else :;fi &&
Packit f574b8
	if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dsttmp"; else :;fi &&
Packit f574b8
	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; else :;fi &&
Packit f574b8
Packit f574b8
# Now remove or move aside any old file at destination location.  We try this
Packit f574b8
# two ways since rm can't unlink itself on some systems and the destination
Packit f574b8
# file might be busy for other reasons.  In this case, the final cleanup
Packit f574b8
# might fail but the new file should still install successfully.
Packit f574b8
Packit f574b8
{
Packit f574b8
	if [ -f "$dstdir/$dstfile" ]
Packit f574b8
	then
Packit f574b8
		$doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null ||
Packit f574b8
		$doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null ||
Packit f574b8
		{
Packit f574b8
		  echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
Packit f574b8
		  (exit 1); exit
Packit f574b8
		}
Packit f574b8
	else
Packit f574b8
		:
Packit f574b8
	fi
Packit f574b8
} &&
Packit f574b8
Packit f574b8
# Now rename the file to the real destination.
Packit f574b8
Packit f574b8
	$doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
Packit f574b8
Packit f574b8
fi &&
Packit f574b8
Packit f574b8
# The final little trick to "correctly" pass the exit status to the exit trap.
Packit f574b8
Packit f574b8
{
Packit f574b8
	(exit 0); exit
Packit f574b8
}