#!/bin/sh

source /etc/conf.d/eworm-repo
source /usr/lib/eworm/colors
source /usr/lib/eworm/noroot
source /usr/lib/eworm/nice

function help() {
	echo "usage: ${0} [OPTIONS]"
	echo
	echo "where OPTIONS are:"
	echo " -h          show this help"
	echo " -p PACKAGE  check PACKAGE"
}

COWER=""
COWER_MAINTAINER=""
COWER_OUTOFDATE=""

while getopts "hp:" opt; do
	case ${opt} in
		h)
			help
			exit 0
			;;
		p)
			PACKAGE="${OPTARG}"
			;;
	esac
done

if [ ! -x "/usr/bin/cower" ]; then
	echo "Sorry, cower is not installed." >&2
	exit 1
fi

if [ -z "${PACKAGE}" ]; then
	echo "No package given." >&2
	help
	exit 1
fi

COWER=($(cower --format="COWER_MAINTAINER='%m'\nCOWER_OUTOFDATE='%t'" -i ${PACKAGE}))
if [ ${?} -gt 0 ]; then
	echo -e "${bldylw}Warning:${default} Could not get information for package ${PACKAGE}! Does it still exist?"
	exit 1
fi

eval "${COWER[@]}"

if [ "${COWER_MAINTAINER}" = "(orphan)" ]; then
	echo -e "${bldylw}Warning:${default} package ${PACKAGE} is orphan!"
fi

if [ "${COWER_OUTOFDATE}" = "yes" ]; then
	echo -e "${bldylw}Warning:${default} package ${PACKAGE} is marked out-of-date!"
fi
