#!/bin/sh -e
if test \! -d package || test \! -d bsd
then
	echo "You are not in the right directory." 1>&2
	exit 100
fi
if [ $# -lt 3 ]
then
	echo "usage: $0 meta-dir root-dir basename" 1>&2
	exit 100
fi
create_pkgng() {
	pkg create -m "$1" -r "$2" -o ./
}
create_oldpkg() {
	local version="$(basename "$(pwd -P)" | sed -e 's/^.*-\([[:digit:]][[:alnum:].]*\)$/\1/')"
	local maintainer="`sed -n -e '/Maintainer:/s/^Maintainer://p' package/debian/control`"
	local comment="`head -n 1 $1/+COMMENT`"
	pkg_create -f "$1/+MANIFEST" -d "$1/+DESC" -D COMMENT="${comment}" -D MAINTAINER="${maintainer}" -D FULLPKGPATH="sysutils/$3" -p / -B "$2" "./$3-${version}.tgz"
}
create_pkgsrc() {
	local version="`/bin/pwd -P | sed -E 's/^.*-([[:digit:]][[:alnum:].]*)$/\1/'`"
	pkg_create -f "$1/+MANIFEST" -d "$1/+DESC" -c "$1/+COMMENT" -p "$2/" "./$3-${version}.tgz"
}
case "`uname`" in
(OpenBSD)	create_oldpkg "$@" ;;
(NetBSD)	create_pkgsrc "$@" ;;
(*)		create_pkgng "$@" ;;
esac
