#!/bin/sh
# sh do-test.sh [min-corruptions(50) [initial-reps(50) [inc(def to 25)]]]

if [ "$1" = "-h" ] ; then
  echo "sh do-test.sh [min-corruptions(50) [initial-reps(50) [inc(def to 25)]]]"
  exit
fi
# find an appropriate rep count

MIN=${1:-50}
REP=${2:-50}
INC=${3:-25}
trap "rm /tmp/t-$$ ; exit 0" 0
trap "rm /tmp/t-$$ ; exit 1" 1 2 3 15

if [ -s test.log ] ; then
  echo "backing up previous test.log to test.log-"
  mv test.log test.log-
fi

echo "detail output saved in test.log"
echo "computing min reps in order to get $MIN mixed outputs"
while true ; do
  echo "trying nolock_raw with $REP reps"
  nolock_raw $REP | tee test.log |
  awk '
  $0 ~ /corrupt sequences found$/ { corrupt = $2 }
  END { print corrupt }
  ' - >/tmp/t-$$
  COUNT=`cat /tmp/t-$$`
  if [ $COUNT -lt $MIN ] ; then
    REP=`expr $REP + $INC`
  else
    break;
  fi
done

echo "$REP repetions satisfy criterion"
mv test-tmp test-tmp.nolock_raw

for x in nolock_buf \
  fcntl_raw fcntl_buf \
  flock_raw flock_buf \
  lockf_raw lockf_buf
do
 if [ -x $x ] ; then
   echo "testing $x"
   echo "" >>test.log
   echo "testing $x" >>test.log
   $x $REP >>test.log
   mv test-tmp test-tmp.$x
 fi
done
