#!/bin/sh

# Runs all regression tests within src/tools/regression.
# Copy it to src/tools/regression and do:
#  ./test.sh make
#  ./test.sh run

MAKE="/usr/bin/make"
PROVE="/usr/local/bin/prove"
DIR=`dirname $0`
PWD=`pwd`
if [ "$DIR" = "." ]; then
	BASEDIR="$PWD"
else
	BASEDIR="$PWD/$DIR"
fi

# Only needs to run once
if [ "$1" = "make" ]; then
   echo "Running 'make':"
   makefiles=`find "$BASEDIR/" -name "Makefile" | sort`
   for m in $makefiles; do
      dir=`dirname $m`
      # Only run 'make' in directories that contain .t files
      dott=`find "$dir" -name "*.t"`
      if [ "$dott" = "" ]; then
         echo "   Skipping $dir (no *.t files in this directory)"
      else
         echo "   Building in $dir"
         cd "$dir"
         # Some of the makefiles start running tests. Ignore the output for now.
         $MAKE > /dev/null
      fi
   done

elif [ "$1" = "run" ]; then
   echo "Running tests:"
   # Find all dirs containing .t files
   tdirs=`find "$BASEDIR/" -name "*.t" | sed 's|[^/]*t$||' | sort -u`
   for t in $tdirs; do
      echo "   Proving $t"
      $PROVE $t
   done

else
   echo "Unknown option ('$1')"
fi

echo "Done"

