#!/bin/sh
# $NetBSD: bulkbuild-restart,v 1.4 2016/12/18 22:38:16 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_list() {
	/usr/bin/awk -v presolve=${loc}/presolve 'BEGIN { 
		while (getline < presolve) {
			if ($0 ~ "^PKGNAME=") {
				cur = substr($0, 9)
				pkg[cur] = 1
			}
		}
	}
	{ if (!pkg[$0]) exit(1) }' "$1" || return 1
	return 0
}

if [ ! -e ${loc}/success -o ! -e ${loc}/error ]; then
	restart_build=no
elif ! check_list ${loc}/success; then
	restart_build=no
elif ! check_list ${loc}/error; then
	restart_build=no
else
	restart_build=yes
fi
if [ "$restart_build" = "no" ]; then
	${script_phase_pre_build}
	${script_phase_scan}
else
	case "${master_mode}" in
	[yY][eE][sS])
		${client_prepare}
		;;
	esac
fi
${script_phase_build}
${script_phase_report}
${script_phase_upload}
