#! /bin/sh
#
# $NetBSD: regpkg,v 1.8 2006/01/03 18:31:09 apb Exp $
#
# Copyright (c) 2003 Alistair G. Crooks.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#	This product includes software developed by Alistair G. Crooks.
#	for the NetBSD project.
# 4. The name of the author may not be used to endorse or promote
#    products derived from this software without specific prior written
#    permission.
#
# 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.
#

# Usage: regpkg set pkgname

rundir="$(dirname "$0")" # ${0%/*} isn't good enough when there's no "/"

SYSPKGROOT="${PKG_DBDIR:-/var/db/pkg}"
case "${SYSPKG_DBDIR}" in
"")	;;
*)	SYSPKGROOT="${SYSPKG_DBDIR}" ;;
esac

PLIST="/tmp/.PLIST.$$"

verbose=""
while [ $# -gt 2 ]; do
	case "$1" in
	-v)	verbose="$1" ;;
	*)	break ;;
	esac
	shift
done

if [ $# -ne 2 ]; then
	echo "Usage: regpkg set pkgname"
	exit 1
fi

pkgset="$1"

pkg="$2"

case "${verbose}" in
-v)	echo "Making PLIST for \"${pkgset}\" set and \"${pkg}\" package" ;;
esac

# create the skeleton PLIST from the pkgset description
"${rundir}/makeplist" "${pkgset}" "${pkg}" > "${PLIST}"

# create the pkg tiny version
case "${SYSPKG_DATES}" in
"")	tinyvers="$(${AWK} '$1 ~ '/"${pkg}"/' { print $2 }' "${rundir}/versions")"
	case "${tinyvers}" in
	"")	tinyvers=0
		;;
	esac
	if [ -f ../../sys/conf/osrelease.sh ]; then
		osvers="$(${HOST_SH} ../../sys/conf/osrelease.sh)"
		method=osreleases
	else
		osvers="$(${UNAME} -r)"
		method=uname
	fi
	t="${osvers}.${tinyvers}"
	;;
*)	args="$(${AWK} '
		/^@cwd/ { prefix = $2; next }
		/^@dirrm/ { next }
		{ printf("%s%s\n", prefix, $0) }' "${PLIST}")"
	# first try for any RCS identifiers in the files
	t=0
	case "${args}" in
	"")	;;
	*)	t="$(${IDENT} ${args} 2>/dev/null | ${AWK} '
			BEGIN { last = 0 }
			$2 == "crt0.c,v" { next }
			NF == 8 { t = $4; gsub("/", "", t); if (t > last) last = t; }
			END { print last }')"
		method=ident
		;;
	esac
	case "${t}" in
	0)	# we need the last mtime of the files which make up the package
		t="$(${ENV_CMD} TZ=UTC LOCALE=C ${LS} -lT ${args} | ${AWK} '
			BEGIN { newest = 0 }
			{
				t = $9 "";
				if ($6 == "Jan") t = t "01";
				if ($6 == "Feb") t = t "02";
				if ($6 == "Mar") t = t "03";
				if ($6 == "Apr") t = t "04";
				if ($6 == "May") t = t "05";
				if ($6 == "Jun") t = t "06";
				if ($6 == "Jul") t = t "07";
				if ($6 == "Aug") t = t "08";
				if ($6 == "Sep") t = t "09";
				if ($6 == "Oct") t = t "10";
				if ($6 == "Nov") t = t "11";
				if ($6 == "Dec") t = t "12";
				if ($7 < 10) t = t "0";
				t = t $7;
				#these next two lines add the 24h clock onto the date
				#gsub(":", "", $8);
				#t = sprintf("%s.%4.4s", t, $8);
				if (t > newest) newest = t;
			}
			END { print newest }')"
		method=ls
		;;
	esac
	;;
esac

# print version number that we're using
case "${verbose}" in
-v)	echo "${pkg} - ${t} version using ${method} method" ;;
esac

# create the directory and minimal contents
SYSPKGDIR="${SYSPKGROOT}/${pkg}-${t}"
if [ -d "${SYSPKGDIR}" ]; then
	echo "There is already a ${pkg}-${t} package installed (${SYSPKGDIR})"
	exit 1
fi

mkdir -p "${SYSPKGDIR}"

# add the dependencies
${AWK} '$1 ~ '/"${pkg}"/' { print $2 }' "${rundir}/deps" | ${SORT} | \
    ${AWK} '{ print "@pkgdep " $1 "-[0-9]*" }' >> "${PLIST}"

# create the comment
comment="$(${AWK} '$1 ~ '/"${pkg}"/' { print substr($0, length($1) + 2) }' \
	"${rundir}/comments")"
case "${comment}" in
"")	echo "***WARNING ***: no comment for \"${pkg}\"" 2>&1
	comment="System package for ${pkg}"
	;;
esac
echo "${comment}" > "${SYSPKGDIR}/+COMMENT"

# create the description
descr="$(${AWK} '$1 ~ '/"${pkg}"/' { print substr($0, length($1) + 2) }' \
	"${rundir}/descrs")"
case "${descr}" in
"")	echo "***WARNING ***: no description for \"${pkg}\"" 2>&1
	descr="System package for ${pkg}"
	;;
esac
echo "${descr}" > "${SYSPKGDIR}/+DESC"
${PRINTF} "\nHomepage:\nhttp://www.NetBSD.org/\n" >> "${SYSPKGDIR}/+DESC"

# create the build information
if [ x"${BUILD_INFO_CACHE}" = x ]; then
	{
	echo "OPSYS=$(${UNAME} -s)"
	echo "OS_VERSION=$(${UNAME} -r)"
	${MAKE} -f- all <<EOF
.include <bsd.own.mk>
all:
	@echo OBJECT_FMT=${OBJECT_FMT}
	@echo MACHINE_ARCH=${MACHINE_ARCH}
	@echo MACHINE_GNU_ARCH=${MACHINE_GNU_ARCH}
EOF
	echo "_PKGTOOLS_VER=$(${PKG_CREATE} -V)"
	} > "${SYSPKGDIR}/+BUILD_INFO"
else
	cp "${BUILD_INFO_CACHE}" "${SYSPKGDIR}/+BUILD_INFO"
fi

# test for attributes
args=""
attrs="$(${AWK} '$1 ~ '/"${pkg}"/' { print substr($0, length($1) + 2) }' \
	"${rundir}/attrs")"
for a in "${attrs}"; do
	case "${attrs}" in
	"")		;;
	preserve)	echo "${pkg}-${t}" > "${SYSPKGDIR}/+PRESERVE"
			args="${args} -n ${SYSPKGDIR}/+PRESERVE"
			;;
	esac
done

${PKG_CREATE} -v -c "${SYSPKGDIR}/+COMMENT" \
	-d "${SYSPKGDIR}/+DESC" \
	${args} \
	-f "${PLIST}" -l -b /dev/null -B "${SYSPKGDIR}/+BUILD_INFO" \
	-s /dev/null -S /dev/null -O "${pkg}-${t}.tgz" \
		> "${SYSPKGDIR}/+CONTENTS"

rm "${PLIST}"
