#!/bin/sh
# -*- ksh -*-

read REPLACE <../xxx.replace
DISPLAY_IMAGE="eeyes"
convert_ppm_to_png() { pnmtopng -compression 9 <$1 >$2 ; }
convert_png_to_ppm() { convert $1 ppm:$2 ; }
convert_pnm_to_ppm() { convert $1 ppm:$2 ; }


# $1 is the dumped image (ppm)
# $2 is the correct image (png)
compare_images() {
  if [ ! -f $2 ]
  then
    if [ -s $1 ]
    then
      convert_ppm_to_png $1 $2
    else
      cp $1 $2
    fi
    echo "$2 created"
  else
    if [ ! -s $1 -a ! -s $2 ]
    then
      echo "$1 is OK (empty)"
    else
      convert_pnm_to_ppm $1 xxx.1.ppm
      convert_png_to_ppm $2 xxx.2.ppm
      if cmp xxx.1.ppm xxx.2.ppm >/dev/null
      then
	echo "$1 is OK"
	rm $1
      else
	echo "$1 is not equal to $2"
	touch xxx.regtestfailed
	if [ "$REPLACE" = "yes" ]
	then
	  convert_ppm_to_png $1 $2
	else
	  if [ "$REPLACE" != "no" ]
	    then
	      $DISPLAY_IMAGE $1 &
	      PID1=$!
	      $DISPLAY_IMAGE $2 &
	      PID2=$!
	      pnmarith -subtract xxx.1.ppm xxx.2.ppm | pnmhisteq > xxx_dif.ppm
	      $DISPLAY_IMAGE xxx_dif.ppm &
	      PID3=$!
	      echo "Replace by the new version (yes|yesforall|no|noforall)? :"
	      read R
	      case "$R" in
		"yes")
		        convert_ppm_to_png $1 $2
		       ;;
		"yesforall")
			     REPLACE="yes"
			     cp $1 $2
			     ;;
		"noforall")
			    REPLACE="no"
			    ;;
	      esac
	      echo $REPLACE >../xxx.replace
	      kill $PID1 $PID2 $PID3
	  fi
	fi
      fi
    fi
  fi
}

if tty >/dev/null
then
  :
else
  REPLACE="no"
fi

I=`basename $(pwd)`

if fgrep -c "DO_NOT_MAKE_REGRESSION_TEST"  "$I.c" >/dev/null
then
  echo "$I : No regression test on this one"
  touch $I.regtest
  exit 0
fi

rm -f xxx.regtestfailed

J=0
while [ -f xxx.$I.$J.ppm ]
do
  if cmp xxx.$I.$J.ppm xxx_.$I.$J.ppm
  then
    rm xxx_.$I.$J.ppm
  else
    touch xxx.regtestfailed
    echo "$I : Not the same result with or without cache"
  fi
  compare_images xxx.$I.$J.ppm $I.$J.png
  J=`expr $J + 1`
done | tee xxx.$I.regtest

mv xxx.$I.regtest $I.regtest

