#!/bin/sh
#
######################################################################
#
# NAME
#	resolve-dependencies -- resolve package dependencies
#
# SYNOPSIS
#	resolve-dependencies
#
# DESCRIPTION
#	resolve-dependencies checks all entries in ${_DEPENDS_FILE}
#	for existance.  The best matching pattern is printed similiar
#	to list-dependencies
#
######################################################################

: ${ECHO:=echo}
: ${TEST:=test}
: ${TRUE:=true}

set -e

error_msg() {
	${ECHO} "ERROR:" "$*" 1>&2
}

find_best() {
	case $1 in
	bootstrap|tool)
		${HOST_PKG_INFO} -E "$2" || ${TRUE};;
	build|full|indirect-build|indirect-full)
		${PKG_INFO} -E "$2" || ${TRUE};;
	esac
}

while read type pattern dir; do
	[ "$type" != test ] || continue
	pkg=`find_best "$type" "$pattern"`
	case "$pkg" in
	"")
		error_msg "[resolve-dependencies] A package matching \`\`$pattern'' should"
		error_msg "    be installed, but one cannot be found.  Perhaps there is a"
		error_msg "    stale work directory for $dir?"
		exit 1
		;;
	*)
		${ECHO} "$type	$pattern	$pkg"
		;;
	esac
done < ${_DEPENDS_FILE}
