#!/bin/sh
# This script automatically test the given tool with the tool's test cases,
# reporting anything of interest.

# exits with 1 if there is nothing of interest
# exits with 0 if there is something interesting
# exits with 2 if an error occurred

# Limitations, don't run this multiple times in one day, unless the -noupdate
# flag is given.

# Written by Mike Stump <mrs@cygnus.com>

if [ $# = 2 ]; then
	if [ "$1" = -noupdate ]; then
		update=no
		shift
	fi
fi

if [ $# != 1 ]; then
	echo "Usage: [-noupdate] tool_name" >&2
	exit 2
fi

tool=$1

if [ -d "$HOME/devo/." ]; then
	DEVO=$HOME/devo
fi

cd $HOME/devo/deja-gnu || exit 2

tmp=/tmp/$tool-testing.$$a
tmp1=/tmp/$tool-testing.$$b
tmp2=/tmp/$tool-testing.$$c

./runtest -tool $tool -v -v -v -v -a -a >/dev/null 2>/dev/null

if [ "$update" = no ]; then
	now=$tool.sum
	before=`ls $tool.sum-* | tail -1`
else
	mv $tool.sum $tool.sum-`date '+%y%m%d'`
	mv detail/$tool.log detail/$tool.log-`date '+%y%m%d'`

	now=`ls $tool.sum-* | tail -1`
	before=`ls $tool.sum-* | tail -2 | sed 1q`
fi
trap "rm -f $tmp $tmp1 $tmp2" 0 1 2 3 5 9 13 15

if [ "$before" = "" ]; then
	echo "Need previous summary to compare against." >&2
	exit 2
fi

grep '^FAIL' "$now" | sed 's/^....:	//' >$tmp1
grep '^PASS' "$before" | sed 's/^....:	//' | comm $tmp1 -12 - >$tmp2

grep -s . $tmp2
if [ $? = 0 ]; then
	echo "Tests that now fail, but worked before:"
	echo
	cat $tmp2
	showchangelog=1
	echo
fi

grep '^PASS' "$now" | sed 's/^....:	//' >$tmp1
grep '^FAIL' "$before" | sed 's/^....:	//' | comm $tmp1 -12 - >$tmp2

grep -s . $tmp2
if [ $? = 0 ]; then
	echo "Tests that now work, but didn't before:"
	echo
	cat $tmp2
	echo
fi

grep '^FAIL' "$now" | sed 's/^....:	//' >$tmp1
grep '^[PF]A[SI][SL]' "$before" | sed 's/^....:	//' | comm $tmp1 -23 - >$tmp2

grep -s . $tmp2
if [ $? = 0 ]; then
	echo "New tests that FAIL:"
	echo
	cat $tmp2
	echo
fi

grep '^PASS' "$now" | sed 's/^....:	//' >$tmp1
grep '^[PF]A[SI][SL]' "$before" | sed 's/^....:	//' | comm $tmp1 -23 - >$tmp2

grep -s . $tmp2
if [ $? = 0 ]; then
	echo "New tests that PASS:"
	echo
	cat $tmp2
	echo
fi

grep '^[PF]A[SI][SL]' "$now" | sed 's/^....:	//' >$tmp1
grep '^[PF]A[SI][SL]' "$before" | sed 's/^....:	//' | comm $tmp1 -13 - >$tmp2

grep -s . $tmp2
if [ $? = 0 ]; then
	echo "Tests that have disappeared: (Eeek!)"
	echo
	cat $tmp2
	echo
fi


if [ "$tool" = g++ ]; then
	devoname=gcc
elif [ "$tool" = gcc ]; then
	devoname=gcc
elif [ "$tool" = gdb ]; then
	devoname=gdb
fi
if [ "$devoname" != "" ]; then
	if [ "$showchangelog" = 1 ]; then
		echo "Here is what's new in the ChangeLog:"
		echo
		diff -c detail/$devoname.ChangeLog $DEVO/$devoname/ChangeLog
		echo
	fi
	if [ "$update" != no ]; then
		# save the old ChangeLog as a reference for nest time
		cp -p $DEVO/$devoname/ChangeLog detail/$devoname.ChangeLog
	fi
fi

diff $before $now | grep -s .
if [ $? = 0 ]; then
	echo "Details:"
	echo
	diff $before $now
	echo
fi
