#
# genlookup - create lookup table data for the searchtest test harness
#
#help
#
# Usage: genlookup file recs percent strlen
#        create 'recs' records containing sorted records 'strlen'
#        long in 'file'. 'percent' records will have a gap in the
#        key sequence before them.
#
#end

if [ $# -lt 4 -o "$1" = '-?' ]
then
    script_help genlookup
    exit 1
fi

outf=$1.list

if [ -f $outf ]
then
    del $outf
fi
if [ $2 -gt 30000 -o $2 -lt 1 ]
then
    echo "No of records must be 1-30000"
    exit 1
fi

if [ $3 -lt 0 -o $3 -gt 100 ]
then
    echo "Gap percentage must be 0-100"
    exit 1
fi

case $3 in
    [0-9])      gapw=1 ;;
    [0-9][0-9]) gapw=2 ;;
    *)          echo "Percentage is not numeric"
                exit 1
esac
gapw=`expr $gapw - 1`
width=0
x=$2
while [ "$x" != '0' ]
do
    width=`expr $width + 1`
    x=`expr $x / 10`
done
width=`expr $width + $gapw`

if [ $4 -lt $width -o $4 -gt 80 ]
then
    echo "String length must be $width-80"
    exit 1
fi

gawk -v RECCOUNT=$2 -v RECLEN=$4 -v GAP=$3 '

BEGIN { fmtstr = sprintf("%%0%dd\n", RECLEN) 
        srand()
        gaps=GAP/100

        key=0;
        for(i=1; i<=RECCOUNT; i++)
        {
           if (rand()<=gaps)
           {
               key += 2
           }
           else
           {
               key += 1
           }
           printf(fmtstr, key)
        }
      }

' >$outf

echo "genlookup ended; output is in $outf"
exit
