#! /bin/sh
#
# $NetBSD: regpkg,v 1.2 2003/06/13 02:32:26 jwise 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

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
./makeplist $pkgset $pkg > $PLIST

# create the pkg tiny version
tinyvers=`awk '$1 ~ '/$pkg/' { print substr($0, length($1) + 2) }' versions`
case "$tinyvers" in
"")	echo "***WARNING ***: no version for \"$pkg\"" 2>&1
	tinyvers="System package for $pkg"
	;;
esac

# we need the last mtime of the files which make up the package
osvers=`sh ../../sys/conf/osrelease.sh`
t=$osvers.$tinyvers

# 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}

# create the comment
comment=`awk '$1 ~ '/$pkg/' { print substr($0, length($1) + 2) }' 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) }' 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 pkg tiny version
tinyvers=`awk '$1 ~ '/$pkg/' { print substr($0, length($1) + 2) }' versions`
case "$tinyvers" in
"")	echo "***WARNING ***: no version for \"$pkg\"" 2>&1
	tinyvers="System package for $pkg"
	;;
esac

# create the build information
echo "OPSYS=`uname -s`" > ${SYSPKGDIR}/+BUILD_INFO
echo "OS_VERSION=`uname -r`" >> ${SYSPKGDIR}/+BUILD_INFO
echo "OBJECT_FMT=`printf '.include <bsd.own.mk>\nall:\n\t@echo ${OBJECT_FMT}' | make -f- all`" >> ${SYSPKGDIR}/+BUILD_INFO
echo "MACHINE_ARCH=`printf '.include <bsd.own.mk>\nall:\n\t@echo ${MACHINE_ARCH}' | make -f- all`" >> ${SYSPKGDIR}/+BUILD_INFO
echo "MACHINE_GNU_ARCH=`printf '.include <bsd.own.mk>\nall:\n\t@echo ${MACHINE_GNU_ARCH}' | make -f- all`" >> ${SYSPKGDIR}/+BUILD_INFO
echo "_PKGTOOLS_VER=`pkg_create -V`" >> ${SYSPKGDIR}/+BUILD_INFO

# test for attributes
args=""
attrs=`awk '$1 ~ '/$pkg/' { print substr($0, length($1) + 2) }' 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 \
		> ${SYSPKGDIR}/+CONTENTS

