#!/bin/sh
# $NetBSD: bulkbuild-rebuild,v 1.3 2008/09/16 18:21:30 joerg Exp $

. /usr/pkg/etc/pbulk.conf

set -e

if [ "${config_version}" != "0.68" ]; then
	echo "Your configuration has version ${config_version}."
	echo "This version of pbulk expects version 0.68."
	exit 1
fi

check_pkg() {
	if /usr/bin/awk '{ if ($0 == "PKGNAME='$1'") exit 1 }' < ${loc}/presolve; then
		echo "Package $1 not found, aborting."
		exit 1
	fi
}

pkg_failed() {
	echo "failed"
	exit 1
}

build_pkg() {
	printf "Building package %s... " "$1"
	/usr/bin/awk '{ if ($0 == "PKGNAME='$1'") p=1; \
	       else if ($0 ~ "^PKGNAME=") p=0; \
	       if (p) print $0 }' < ${loc}/presolve | \
	${pbuild_script} || pkg_failed "$1"
	echo "done"
}

if [ $# = 0 ]; then
	echo "Usage: $0 package ..."
fi

while [ $# != 0 ]; do
	check_pkg "$1"
	build_pkg "$1"
	shift
done
