#!/bin/bash

source ./include.sh

set +x

log_file=tests.comments
rm $log_file -f
test_num=1

function myEcho
{
  echo $*
  echo $* >> $log_file
}

function nextInputFile
{
  res=`printf "%03d" "$test_num"`  
  let test_num=$test_num+1
}

function nextTest
{
  nextInputFile
  inputFile=$res
  genLine=$1
  comment=$2
  myEcho "$inputFile : $genLine : $comment"
  bash -c "$genLine" > $inputFile || die "FAILED: generation"
  echo "$genLine" > ${inputFile}.l
  echo "$currentGroup" > ${inputFile}.g
  validationOutput=`./validate $currentGroup < $inputFile 2>&1` || die "FAILED: validation, message = $validationOutput"
}

function testFromFile
{
  fileName=$1
  shift
  comment=$*
  nextTest "cat $fileName | ./twf_" "$comment"
}

function setGroup
{
  if [ "$1" != "$currentGroup" ]; then
    myEcho "======== group $1 : $2 ======="
    currentGroup=$1
  fi
}

function echoTest
{
  nextTest "echo $* | ./twf"
}

echo "-- Compile generators"
compile_cpp twf_.c twf_ 
compile_cpp validate.cpp validate

for gen in gen* add*; do
    if [[ "$gen" != "gen_ans_cpp.sh" ]]; then
        compile_cpp $gen `basename $gen .cpp`;
    fi
done

echo "-- Generate tests"
rm -f ???
rm -f ???.a
rm -f ???.l

source script.sh

#nextTest "./gen_rand ... ... ..."

echo "-- Generate answers"
bash gen_ans_cpp.sh docheck || exit
