#!/bin/bash -e
DESCRIPTION="dput wrapper: Waits until package has been successfully build"

case "${1}" in
	"--help")
		cat <<EOF
Usage: mini-buildd-dput-wait-for-build <DPUT_OPTIONS>

${DESCRIPTION}

Deprecated: This should be updated to events, or removed.
EOF
	exit 0
esac

# Get values from dput args
_cv()
{
	grep --max-count=1 "^${1}:" "${2}" | cut -d" " -f2-
}
DIST=$(_cv "Distribution" "${@: -1}")
PACKAGE=$(_cv "Source" "${@: -1}")
VERSION=$(_cv "Version" "${@: -1}")
HOST="$(printf ${@: -2} | cut -d" " -f1)"

# Run dput
dput "$@"

# Poll for version in repo
SLEEP=30
printf "\nWaiting for ${PACKAGE}-${VERSION} to appear in ${DIST}@${HOST}:\n"
while true; do
	sleep ${SLEEP}
	if mini-buildd-client show ${HOST} ${PACKAGE} 2>/dev/null | grep "${DIST}.*${VERSION}"; then
		printf "\nOK, build\n"
		break
	else
		printf "*"
	fi
done
