#! /bin/sh
# automata.d/src and automata.d/bin file automata
#First set the path directory
DIR=`echo $0 | sed -e 's/[^\/]*$//'`
#echo $DIR
if test -f ${DIR}procrels
    then
		# found the necessary programs, so do nothing.
		:
	else
		# Cant find the programs used in automata so give a warning.
		echo Warning. Running ${DIR}automata, which calls ${DIR}procrels, ${DIR}diff1, etc.
		echo ${DIR}procrels not found
		echo Using versions in path. 
		DIR=
fi
#echo $DIR
PROCRELS=${DIR}procrels
DIFF1=${DIR}diff1
DOUBLEPAR=${DIR}doublepar
DIFF2=${DIR}diff2
WA=${DIR}wa
MULT=${DIR}mult
NEWDIFF=${DIR}newdiff
AXIOMS=${DIR}axioms
#Now find the options
procrelss=false procrelst=false diff1w=false diff1r=false keepfiles=true
KBparam=false getKBtreesize=false
gpname=
for i
do case $i in
  -*)
   flags="`echo ' '$i|awk '{for (j=2;j<=length($1);j++) print substr($1,j,1)}'`"
    for j in $flags
    do case $j in
         s) procrelss=true;;
         t) procrelst=true;;
         w) diff1w=true;;
         r) diff1r=true;;
         d) keepfiles=false;;
	 k) KBparam=true; getKBtreesize=true;;
         *) echo Usage:  automata "[-stwrd] [-k n]" gpname; exit 2;;
       esac
    done;;
  *) if test $getKBtreesize = true
     then getKBtreesize=false; KBtreesize=$i
     else
       if test $gpname
       then echo Usage:  automata "[-stwrf] [-k n]" gpname; exit 2
       else gpname=$i
       fi
     fi;;
   esac
done
  if test -z "$gpname"
    then  echo Usage:  automata "[-stwrf] [-k n]" gpname; exit 2
  fi
  procrelsflag=
  if test $procrelss = true
    then if test $procrelst = true
           then procrelsflag=" -s -t"
           else procrelsflag=" -s"
         fi
    else if test $procrelst = true
           then procrelsflag=" -t"
         fi
  fi
  if test $KBparam = true
  then procrelsflag=${procrelsflag}" -k "${KBtreesize}
  fi
  diff1flag=
  if test $diff1w = true
    then if test $diff1r = true
           then diff1flag=" -w -r"
           else diff1flag=" -w"
         fi
    else if test $diff1r = true
           then diff1flag=" -r"
         fi
  fi

echo Format 2.2

#Initial processing of relations.
	$PROCRELS $procrelsflag $gpname	
  if test "$?" != 0
    then if test $keepfiles = false
           then for i in procrels diff1* wa diff2 mult* newdiff doublepar
				do 
				    for j in ${gpname}.${i}
				    do
					if test -w $j
   						then rm $j
  					fi
				    done
				done
         fi
         exit 2
  fi
#nondiag true will mean that the equality automaton has a nonzero transition
#from some (a,b) with a!=b, and so it must be incorrect.
  nondiag=false
#The following commands get executed more than once if nondiag becomes true.
#In this case, the principal transition automaton must have been incomplete,
#and so we repeat diff1 (Knuth-Bendix) with a higher parameter.
  while
    while
      $DIFF1 $diff1flag $gpname
      if test "$?" != 0
        then if test $keepfiles = false
           then for i in procrels diff1* wa diff2 mult* newdiff doublepar
				do 
				    for j in ${gpname}.${i}
				    do
					if test -w $j
   						then rm $j
  					fi
				    done
				done
             fi
             exit 2
      fi
      if $nondiag = true
#if the diff1 output has not changed, then increase the parameter again.
        then  cmp -s ${gpname}.diff1 ${gpname}.diff1o
#The following 'else' command always returns 1, and so it ensures that we exit
#the inner while-loop immediately when nondiag is false.
        else test
      fi
    do
