#!/bin/sh

# Test the status within Hugs of each module in the hierarchical libraries.
# This tests your installation, so you need to install first.

packages='haskell98 base haskell-src network'

case $# in
1)	;;
*)	echo "usage: $0 <directory where libraries lives>"
	exit 1 ;;
esac

srcdir=$1/libraries
if [ ! -d $srcdir ]; then
	echo "Can't find libraries in directory '$1'"
	exit 1
fi

tmpfile=/tmp/libs.$$
trap 'rm -f $tmpfile+ $tmpfile-; exit 0' 0 1 2 3 15

HUGSFLAGS="-P{Hugs}/libraries:{Hugs}/oldlib -W"
export HUGSFLAGS

test_module() {
	module=$1
	# The +. flag ensures that ERRORs will be at the start of a line
	hugs +98 +. $module </dev/null >$tmpfile+
	hugs -98 +. $module </dev/null >$tmpfile-
	if grep "^ERROR \"$module\" - Unable to open file \"$module\"" $tmpfile- >/dev/null
	then	echo "missing  $module"
	elif grep ^ERROR $tmpfile- >/dev/null
	then
		module2="`grep '^ERROR ' $tmpfile- | sed -e 's:.*"[^"]*/[a-z][^"/]*/\([^".]*\)\.[a-z]*".*:\1:' -e 's:/:.:g'`"
		if [ "$module" != "$module2" ]
		then	reason="imports $module2"
		else	reason="`sed -n '/^ERROR / s/.* - //p' $tmpfile-`"
		fi
		echo "*ERROR*  $module ($reason)"
	elif grep ^ERROR $tmpfile+ >/dev/null
	then
		module2="`grep '^ERROR ' $tmpfile+ | sed -e 's:.*"[^"]*/[a-z][^"/]*/\([^".]*\)\.[a-z]*".*:\1:' -e 's:/:.:g'`"
		if [ "$module" != "$module2" ]
		then	reason="imports $module2"
		else
			case "`sed -n '/^ERROR / s/.* - //p' $tmpfile+`" in
			"Syntax error in data type definition (unexpected \`.')")
				reason='rank-2 datatypes' ;;
			"Syntax error in type expression (unexpected \`.')")
				reason='rank-2 types' ;;
			"Haskell 98 does not support dependent parameters")
				reason='functional dependencies' ;;
			"Haskell 98 does not support multiple parameter classes")
				reason='multi-parameter type classes' ;;
			*)
				reason=unknown ;;
			esac
		fi
		echo "hugs -98 $module ($reason)"
	else	echo "hugs +98 $module"
	fi
}

cd $srcdir
for package in $packages
do	find $package -type f \( -name \*.hs  -o -name \*.lhs -o -name \*.y -o -name \*.ly -o -name \*.hsc \) |
		sed 's:^[^/]*/::
			s/\..*//
			s:/:.:g' |
		sort
done | grep -v '^Prelude$' | grep -v '^[GN]HC' | grep -v '\.[a-z]' | (
	cd /tmp
	while read module
	do
		test_module $module
	done
)
