#! /bin/ksh

if test -z "$1" -o "X$1" = "Xhelp"
then
echo
echo "Usage:"
echo " $0  <format>
echo       [<minrnd>-<maxrnd> [<minblck>-<maxblck> [<maxtry> [<maxkey>]]]]"
echo 
echo "Where:"
echo "<format> = format of the output formulae (one of the following)"
echo "     cnf = standard clauses as lists of integers"
echo "     fof = ground first order formuale in TPTP format"
echo "     nmr = ground first order formuale in SMODELS format"
echo "     fml = human readable (well...) format"
echo 
echo "Optional Parameters:"
echo "minrnd-maxrnd   = minimum and maximum number of rounds to generate"
echo "minblk-maxblk   = minimum and maximum number of blocks to generate"
echo "maxtry          = maximum number of instances to generate"
echo "maxkey          = maximum number of keys solution) to generate"
echo
exit 1
fi


##################################################################
PREFIX="$1"

MINROUND=`echo "$2" | sed "s/[^0-9\-]//g" | sed "s/-.*//"`
MAXROUND=`echo "$2" | sed "s/[^0-9\-]//g" | sed "s/.*-//"`

MINBLOCK=`echo "$3" | sed "s/[^0-9\-]//g" | sed "s/-.*//"`
ALLBLOCK=`echo "$3" | sed "s/[^0-9\-]//g" | sed "s/.*-//"`

MAXTRY=`echo "$4" | sed "s/[^0-9]//g"`
MAXKEY=`echo "$5" | sed "s/[^0-9]//g"`

DIRDES=`pwd`



###############################
#  Check consistency of input #
###############################

if test "X${PREFIX}" = "Xcnf"
then
FORMAT=1
elif test "X${PREFIX}" = "Xfof"
then
FORMAT=2
elif test "X${PREFIX}" = "Xnmr"
then
FORMAT=3
else
FORMAT=0
PREFIX="fml"
fi

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

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

if test MINROUND -ge MAXROUND
then
MAXROUND=${MINROUND}
fi

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

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

if test MINBLOCK -ge ALLBLOCK
then
ALLBLOCK=${MINBLOCK}
fi

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


###########################
#  Generation of Formulae #
###########################

echo "###### $0 #####"
echo "Generation of ${MAXTRY} instance(s) of DES formulae"
echo "Using ${MINROUND}..${MAXROUND} rounds of DES"
echo "Conjoining the encryptions of ${MINBLOCK}..${ALLBLOCK} blocks"
echo "With ${MAXKEY} key(s)"
echo "Output format in ${PREFIX}"
echo
echo "Please wait..."
echo

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 R=${MINROUND}
  
  while test ${R} -le ${MAXROUND}
  do

    let MAXBLOCK=${MINBLOCK}

    while test ${MAXBLOCK} -le ${ALLBLOCK}
    do

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

        let T=1
        while test ${T} -le ${MAXTRY}
        do
            let BLOCKS=$((${T}*${MAXBLOCK}))
            let B=$(($(($((${T}-1))*${MAXBLOCK}))+1))

            cat /dev/null > all-fml

            while test ${B} -le ${BLOCKS}
            do
                des2fml -r${R} -i${B} -p -c -f${FORMAT} > /dev/null
                cat formulae >> all-fml

                let B+=1
            done

            mv all-fml ${PREFIX}-r${R}-b${MAXBLOCK}-k${K}.${T}

            cp chiave key${K}

            let T+=1
        done
        echo DONE b${MAXBLOCK}
        let MAXBLOCK+=1
    done

    tar -cf ${PREFIX}-r${R}-k${K}.tar ${PREFIX}-r*-b*-k${K}.* key${K}
    rm -f ${PREFIX}-r*-b*-k${K}.*
    gzip ${PREFIX}-r${R}-k${K}.tar

    echo DONE r${R}
    let R+=1
  done


  rm key${K}

  echo DONE key${K}
  let K+=1
done
