Blame install-sh

Packit 2fc92b
#!/bin/sh
Packit 2fc92b
#
Packit 2fc92b
# Install a program, script, or datafile.
Packit 2fc92b
#
Packit 2fc92b
# Copyright 2008-2012 by Apple Inc.
Packit 2fc92b
#
Packit 2fc92b
# This script is not compatible with BSD (or any other) install program, as it
Packit 2fc92b
# allows owner and group changes to fail with a warning and makes sure that the
Packit 2fc92b
# destination directory permissions are as specified - BSD install and the
Packit 2fc92b
# original X11 install script did not change permissions of existing
Packit 2fc92b
# directories.  It also does not support the transform options since CUPS does
Packit 2fc92b
# not use them...
Packit 2fc92b
#
Packit 2fc92b
# Original script from X11R5 (mit/util/scripts/install.sh)
Packit 2fc92b
# Copyright 1991 by the Massachusetts Institute of Technology
Packit 2fc92b
#
Packit 2fc92b
# Permission to use, copy, modify, distribute, and sell this software and its
Packit 2fc92b
# documentation for any purpose is hereby granted without fee, provided that
Packit 2fc92b
# the above copyright notice appear in all copies and that both that
Packit 2fc92b
# copyright notice and this permission notice appear in supporting
Packit 2fc92b
# documentation, and that the name of M.I.T. not be used in advertising or
Packit 2fc92b
# publicity pertaining to distribution of the software without specific,
Packit 2fc92b
# written prior permission.  M.I.T. makes no representations about the
Packit 2fc92b
# suitability of this software for any purpose.  It is provided "as is"
Packit 2fc92b
# without express or implied warranty.
Packit 2fc92b
#
Packit 2fc92b
# Calling this script install-sh is preferred over install.sh, to prevent
Packit 2fc92b
# `make' implicit rules from creating a file called install from it
Packit 2fc92b
# when there is no Makefile.
Packit 2fc92b
Packit 2fc92b
# set DOITPROG to echo to test this script
Packit 2fc92b
# Don't use :- since 4.3BSD and earlier shells don't like it.
Packit 2fc92b
doit="${DOITPROG-}"
Packit 2fc92b
Packit 2fc92b
# Force umask to 022...
Packit 2fc92b
umask 022
Packit 2fc92b
Packit 2fc92b
# put in absolute paths if you don't have them in your path; or use env. vars.
Packit 2fc92b
mvprog="${MVPROG-mv}"
Packit 2fc92b
cpprog="${CPPROG-cp}"
Packit 2fc92b
chmodprog="${CHMODPROG-chmod}"
Packit 2fc92b
chownprog="${CHOWNPROG-chown}"
Packit 2fc92b
chgrpprog="${CHGRPPROG-chgrp}"
Packit 2fc92b
stripprog="${STRIPPROG-strip}"
Packit 2fc92b
rmprog="${RMPROG-rm}"
Packit 2fc92b
mkdirprog="${MKDIRPROG-mkdir}"
Packit 2fc92b
gzipprog="${GZIPPROG-gzip}"
Packit 2fc92b
Packit 2fc92b
transformbasename=""
Packit 2fc92b
transform_arg=""
Packit 2fc92b
instcmd="$mvprog"
Packit 2fc92b
chmodcmd="$chmodprog 0755"
Packit 2fc92b
chowncmd=""
Packit 2fc92b
chgrpcmd=""
Packit 2fc92b
stripcmd=""
Packit 2fc92b
rmcmd="$rmprog -f"
Packit 2fc92b
mvcmd="$mvprog"
Packit 2fc92b
src=""
Packit 2fc92b
dst=""
Packit 2fc92b
dir_arg=""
Packit 2fc92b
Packit 2fc92b
gzipcp() {
Packit 2fc92b
	# gzipcp from to
Packit 2fc92b
	$gzipprog -9 <"$1" >"$2"
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
while [ x"$1" != x ]; do
Packit 2fc92b
	case $1 in
Packit 2fc92b
		-c)
