#! /bin/sh

rootdir="$(readlink -f "$1")"
pkgfile="$(readlink -f "$2")"
pkgname="$(basename "${pkgfile}" | sed 's@\.tgz$@@')"

if [ -z "${rootdir}" -o -z "${pkgfile}" ]; then
	echo 'Usage: install_pkg_local <rootdir> <pkgfile>' >&2
	exit 1
fi

if [ ! -f "${pkgfile}" ]; then
	echo "Unable to find package file: ${pkgfile}" >&2
	exit 1
fi

cd "${rootdir}"
tar -zxf "${pkgfile}" >/dev/null 2>/dev/null

mkdir -p "${rootdir}/var/adm/packages/" "${rootdir}/var/adm/scripts/" >/dev/null 2>/dev/null

cat << __EOF__ > "${rootdir}/var/adm/packages/${pkgname}"
PACKAGE NAME:     ${pkgname}
COMPRESSED PACKAGE SIZE:     $(du -k "${pkgfile}" | awk '{ print $1 }') K
UNCOMPRESSED PACKAGE SIZE:     $[$(gzip -dc "${pkgfile}" | wc -c 2>/dev/null | awk '{ print $1 }') / 1024] K
PACKAGE LOCATION: ${pkgfile}
PACKAGE DESCRIPTION:
$(grep '^[a-zA-Z0-9_+]*:' install/slack-desc)
FILE LIST:
$(tar -ztf "${pkgfile}")
__EOF__
if [ -f install/doinst.sh ]; then
	sh install/doinst.sh >/dev/null 2>/dev/null
	cp install/doinst.sh "${rootdir}/var/adm/scripts/${pkgname}"
	chmod +x "${rootdir}/var/adm/scripts/${pkgname}"
fi
rm -rf install
