#!/bin/sh
set -exu

# we have to export PYTHONHASHSEED because otherwise networkx will create
# content with random output order
#   https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=749710
#   https://github.com/networkx/networkx/issues/1181
export PYTHONHASHSEED=0
# to make the sort order in the tests locale independent
export LC_COLLATE=C.UTF-8
export LC_ALL=C.UTF-8
# because dose2html outputs utf8 on standard output
export PYTHONIOENCODING=utf-8

rm -rf $ADTTMP/out $ADTTMP/tmp
mkdir -p $ADTTMP/out
cp tests/cross-ma.diff $ADTTMP/out/ma.diff

botch-cross --debug --verbose --output=$ADTTMP/out --tmp=$ADTTMP/tmp --deb-drop-b-d-indep --optgraph --jobs=2 amd64 armhf tests/sid-amd64-packages-20160830T000000Z tests/sid-sources-20160830T000000Z

testname=cross
# the "while : ; do" loop around graph-difference is a temporary fix to work
# around random segmentation faults. See http://bugs.python.org/issue24605
for f in tests/$testname/tmp/* $ADTTMP/tmp/*; do basename "$f"; done | sort | uniq | while read f; do
	echo checking $f;
	case "$f" in
		*.xml)
			echo "+ botch-graph-difference \"tests/$testname/tmp/$f\" \"$ADTTMP/tmp/$f\"";
			while : ; do botch-graph-difference "tests/$testname/tmp/$f" "$ADTTMP/tmp/$f"; exit=$?; if [ $exit -eq 139 ]; then echo segfault; continue; fi; if [ $exit -ne 0 ]; then exit 1; else break; fi; done;;
		*)
			echo "+ diff -u \"tests/$testname/tmp/$f\" \"$ADTTMP/tmp/$f\"";
			diff -u "tests/$testname/tmp/$f" "$ADTTMP/tmp/$f" | head --lines 100 || exit 1;;
	esac;
done
for f in tests/$testname/out/* $ADTTMP/out/*; do basename "$f"; done | sort | uniq | while read f; do
	echo checking $f;
	case "$f" in
		*.xml)
			echo "+ botch-graph-difference \"tests/$testname/out/$f\" \"$ADTTMP/out/$f\"";
			while : ; do botch-graph-difference "tests/$testname/out/$f" "$ADTTMP/out/$f"; exit=$?; if [ $exit -eq 139 ]; then echo segfault; continue; fi; if [ $exit -ne 0 ]; then exit 1; else break; fi; done;;
		*)
			echo "+ diff -u \"tests/$testname/out/$f\" \"$ADTTMP/out/$f\"";
			diff -u "tests/$testname/out/$f" "$ADTTMP/out/$f" | head --lines 100 || exit 1;;
	esac;
done