Packit 2fc92b
		instcmd="$cpprog"
Packit 2fc92b
		shift
Packit 2fc92b
		continue
Packit 2fc92b
		;;
Packit 2fc92b
Packit 2fc92b
		-d)
Packit 2fc92b
		dir_arg=true
Packit 2fc92b
		shift
Packit 2fc92b
		continue
Packit 2fc92b
		;;
Packit 2fc92b
Packit 2fc92b
		-m)
Packit 2fc92b
		chmodcmd="$chmodprog $2"
Packit 2fc92b
		shift
Packit 2fc92b
		shift
Packit 2fc92b
		continue
Packit 2fc92b
		;;
Packit 2fc92b
Packit 2fc92b
		-o)
Packit 2fc92b
		chowncmd="$chownprog $2"
Packit 2fc92b
		shift
Packit 2fc92b
		shift
Packit 2fc92b
		continue
Packit 2fc92b
		;;
Packit 2fc92b
Packit 2fc92b
		-g)
Packit 2fc92b
		chgrpcmd="$chgrpprog $2"
Packit 2fc92b
		shift
Packit 2fc92b
		shift
Packit 2fc92b
		continue
Packit 2fc92b
		;;
Packit 2fc92b
Packit 2fc92b
		-s)
Packit 2fc92b
		stripcmd="$stripprog"
Packit 2fc92b
		shift
Packit 2fc92b
		continue
Packit 2fc92b
		;;
Packit 2fc92b
Packit 2fc92b
		-z)
Packit 2fc92b
		instcmd="gzipcp"
Packit 2fc92b
		shift
Packit 2fc92b
		continue
Packit 2fc92b
		;;
Packit 2fc92b
Packit 2fc92b
		*)
Packit 2fc92b
		if [ x"$src" = x ]; then
Packit 2fc92b
			src="$1"
Packit 2fc92b
		else
Packit 2fc92b
			dst="$1"
Packit 2fc92b
		fi
Packit 2fc92b
		shift
Packit 2fc92b
		continue
Packit 2fc92b
		;;
Packit 2fc92b
	esac
Packit 2fc92b
done
Packit 2fc92b
Packit 2fc92b
if [ x"$src" = x ]; then
Packit 2fc92b
	echo "install-sh: No input file specified"
Packit 2fc92b
	exit 1
Packit 2fc92b
fi
Packit 2fc92b
Packit 2fc92b
if [ x"$dir_arg" != x ]; then
Packit 2fc92b
	dst="$src"
Packit 2fc92b
	src=""
Packit 2fc92b
Packit 2fc92b
	if [ -d "$dst" ]; then
Packit 2fc92b
		instcmd=:
Packit 2fc92b
	else
Packit 2fc92b
		instcmd=$mkdirprog
Packit 2fc92b
	fi
Packit 2fc92b
else
Packit 2fc92b
	# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
Packit 2fc92b
	# might cause directories to be created, which would be especially bad
Packit 2fc92b
	# if $src (and thus $dsttmp) contains '*'.
Packit 2fc92b
	if [ ! -f "$src" -a ! -d "$src" ]; then
Packit 2fc92b
		echo "install: $src does not exist"
Packit 2fc92b
		exit 1
Packit 2fc92b
	fi
Packit 2fc92b
Packit 2fc92b
	if [ x"$dst" = x ]; then
Packit 2fc92b
		echo "install: No destination specified"
Packit 2fc92b
		exit 1
Packit 2fc92b
	fi
Packit 2fc92b
Packit 2fc92b
	# If destination is a directory, append the input filename.
Packit 2fc92b
	if [ -d "$dst" ]; then
Packit 2fc92b
		dst="$dst/`basename $src`"
Packit 2fc92b
	fi
