#! /bin/sh
# automata.d/src and automata.d/bin file automatac
#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 automatac so give a warning.
echo Warning. Running ${DIR}automatac, which calls ${DIR}procrels, ${DIR}diff1c, etc.
		echo ${DIR}procrels not found
		echo Using versions in path. 
		DIR=
fi
#echo $DIR
PROCRELS=${DIR}procrels
DIFF1=${DIR}diff1c
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
firsttime=true
timestop=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;;
         *) echo Usage:  automatac "[-stwrd]" gpname; exit 2;;
       esac
    done;;
  *) if test $gpname
       then echo Usage:  automatac "[-stwrf]" gpname; exit 2
       else gpname=$i
     fi;;
   esac
done
  if test -z "$gpname"
    then  echo Usage:  automatac "[-stwrf]" 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
  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
				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
  KBtidyint=30
  numreps=2
#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 diff1c (Knuth-Bendix) with a higher parameter.
#on first run of diff1c make sure we do not waste too much time

  while
    while
      if test $firsttime = true
        then $DIFF1 $diff1flag -n $numreps -t $KBtidyint -m 10 $gpname
             firsttime=false
        else $DIFF1 $diff1flag -n $numreps -t $KBtidyint $gpname
      fi
      case "$?" in
        0) timestop=false;;
        1) timestop=true;;
        *) if test $keepfiles = false
           then for i in procrels diff1* wa diff2 mult* newdiff
				do 
				    for j in ${gpname}.${i}
				    do
					if test -w $j
   						then rm $j
  					fi
				    done
				done
             fi
             exit 2;;
      esac

      if $nondiag = true
#if the diff1c 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
#Increase Knuth-Bendix parameters and then repeat diff1c.
      echo \#diff1c file was unchanged - increasing parameters again
      case $KBtidyint in
        30) KBtidyint=100; numreps=4;;
       100) KBtidyint=300; numreps=8;;
       300) echo Giving up!
             if test $keepfiles = false
             then for i in procrels diff1* wa diff2 mult* newdiff 
				do 
				    for j in ${gpname}.${i}
				    do
					if test -w $j
   						then rm $j
  					fi
				    done
				done
           fi
           exit 2;;
      esac
    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
				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
				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
#diff1c 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
				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
				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 diff1c. 
 echo \#Equality automaton was not diagonal - increasing Knuth-Bendix parameters
 cp ${gpname}.diff1 ${gpname}.diff1o
#Increase the Knuth-Bendix parameters.
      if test $timestop = true
        then timestop=false
      else
        case $KBtidyint in
          30) KBtidyint=100; numreps=4;;
         100) KBtidyint=300; numreps=8;;
         300) echo Giving up!
               if test $keepfiles = false
               then for i in procrels diff1* wa diff2 mult* newdiff
	  			do 
	  			    for j in ${gpname}.${i}
	  			    do
	  				if test -w $j
   	  					then rm $j
  	  				fi
	  			    done
	  			done
             fi
             exit 2;;
        esac
      fi
  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
				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
				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
				do 
				    for j in ${gpname}.${i}
				    do
					if test -w $j
   						then rm $j
  					fi
				    done
				done
         fi
         exit 2;;
  esac
