#!/usr/local/bin/bash

if [ $# = 2 ] ; then
    tarf=$1
    tests=$2
else
    echo echo $0 distribution.tar.gz tests
    exit 1
fi

sdir=$(pwd)

testdir=$(dirname $tarf)/test

mkdir -p $testdir

cp $tarf $testdir

cd $testdir

zcat $(basename $tarf) | tar -xf -

ccsldir=$(basename $tarf .tar.gz)

cd $ccsldir

MAKE=make
if $MAKE -version 2>&1 | grep GNU > /dev/null ; then
    echo > /dev/null
else
    MAKE=gmake
    if $MAKE -version 2>&1 | grep GNU > /dev/null ; then
	echo > /dev/null
    else
	echo GNU make not found
	exit
    fi
fi

sed -e "/^BINDIR/ s%=.*%=../bin%" \
    -e "/^LIBDIR/ s%=.*%=../lib%" \
    -e "/^USEOPT/ s/no/yes/" Makefile > Makefile.tmp

#diff Makefile Makefile.tmp

mv Makefile.tmp Makefile

echo %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
echo %%% $MAKE ccslc
echo %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

$MAKE all

echo %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
echo %%% $MAKE ccslc.opt
echo %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

$MAKE opt

$MAKE install

cd ..

mkdir Th

echo %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
echo %%% run ccslc
echo %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

bin/ccslc -batch $tests/*beh -d Th

DIFF=diff
if $DIFF -version 2>&1 | grep GNU > /dev/null ; then
    echo > /dev/null
else
    DIFF=/sun/theorie/hensel/diffutils-2.7/diff
    if $DIFF -version 2>&1 | grep GNU > /dev/null ; then
	echo > /dev/null
    else
	echo GNU diff not found
	exit
    fi
fi


echo %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
echo %%% $DIFF 
echo %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

echo $DIFF -c -I '^% date' -I '^% command line' 
echo	     -I '^% source file' \
echo 	     -I 'ccsllib : Library =' 
echo 	     -x '*bin' -x '*_adt.pvs' -x '.pvscontext' 
echo 	     -x '*~' -x '.#*' 
echo 	     $tests/StdTh Th

$DIFF -c -I '^% date' -I '^% command line' \
	-I '^% source file' \
	-I 'ccsllib : Library =' \
	-x '*bin' -x '*_adt.pvs' -x '.pvscontext' \
	-x '*~' -x '.#*' \
	$tests/StdTh Th


cd $sdir
