#! /bin/sh
#
# squid-gw/http-reg
#
# Regression tests for HTTP parser
#
verbose=true

do_test()
{
  if [ ! -f $2.opt ] ;then
    echo "$2.opt: No such file or directory"
    exit 2
  fi
  options=`cat $2.opt`
  $verbose "Current test: $1 $2.exp $options"
  sed 's/$/
/' <$1 >reg.inp
  ./testhttp $options <reg.inp >reg.out 3>reg.err
  sed 's/$/
/' <$2.exp >reg.exp
  if cmp reg.out reg.exp ;then true
  else
    echo "Regression test $2 failed."
    echo "Input from:           $1"
    echo "Expected output from: $2.exp"
    echo "Input in:             reg.inp"
    echo "Expected output in:   reg.exp"
    echo "Actual output in:     reg.out"
    echo "Error page in:        reg.err"
    echo -n "Options:              "
    sed '2,$s/^/                      /' <$2.opt
    exit 1
  fi
  rm -f reg.out reg.err reg.exp reg.inp
}

if [ $# -ge 1 -a "$1" = "-v" ] ;then
  verbose=echo
  shift
fi
if [ $# -lt 1 ] ;then
  echo "Usage: http-reg <input_file>..."
  exit 1
fi

for i in $*
do
  if [ ! -f $i ] ;then
    echo "$i: No such file or directory"
    exit 2
  fi
  t=`echo $i|sed 's/\.inp$//'`
  if [ -f ${t}.exp ] ;then
    if [ -f ${t}a.exp ] ;then
      echo "Both ${t}.exp and ${t}a.exp exist!"
      exit 2
    fi
    do_test $i $t
  elif [ -f ${t}a.exp ] ;then
    for s in a b c d e f
    do
      test -f ${t}${s}.exp || break
      do_test $i ${t}${s}
    done
  else
    echo "${t}.exp: No such file or directory"
    exit 2
  fi
done
exit 0
