#!/bin/sh
#
# BUG?:
# following line results in filenames (in the package description files) with
# names such as '-g'. SDK, 4.1.96
# pkginstall install -m 755 -s -o bin -g bin bin-ELF/netromd /usr/local/sbin
#
if echo $* | grep -wq "\-\-version" ; then
	cat <<EOF 1>&2
pkginstall version 3.02
This script should be called up by \`mkpkg\`
EOF
	exit 0
fi

if echo $* | grep -wq "\-\-copyright" ; then
	sh "$0" --version
cat <<EOF 1>&2
----------------------------------------------------------------------------
This version written entirely by Stephen Kennedy <steve@sdk-software.com>
(G0LRI), 1995-1996. Concept by A Davison.

http://www.sdk-software.com/
Copyright (C) 1995-2000, S D K Software

All rights reserved.

Distribution and use of this script is permitted provided that the
following conditions are met:

 Distributions of this script must retain the above copyright
 notice, this list of conditions and the disclaimer below.
 If the script is modified it may be distributed provided this copyright
 notice is not removed and provided any changes you make are clearly marked.
 If the script is changed in any substantial way it must be distributed
 under a different name BUT with this copyright notice intact.
 Extracts of the script may be used in your work provided you include my
 copyright notice, list of conditions and disclaimer.

I also request, but do not insist, that the script is distributed together
with its manpage or, ideally, in its Slackware Package form as released by
me and that you send any suggested changes to me for inclusion in the
"official" version.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR \`\`AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

----------------------------------------------------------------------------
EOF
	exit 0
fi

if echo $* | grep -q "\-\-help" ; then # we'll call this an error
	echo -e "No help available - pkginstall should only be called by mkpkg.\\nSee manpage mkpkg(8) for more information." 1>&2
	exit 1
fi

if [ ! -d /usr/sbin ] || [ "$TARGET_DIR" -a "`echo "$TARGET_DIR" | tr -s /`" != "/" ] ; then
	echo "pkginstall cannot be used from the boot disk, sorry."
	exit 2
fi

# TODO: perhaps we shouldn't restrict ourselves to the install binary (many
# 'make install's use more than that). I think WE should only accept
# known options to 'install' though. -- SDK, 28.12.95

if [ $# -lt 3 ] ; then
# minimum of "pkginstall install source dest"
	echo "No/insufficient arguments specified.
Perhaps you meant \"pkginstall install <source>... <destination>\" ??" 1>&2
	exit $(expr 10 - $#)
fi

if [ "$MAKE_FILELIST" = "" ] ; then
	echo "MAKE_FILELIST variable is not set." 1>&2
	exit 1
fi

unset DIRECTORY_MODE

PKGINSTALL() {
	get_opts() {
		IFS="$OLDIFS"
		unset OLDIFS
		local OPTION
		while getopts cdg:m:o:s: OPTION ; do
			case $OPTION in
				d) DIRECTORY_MODE=on ;;
				c|g|m|o|s) : ;;
				*) echo "Unknown option." 1>&2
				exit 2 ;;
			esac
		done
	}

	shift
	OLDIFS="$IFS"
	IFS='
'
	get_opts $(for GET_OPTS in "$@" ; do echo $GET_OPTS | sed -e 's/--strip\>/-s/' -e 's/--directory\>/-d/' -e 's/--group=\|--group\>/-g/' -e 's/--mode=\|--mode\>/-m/' -e 's/--owner=\|--owner\>/-o/' ; done)
	shift $(expr $OPTIND - 1)
	unset get_opts GET_OPTS OPTIND

# mkpkg will tidy up messy filenames and do 'tr -s /' (no point in
# doing the same job twice)

	if [ "$DIRECTORY_MODE" ] ; then # installing directories
# the remaining positional params are just directories to be created
		for DIRECTORY in "$@"; do
			if [ "`echo "$DIRECTORY" | cut -b 1`" = "/" ] ; then # full path :-)
				echo "$DIRECTORY/" >> "$MAKE_FILELIST"
			else # relative path
				echo "`pwd`/$DIRECTORY" >> "$MAKE_FILELIST"
			fi
		done
	else # installing file(s)
# minimum of 2 arguments
		if [ $# = 2 ] ; then # last arg could be a dir or a filename
			if [ "`echo "$2" | cut -b 1`" = "/" ] ; then # full path
				if [ -d "$2" ] ; then # dir
					echo "$2/$1" >> "$MAKE_FILELIST"
				else # file
					echo "$2" >> "$MAKE_FILELIST"
				fi
			else # relative path
				if [ -d "$2" ] ; then # dir
					echo "`pwd`/$2/$1" >> "$MAKE_FILELIST"
				else # file
					echo "`pwd`/$2" >> "$MAKE_FILELIST"
				fi
			fi
		else # multiple files to install - last arg must be a dir
			get_dirname() {
				shift $(expr $# - 1)
				DIRNAME="$1"
				[ "`echo "$DIRNAME" | cut -b 1`" != "/" ] && DIRNAME="`pwd`/$DIRNAME" # relative path
			}
			get_dirname "$@"
			unset get_dirname
			until [ 1 = $# ] ; do
				echo "$DIRNAME/$1" >> "$MAKE_FILELIST"
				shift
			done
		fi
	fi
}

PKGINSTALL "$@"
unset PKGINSTALL
"$@"
exit $?