#Double Knuth-Bendix parameter and then repeat diff1.
      echo \#diff1 file was unchanged - doubling KBtreesize again
      $DOUBLEPAR $gpname
      if test "$?" != 0
        then if test $keepfiles = false
           then for i in procrels diff1* wa diff2 mult* newdiff doublepar
				do 
				    for j in ${gpname}.${i}
				    do
					if test -w $j
   						then rm $j
  					fi
				    done
				done
             fi
             exit 2
      fi
      mv ${gpname}.doublepar ${gpname}.procrels
    done
    nondiag=false

#Construct the word acceptor automaton.
	$WA $gpname
    if test "$?" != 0
      then if test $keepfiles = false
           then for i in procrels diff1* wa diff2 mult* newdiff doublepar
				do 
				    for j in ${gpname}.${i}
				    do
					if test -w $j
   						then rm $j
  					fi
				    done
				done
           fi
           exit 2
    fi
#We now construct the second difference transition automaton and the 
#multipliers. We make a partial test of their correctness, by checking that
#the automata (there exists b such that (a,b) in L(Mx)) is identical to the
#word acceptor for all generators  x. If not, then we can always find some
#new word differences, and we restart the while loop.
    while
#Construct second difference transition automaton.
      $DIFF2 $gpname
      if test "$?" != 0
        then if test $keepfiles = false
           then for i in procrels diff1* wa diff2 mult* newdiff doublepar
				do 
				    for j in ${gpname}.${i}
				    do
					if test -w $j
   						then rm $j
  					fi
				    done
				done
             fi
             exit 2
      fi
#Construct multipliers. The mult program returns 1 if the equality automaton
#has non-diagonal entries, as described above, in which case we will re-run
#diff1 with a higher parameter.
      $MULT $gpname
      case "$?" in
#Do the partial correctness tests on the multipliers. newdiff returns 0 if
#these tests fail.
        0) $NEWDIFF $gpname;
           case "$?" in
             0) test x;;
             1) test;;
             *) if test $keepfiles = false
           then for i in procrels diff1* wa diff2 mult* newdiff doublepar
				do 
				    for j in ${gpname}.${i}
				    do
					if test -w $j
   						then rm $j
  					fi
				    done
				done
                fi
                exit 2;;
           esac;;
#If nondiag has become true, then we ensure that we exit the inner while loop.
        1) nondiag=true; test;;
        *) if test $keepfiles = false
           then for i in procrels diff1* wa diff2 mult* newdiff doublepar
				do 
				    for j in ${gpname}.${i}
				    do
					if test -w $j
   						then rm $j
  					fi
				    done
				done
           fi
           exit 2;;
      esac
    do
#We must re-run diff2.
      mv ${gpname}.newdiff ${gpname}.diff1
      echo \#done a copy
    done
    test $nondiag = true
  do
#We must re-run diff1. Keep a copy of old output, for comparison.
    echo \#Equality automaton was not diagonal - doubling KBtreesize
    cp ${gpname}.diff1 ${gpname}.diff1o 
#Double the Knuth-Bendix parameter.
	$DOUBLEPAR $gpname
    if test "$?" != 0
      then if test $keepfiles = false
           then for i in procrels diff1* wa diff2 mult* newdiff doublepar
				do 
				    for j in ${gpname}.${i}
				    do
					if test -w $j
   						then rm $j
  					fi
				    done
				done
           fi
           exit 2
    fi
    mv ${gpname}.doublepar ${gpname}.procrels
  done
#Finally do the axiom checking.
  $AXIOMS $gpname
  case "$?" in
   0) if test $keepfiles = false
           then for i in procrels diff1* wa diff2 mult* newdiff doublepar
				do 
				    for j in ${gpname}.${i}
				    do
					if test -w $j
   						then rm $j
  					fi
				    done
				done
         fi
         exit 0;;
   1) if test $keepfiles = false
           then for i in procrels diff1* wa diff2 mult* newdiff doublepar
				do 
				    for j in ${gpname}.${i}
				    do
					if test -w $j
   						then rm $j
  					fi
				    done
				done
         fi
         exit 1;;
   *) if test $keepfiles = false
           then for i in procrels diff1* wa diff2 mult* newdiff doublepar
				do 
				    for j in ${gpname}.${i}
				    do
					if test -w $j
   						then rm $j
  					fi
				    done
				done
         fi
         exit 2;;
  esac
