#!/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
mkdir -p $ADTTMP/tmp

packages=tests/sid-amd64-packages-20160830T000000Z
sources=tests/sid-sources-20160830T000000Z

grep-dctrl --exact-match --field Package build-essential $packages \
	| botch-latest-version - - > $ADTTMP/tmp/build-essential
botch-bin2src --deb-native-arch=amd64 $ADTTMP/tmp/build-essential $sources \
	> $ADTTMP/tmp/build-essential-src
botch-create-graph --deb-native-arch=amd64 --bg $sources $packages $ADTTMP/tmp/build-essential-src \
	> $ADTTMP/tmp/selfcontained_repo.xml
botch-buildgraph2packages $ADTTMP/tmp/selfcontained_repo.xml $packages \
	> $ADTTMP/tmp/packages
botch-bin2src --deb-native-arch=amd64 $ADTTMP/tmp/packages $sources \
	> $ADTTMP/tmp/sources
# test botch-calcportsmetric
botch-create-graph --deb-drop-b-d-indep --deb-native-arch=amd64 $ADTTMP/tmp/packages $ADTTMP/tmp/sources --strongtype > $ADTTMP/tmp/strongbuildgraph.xml
botch-buildgraph2srcgraph $ADTTMP/tmp/strongbuildgraph.xml --deb-drop-b-d-indep --deb-native-arch=amd64 $ADTTMP/tmp/packages $ADTTMP/tmp/sources > $ADTTMP/tmp/strongsrcgraph.xml
botch-create-graph --deb-drop-b-d-indep --deb-native-arch=amd64 $ADTTMP/tmp/packages $ADTTMP/tmp/sources --closuretype > $ADTTMP/tmp/closurebuildgraph.xml
botch-buildgraph2srcgraph $ADTTMP/tmp/closurebuildgraph.xml --deb-drop-b-d-indep --deb-native-arch=amd64 $ADTTMP/tmp/packages $ADTTMP/tmp/sources > $ADTTMP/tmp/closuresrcgraph.xml
botch-calcportsmetric $ADTTMP/tmp/strongsrcgraph.xml $ADTTMP/tmp/closuresrcgraph.xml > $ADTTMP/out/importance_metric.txt
# test botch-multiarch-interpreter-problem
dose-ceve --deb-drop-b-d-indep --deb-native-arch=amd64 -G pkg -T grml deb://$ADTTMP/tmp/packages debsrc://$ADTTMP/tmp/sources > $ADTTMP/tmp/ma_interpreter.pkggraph
botch-multiarch-interpreter-problem --packages $ADTTMP/tmp/packages $ADTTMP/tmp/ma_interpreter.pkggraph > $ADTTMP/out/ma_interpreter.txt
# test y-u-b-d-transitive-essential
botch-y-u-b-d-transitive-essential --debug --verbose --tmp $ADTTMP/tmp --output=$ADTTMP/out amd64 $ADTTMP/tmp/packages $ADTTMP/tmp/sources acl
# test botch-buildcheck-more-problems
botch-buildcheck-more-problems --checkonly=aster,bustle --deb-native-arch=amd64 --explain --failures $packages $sources > $ADTTMP/out/buildcheck.yaml || [ $? -eq 1 ]
# test botch-distcheck-more-problems
botch-distcheck-more-problems --checkonly=arista:amd64,chef:amd64 --deb-native-arch=amd64 --explain --failures deb://$packages > $ADTTMP/out/distcheck.yaml || [ $? -eq 1 ]
# test dose2html
botch-dose2html --srcsdir=$ADTTMP/out --wwwroot=$ADTTMP/out/ --packages=$packages $ADTTMP/out/distcheck.yaml $ADTTMP/out/distcheck.html
botch-dose2html --srcsdir=$ADTTMP/out --wwwroot=$ADTTMP/out/ --packages=$packages $ADTTMP/out/buildcheck.yaml $ADTTMP/out/buildcheck.html
# test networkx dot read/writ
dose-ceve --deb-drop-b-d-indep -c "src:acl" --deb-native-arch=amd64 -G pkg -T dot deb://$ADTTMP/tmp/packages debsrc://$ADTTMP/tmp/sources > $ADTTMP/tmp/aclbd.dot
botch-graph-shortest-path $ADTTMP/tmp/aclbd.dot --all --source __ID__:"src:acl (= 2.2.52-3)" --target __ID__:"multiarch-support:amd64 (= 2.23-5)" > $ADTTMP/out/acl-ma-path.dot

testname=misc
# 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 "+ ./tools/graph-difference.py \"tests/$testname/tmp/$f\" \"$ADTTMP/tmp/$f\"";
			while : ; do ./tools/graph-difference.py "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 -q \"tests/$testname/tmp/$f\" \"$ADTTMP/tmp/$f\"";
			diff -q "tests/$testname/tmp/$f" "$ADTTMP/tmp/$f" || 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 "+ ./tools/graph-difference.py \"tests/$testname/out/$f\" \"$ADTTMP/out/$f\"";
			while : ; do ./tools/graph-difference.py "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 -q \"tests/$testname/out/$f\" \"$ADTTMP/out/$f\"";
			diff -q "tests/$testname/out/$f" "$ADTTMP/out/$f" || exit 1;;
	esac;
done
