#!/bin/bash

set -e

PATH=/home/iwj/autopkgtest/runner:/usr/local/sbin:/usr/sbin:$PATH
export PATH

#any_tests_ok=false
any_tests_ok=true
source_tests_ok=true
binary_tests_ok=true
gutsy_tests_ok=true
max_test_count=10
go_anyway=false

for x in "$@"; do eval "$x"; done

fail () { printf >&2 "%s\n" "$*"; exit 127; }
progress () { printf "========== %s ==========\n" "$1"; }
x () { printf "+ %s\n" "$*"; "$@"; }
x2 () { printf >&2 "+ %s\n" "$*"; "$@"; }

wait_for () {
	remain=$1; interval=$2; on_not=$3; shift;shift;shift
	progress "await ($interval/$remain) $*"
	while true; do
		date
		if "$@"; then return; fi
		remain=$(( $remain - $interval ))
		if [ $remain -lt 0 ]; then fail "wait timed out for $*"; fi
		eval "$on_not"
		sleep $interval
	done
}

progress testing

xoptslist="source gutsy, target=source:binary gutsy, target=binary-amd64"

test_count=0

while $any_tests_ok && ($go_anyway || test -f go) && test $test_count -lt $max_test_count; do
	now=`date`
	printf "%s" "$now: "
	
	xopts="${xoptslist%%:*}"
	xopts_opts="${xopts#*,}"
	xoptslist="${xoptslist#*:}:$xopts"
	printf "considering (%s) " "$xopts_opts"

	anybroken=false
	for x in ${xopts%%,*}; do
		if eval \$${x}_tests_ok; then :; else
			printf "   skipping because of %s\n" "$x"
			anybroken=true
			break
		fi
	done
	if $anybroken; then continue; fi

	adt-testreport-onepackage /home/iwj/adt-play/onepackage-config \
		adtrun_extra_opts=--built-binaries-filter=__ \
		interactive=false $xopts_opts

	test_count=$(( $test_count + 1 ))
done

progress 'stopping'
