#!/bin/sh


# This script looks at the *.sh files in misc,
# converts them to test scripts, tries them out,
# and -- if necessary -- uses them to create code
# to fill requirements that are not already filled
# by the standard library.
#
# It can create some .c files and can insert lines
# into gpklib.h .


echo > misc.c
echo > tmp.h
(
cd misc
echo > misc.log

for n in *.sh
do
  rm -f misctest.c
  echo "Trying $n" >>misc.log
  grep '^T' $n | sed 's/^T//' >misctest.c
  rm -f misctest.o misctest
  if make misctest >>misc.log && ./misctest >>misc.log
  then
    echo "File misc/$n is unnecessary."
    echo "File misc/$n is unnecessary." >>misc.log
    true
  else
    echo "File misc/$n is needed."
    echo "File misc/$n is needed." >>misc.log
    grep '^H' $n | sed -e 's/^H//' >misctest2.c
    grep '^C' $n | sed -e 's/^C//' >>misctest2.c
    grep '^T' $n | sed 's/^T//' >>misctest2.c
    echo '# misctest2' >>misc.log
    if make misctest2 >>misc.log && ./misctest2 >>misc.log
    then
        grep '^H' sh.foo | sed -e 's/^H//' >>../tmp.h
        grep '^C' sh.foo | sed -e 's/^C//' >>../misc.c
    else
	echo "# misctest 2 fails for $n"
	echo "# misctest 2 fails for $n" >>misc.log
	exit 2
    fi
    echo 'X-lines:' >>misc.log
    cat foo.misc >>misc.log
    echo 'Result of executing the X-lines' >>misc.log
    cat sh.foo >>misc.log
  fi
done
)


awk 'BEGIN {k=0;}
	F==1 {x[k]=$0; k++;}
	F==2 {if($0=="INSERT HERE") {
			for(i=0; i<k; i++)
				print x[i];
			}
		else
			print $0;
		}' F=1 tmp.h F=2 gpklib.in.h > gpklib.h
