#!/bin/bash
# This file runs all Tralics tests
# Most file contain comment telling what happens
# The testb file requires bibtex.

# We make sure that we call the right binary by saying ../src/tralics
# We also specify where to search for configuration files.


# This checks that no error is signaled.
run_tralics() {
   local status
   echo testing $1
   ../src/tralics -confdir=../confdir $* >> Trace
   status=$?
   if [[ $status != 0 ]] ;  then
     echo "Tralics failed for $1"
     exit 1
   fi
}

# in Tralics \read16 reads from the tty, and we use echo to provide a value
# the line is also printed on stderr, that we redirect to /dev/null
run_tralics_with_param() {
   local status
   echo testing $1 with $2
   echo $2 | ../src/tralics -confdir=../confdir $1 >> Trace 2>/dev/null
   status=$?
   if [[ $status != 0 ]] ;  then
     echo "Tralics failed for $1"
     exit 1
   fi
}

# Checking errors: we check for a string in the transcript file
# The XML file should contain an item for each error
run_tralics_with_err() {
    local status
   echo testing $1
   ../src/tralics -confdir=../confdir $1 $3 >> Trace
   grep -q "$2" $1.log
   status=$?
   if [[ $status != 0 ]] ;  then
     echo "Tralics failed for $1"
     exit 1
   fi  
}

# 
compare_file () {
  local nb
  nb=4 
  if [ $# = 2 ]; then 
    nb=$2 
  fi
  tail -n +$nb $1 > fileA
  tail -n +$nb ../Modele/$1 > fileB
  diff -q fileA fileB 
  if [[ $? != 0 ]] ;  then
     diff $1 ../Modele/$1
     echo "Wrong translation of $1 "
     exit 1
   fi
}


echo "Tralics tests started at $(date) " > Trace
run_tralics hello
run_tralics hello1
run_tralics hello2.tex -config=hello.tcf -oe8
run_tralics hello3
run_tralics testb
run_tralics_with_err txtc "There was one error." -nozerowidthelt
run_tralics txtd
run_tralics torture
run_tralics tormath
run_tralics tormath1
run_tralics tormath2
run_tralics tormath3
run_tralics amsldoc
run_tralics testmath
run_tralics fptest
run_tralics mathmlc
run_tralics_with_err tpa "No value given for command "
run_tralics tpa2 -nofloathack
mv tpa2.xml tpa2x.xml
run_tralics tpa2 
run_tralics_with_err txerr "There were 8 errors." -oe8a
run_tralics testpack
run_tralics testpackii  -param bibtex_fields -year  -param bibtex_extensions -phdthesis
run_tralics bo
run_tralics testhtml
run_tralics testclass  -defaultclass=article 
run_tralics_with_err testkeyval "There were 32 errors." 
run_tralics xii
run_tralics_with_param comp_pi 100
run_tralics_with_err teststr "There were 10 errors." 
run_tralics_with_err testerr "Following images not defined: gee." "-year=2008"
run_tralics beauvilams2
run_tralics testbe
run_tralics testb1
mv testb1.xml testb0.xml
run_tralics testb1 -raw_bib
run_tralics exemple2003
mv exemple.xml exemple2003.xml
run_tralics exemple2005
mv exemple.xml exemple2005.xml
run_tralics exemple2006
mv exemple.xml exemple2006.xml
run_tralics exemple2007
mv exemple.xml exemple2007.xml
run_tralics exemple2008
mv exemple.xml exemple2008.xml
run_tralics ex_emple2008
mv ex_emple.xml ex_emple2008.xml
run_tralics exemple2009
mv exemple.xml exemple2009.xml


echo 
echo Comparing results with models

compare_file hello.xml
compare_file hello1.xml
compare_file hello2.xml 5
compare_file hello3.xml
compare_file fptest.xml 5
compare_file testb.xml
compare_file txtc.xml
compare_file txtd.xml
compare_file tpa.xml
compare_file tpa2.xml
compare_file tpa2x.xml
compare_file tormath.xml 5
compare_file tormath1.xml 5
compare_file tormath2.xml 5
compare_file tormath3.xml 5
compare_file amsldoc.xml
compare_file bo.xml 9 # there is a date on line 8
compare_file testhtml.xml
compare_file testclass.xml
compare_file torture.xml 6 
compare_file txerr.xml 
compare_file xii.xml 
compare_file testkeyval.xml
compare_file comp_pi.xml
compare_file testpack.xml
compare_file testpackii.xml
compare_file teststr.xml
compare_file testerr.xml
compare_file testbe.xml
compare_file testb0.xml 5
compare_file testb1.xml 5
compare_file exemple2003.xml 5
compare_file exemple2005.xml 5
compare_file exemple2006.xml 5
compare_file exemple2007.xml 5
compare_file exemple2008.xml 5
compare_file ex_emple2008.xml 5
compare_file exemple2009.xml 5
compare_file beauvilams2.xml 
compare_file testmath.xml 

echo done


