#!/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.UTF8
export LC_ALL=C.UTF8
# 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-20140101T000000Z
sources=tests/sid-sources-20140101T000000Z

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-native-arch=amd64 $ADTTMP/tmp/packages $ADTTMP/tmp/sources --strongtype > $ADTTMP/tmp/strongbuildgraph.xml
botch-buildgraph2srcgraph $ADTTMP/tmp/strongbuildgraph.xml --deb-native-arch=amd64 $ADTTMP/tmp/packages $ADTTMP/tmp/sources > $ADTTMP/tmp/strongsrcgraph.xml
botch-create-graph --deb-native-arch=amd64 $ADTTMP/tmp/packages $ADTTMP/tmp/sources --closuretype > $ADTTMP/tmp/closurebuildgraph.xml
botch-buildgraph2srcgraph $ADTTMP/tmp/closurebuildgraph.xml --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-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 --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


# 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/misc/tmp/* $ADTTMP/tmp/*; do basename "$f"; done | sort | uniq | while read f; do
	echo checking $f
	case "$f" in
		*.xml) while : ; do
			exit=0
			botch-graph-difference "tests/misc/tmp/$f" "$ADTTMP/tmp/$f" || exit=$?
			case "$exit" in
				139) continue;;
				0)   break;;
				*)   exit $exit;;
			esac
		done;;
		*) diff -u "tests/misc/tmp/$f" "$ADTTMP/tmp/$f" || exit 1;;
	esac
done
for f in tests/misc/out/* $ADTTMP/out/*; do basename "$f"; done | sort | uniq | while read f; do
	echo checking $f
	case "$f" in
		*.xml) while : ; do
			exit=0
			botch-graph-difference "tests/misc/out/$f" "$ADTTMP/out/$f" || exit=$?
			case "$exit" in
				139) continue;;
				0)   break;;
				*)   exit $exit;;
			esac
			done;;
		*) diff -u "tests/misc/out/$f" "$ADTTMP/out/$f" || exit 1;;
	esac
done
