#! /bin/ksh

PROGRAM="$1"
MAXROUND=$2
MAXBLOCK=$3
MAXTRY=$4
MAXKEY=$5

DIRDES=`pwd`

PROVERS="${DIRDES}/provers"

if test -z "${MAXROUND}"
then
MAXROUND=1
fi

if test -z "${MAXBLOCK}"
then
MAXBLOCK=1
fi

if test -z "${MAXTRY}"
then
MAXTRY=1
fi

if test -z "${MAXKEY}"
then
MAXKEY=1
fi

randblock > plaintxt

rm -f all_keys
rm -f temp.*

let K=1
while test ${K} -le ${MAXKEY}
do

#  echo  "** KEY = ${K}"
  randkey > key_des
  cat key_des >> all_keys
  key2bin
  
  let N=${MAXROUND}
  
  while test ${N} -le ${MAXROUND}
  do
#    echo "**** ROUND = ${N}"
    echo "Program; Round; Blocks; Try; Solutions; Keybits; Branches; Depth; Times" >> ${PROVERS}/result.K${K}.R${N}
    
#    echo "  DES"

    let BLOCKS=$((${MAXTRY}*${MAXBLOCK}))
    des -r${N} -b${BLOCKS} > /dev/null

    let T=1
    while test ${T} -le ${MAXTRY}
    do
        let BLOCKS=$((${T}*${MAXBLOCK}))
        let B=$(($(($((${T}-1))*${MAXBLOCK}))+1))
        let GOODBLOCKS=0
#        echo  "     generation of ${MAXBLOCK} blocks"
        cat /dev/null > all-clausole
        while test ${B} -le ${BLOCKS}
        do
#           echo "  BLOCCO ${B}"
            des2fml -r${N} -i${B} -p -c -f1 > /dev/null
            if test $? = 0
            then
                cat formulae >> all-clausole
                let GOODBLOCKS+=1
            fi
            let B+=1
        done

	mv all-clausole ${PROVERS}
	cp chiave ${PROVERS}
	cd ${PROVERS}
	
        if test ${MAXBLOCK} -le ${GOODBLOCKS}
        then
#            echo "     shortening"

            cat /dev/null > report-cnf

echo "all-clausole
clausole.cnf
cnf
" | fml2cnf  >> report-cnf

#	    echo "     searching"
            cat /dev/null > report
            ${PROGRAM} clausole.cnf > report | true

            SUCCESS=`cat report | grep -c "^SAT"`
            BRANCHES=`cat report | grep "Branch Points" | head -n 1 | sed "s/\([^0-9]\)//g"`
	    TIME=`cat report | grep "Elapsed Time" | head -n 1 | sed "s/\([^0-9\.]\)//g"`

	    DEPTH=`cat report | grep "^>" | sed "s/\([0-9]*\)/n/g" | sed "s/[> ]//g" | sort | tail -1 | wc -c | sed "s/\([^0-9]\)//g"`

            if test $SUCCESS != 0
	    then
#                echo "      done"

		grep "K" report | grep "=" >  assign

		rm -f temp.*

		grep "K" chiave | grep "=" > temp.0
		grep -v "K8="  temp.0 > temp.1
		grep -v "K16=" temp.1 > temp.2
		grep -v "K24=" temp.2 > temp.3
		grep -v "K32=" temp.3 > temp.4
		grep -v "K40=" temp.4 > temp.5
		grep -v "K48=" temp.5 > temp.6
		grep -v "K56=" temp.6 > temp.7
		grep -v "K64=" temp.7 > temp.8
		sort temp.8 > temp.key

		grep "K" report | grep "=" | head -n64 >  temp.0
		grep -v "K8="  temp.0 > temp.1
		grep -v "K16=" temp.1 > temp.2
		grep -v "K24=" temp.2 > temp.3
		grep -v "K32=" temp.3 > temp.4
		grep -v "K40=" temp.4 > temp.5
		grep -v "K48=" temp.5 > temp.6
		grep -v "K56=" temp.6 > temp.7
		grep -v "K64=" temp.7 > temp.8
		sort temp.8 > temp.found

		NBITS=`comm -1 -2 temp.key temp.found | wc -l | sed "s/\([^0-9]\)//g"`

	        FOUND=`cat report | grep -c "^SAT" | sed "s/\([^0-9]\)//g"`
		echo "${PROGRAM}; ${N}; ${MAXBLOCK}; ${T}; SI ${FOUND}; ${NBITS}; ${BRANCHES}; ${DEPTH}; ${TIME}" >> result.K${K}.R${N}
	    else
		echo "${PROGRAM}; ${N}; ${MAXBLOCK}; ${T}; SATO-FAIL; ; ${BRANCHES}; ${DEPTH}; ${TIME}" >> result.K${K}.R${N}
	    fi
	else
#	echo "   generation failed"
        echo "${PROGRAM}; ${N}; ${MAXBLOCK}; ${T}; GEN-FAIL" >> result.K${K}.R${N}
      fi  
      cd ${DIRDES}
      let T+=1
    done
    let N+=1
  done
  let K+=1
done

