#
# gendata - create input data for the searchtest test harness
#
#help
#
# Usage: gendata file recs tabrecs keylen gap%
#        create 'recs' records containing random values to be 
#        looked up in the lookup table. 'recs' records will be 
#        generated starting with a record less than the lowest 
#        value in the table and ending with one greater than any
#        value in the table.
#
#        First create table data containing 'tabrecs' records
#        containing values 'keylen long with 'gap%' missing values.
#
#end

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

outf=$1.data
inf=$1.list

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

genlookup $1 $3 $5 $4

if [ ! -f $inf ]
then
    echo "can't find $inf"
    exit 1
fi

if [ -f $outf ]
then
    del $outf
fi

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

BEGIN {
          fmtstr = sprintf("%%0%dd\n", RECLEN) 
          printf(fmtstr, 0)
          max=0
      }

      { 
          if ($1>max)
          {
              maxval = $1
          }
      }

END   {
          finalval = maxval
          srand()
          lim = RECCOUNT - 2
          for(i=1; i<=lim; i++)
          {
              key = rand() * maxval
              if (key>finalval)
              {
                  finalval = key
              }
              printf(fmtstr, key)
          }
          printf(fmtstr, finalval + 1)
      }

' $inf >$outf

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