Packit 2fc92b
fi
Packit 2fc92b
Packit 2fc92b
## this sed command emulates the dirname command
Packit 2fc92b
dstdir="`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`"
Packit 2fc92b
Packit 2fc92b
# Make sure that the destination directory exists.
Packit 2fc92b
# This part is taken from Noah Friedman's mkinstalldirs script
Packit 2fc92b
Packit 2fc92b
# Skip lots of stat calls in the usual case.
Packit 2fc92b
if [ ! -d "$dstdir" ]; then
Packit 2fc92b
	defaultIFS='
Packit 2fc92b
	'
Packit 2fc92b
	IFS="${IFS-${defaultIFS}}"
Packit 2fc92b
Packit 2fc92b
	oIFS="${IFS}"
Packit 2fc92b
	# Some sh's can't handle IFS=/ for some reason.
Packit 2fc92b
	IFS='%'
Packit 2fc92b
	set - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
Packit 2fc92b
	IFS="${oIFS}"
Packit 2fc92b
Packit 2fc92b
	pathcomp=''
Packit 2fc92b
Packit 2fc92b
	while [ $# -ne 0 ] ; do
Packit 2fc92b
		pathcomp="${pathcomp}${1}"
Packit 2fc92b
		shift
Packit 2fc92b
Packit 2fc92b
		if [ ! -d "${pathcomp}" ]; then $doit $mkdirprog "${pathcomp}"; fi
Packit 2fc92b
Packit 2fc92b
		pathcomp="${pathcomp}/"
Packit 2fc92b
	done
Packit 2fc92b
fi
Packit 2fc92b
Packit 2fc92b
if [ x"$dir_arg" != x ]; then
Packit 2fc92b
	# Make a directory...
Packit 2fc92b
	$doit $instcmd $dst || exit 1
Packit 2fc92b
Packit 2fc92b
	# Allow chown/chgrp to fail, but log a warning
Packit 2fc92b
	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst || echo "warning: Unable to change owner of $dst!"; fi
Packit 2fc92b
	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst || echo "warning: Unable to change group of $dst!"; fi
Packit 2fc92b
	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst || exit 1; fi
Packit 2fc92b
else
Packit 2fc92b
	# Install a file...
Packit 2fc92b
	dstfile="`basename $dst`"
Packit 2fc92b
Packit 2fc92b
	# Check the destination file - for libraries just use the "-x" option
Packit 2fc92b
	# to strip...
Packit 2fc92b
	case "$dstfile" in
Packit 2fc92b
		*.a | *.dylib | *.sl | *.sl.* | *.so | *.so.*)
Packit 2fc92b
			stripopt="-x"
Packit 2fc92b
			;;
Packit 2fc92b
		*)
Packit 2fc92b
			stripopt=""
Packit 2fc92b
			;;
Packit 2fc92b
	esac
Packit 2fc92b
Packit 2fc92b
	# Make a temp file name in the proper directory.
Packit 2fc92b
	dsttmp="$dstdir/#inst.$$#"
Packit 2fc92b
Packit 2fc92b
	# Move or copy the file name to the temp name
Packit 2fc92b
	$doit $instcmd $src $dsttmp || exit 1
Packit 2fc92b
Packit 2fc92b
	# Update permissions and strip as needed, then move to the final name.
Packit 2fc92b
	# If the chmod, strip, rm, or mv commands fail, remove the installed
Packit 2fc92b
	# file...
Packit 2fc92b
	if [ x"$stripcmd" != x ]; then $doit $stripcmd $stripopt "$dsttmp" || echo "warning: Unable to strip $dst!"; fi
Packit 2fc92b
	if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp" || echo "warning: Unable to change owner of $dst!"; fi
Packit 2fc92b
	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp" || echo "warning: Unable to change group of $dst!"; fi
Packit 2fc92b
Packit 2fc92b
	trap "rm -f ${dsttmp}" 0 &&
Packit 2fc92b
	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; fi &&
Packit 2fc92b
	$doit $rmcmd -f "$dstdir/$dstfile" &&
Packit 2fc92b
	$doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
Packit 2fc92b
fi
Packit 2fc92b
Packit 2fc92b
exit 0