#!/bin/sh

# Note:  use sed to strip blanks because not all systems have same version
# of diff.

if [ "x$GCTLIB" = "x" ]
then
  GCTLIB=`pwd`/../../../
  PATH=.:`pwd`/../../..:$PATH
  export PATH
fi

say()
{
	echo "$INDENT" " $1"
	make clean
}

# Silence silly out-of-date shared library messages (particular to my machine
# at the time these tests were written).
hush_gct()
{
 grep -v "has older revision"
}

# Check whether .o files have been (have not been) permanently
# created.
# C compilers are inconsistent about this, so only worry about what
# GCT does when used as a front end, or when cc explicitly used with -c.
# We do not care whether gct-ps-defs.o and gct-write.o are created
# when GCT implicitly does the work.
needo()
{
  if [ ! -f $1 ]
  then
    echo $1 is supposed to have been created.
  fi
}

noo()
{
  if [ -f $1 ]
  then
    echo $1 should not have been created.
  fi
}

echo "$INDENT" " If you use the GNU C compiler, you may see warnings about "
echo "$INDENT" " redefining __GNUC__ in tests 1 and 3.  These are harmless."

say test1
gct-init $GCTLIB
gct -B$GCTLIB -c -DTESTING main.c second.c noinst.c
gct -B$GCTLIB main.o second.o noinst.o 
a.out > result.LOG; diff result.LOG result.ref
greport -all GCTLOG > cover.LOG; diff cover.LOG cover.ref
needo main.o ; needo second.o ; needo noinst.o 

say test2
gct-init $GCTLIB
gct -B$GCTLIB -c -test-control gct-ctrl.old -DTESTING main.c second.c noinst.c
cc main.c second.c noinst.c gct-write.c gct-ps-defs.c > /dev/null
grestore
a.out > result.LOG; diff result.LOG result.ref
greport -all GCTLOG > cover.LOG; diff cover.LOG cover.ref


say test3
gct-init $GCTLIB
gct -B$GCTLIB -c -DTESTING main.c
gct -B$GCTLIB -c -DTESTING noinst.c
gct -B$GCTLIB -o tryit main.o noinst.o second.c
tryit > result.LOG; diff result.LOG result.ref
greport -all GCTLOG > cover.LOG; diff cover.LOG cover.ref
needo main.o ; needo noinst.o ; noo second.o 

say test4
gct-init $GCTLIB
gct -B$GCTLIB -c -test-control gct-ctrl.old -DTESTING main.c
gct -B$GCTLIB -c -test-control gct-ctrl.old -DTESTING noinst.c
gct -B$GCTLIB -test-control gct-ctrl.old -o tryit main.o noinst.o second.c gct-write.c gct-ps-defs.c
cc -o tryit main.c second.c noinst.c gct-write.c gct-ps-defs.c > /dev/null
grestore
tryit > result.LOG; diff result.LOG result.ref
greport -all GCTLOG > cover.LOG; diff cover.LOG cover.ref


say test5
gct-init $GCTLIB
cc -E -DTESTING main.c > main.i
cc -E -DTESTING noinst.c > noinst.i
gct -B$GCTLIB main.i noinst.i second.c 
a.out > result.LOG; diff result.LOG result.ref
greport -all GCTLOG > cover.LOG; diff cover.LOG cover.ref
noo main.o ; noo noinst.o; noo second.o

say test6
gct-init $GCTLIB
cc -E -DTESTING main.c > main.i
cc -E -DTESTING noinst.c > noinst.i
gct -B$GCTLIB -test-control gct-ctrl.old main.i noinst.i second.c gct-write.c gct-ps-defs.c
cc main.i noinst.i second.c gct-write.c gct-ps-defs.c > /dev/null
grestore
a.out > result.LOG; diff result.LOG result.ref
echo "==$INDENT" NOTE:  Old style instrumentation does not instrument  .i files.
# greport -all GCTLOG > cover.LOG; diff cover.LOG cover.ref


if [ "$GCT_UNPORTABLE_OK" = "true" ]
then
  # In cases of -c -o, check that the file that would have been the .o
  # file, had it not been for the -o argument, is not overwritten.

  say test7
  gct-init $GCTLIB
  echo "EXPECT THIS" > EXPECT.o
  cp EXPECT.o main.o
  cp EXPECT.o noinst.o
  gct -B$GCTLIB -c -o Xmain.o -DTESTING main.c
  gct -B$GCTLIB -c -o Xnoinst.o -DTESTING noinst.c
  gct -B$GCTLIB -c -DTESTING second.c
  gct -B$GCTLIB Xmain.o Xnoinst.o second.o
  a.out > result.LOG; diff result.LOG result.ref
  greport -all GCTLOG > cover.LOG; diff cover.LOG cover.ref
  needo Xmain.o ; needo Xnoinst.o ; 
  needo second.o
  diff EXPECT.o main.o
  diff EXPECT.o noinst.o

  # Make sure that -c -o main.o main.c results in a usable main.o
  say test7B
  gct-init $GCTLIB
  gct -B$GCTLIB -c -o main.o -DTESTING main.c
  gct -B$GCTLIB -c -o noinst.o -DTESTING noinst.c
  gct -B$GCTLIB -c -DTESTING second.c
  gct -B$GCTLIB main.o noinst.o second.o
  a.out > result.LOG; diff result.LOG result.ref
  greport -all GCTLOG > cover.LOG; diff cover.LOG cover.ref
  needo main.o ; needo noinst.o ; 
  needo second.o

  say test8
  gct-init $GCTLIB
  echo "EXPECT THIS" > EXPECT.o
  cp EXPECT.o main.o
  cp EXPECT.o noinst.o
  gct -B$GCTLIB -test-control gct-ctrl.old -c -o Xmain.o -DTESTING main.c
  gct -B$GCTLIB -test-control gct-ctrl.old -c -o Xnoinst.o -DTESTING noinst.c
  gct -B$GCTLIB -test-control gct-ctrl.old -c -DTESTING second.c
  gct -B$GCTLIB -test-control gct-ctrl.old Xmain.o Xnoinst.o second.o
  noo Xmain.o; noo Xnoinst.o ; noo second.o
  cc -c -o Xmain.o -DTESTING main.c
  cc -c -o Xnoinst.o -DTESTING noinst.c
  cc -c -DTESTING second.c
  cc Xmain.o second.o Xnoinst.o gct-write.c gct-ps-defs.c > /dev/null
  grestore
  a.out > result.LOG; diff result.LOG result.ref
  greport -all GCTLOG > cover.LOG; diff cover.LOG cover.ref
  needo Xmain.o ; needo Xnoinst.o ; needo second.o
  diff EXPECT.o main.o
  diff EXPECT.o noinst.o
else
  echo $INDENT "Tests using -c and -o are not portable"
fi

# Not quite what's wanted in test conditions, but as close as can come.
say test9
gct-init $GCTLIB
gct -B$GCTLIB -o tryit -DTESTING all.c 
tryit > result.LOG; diff result.LOG result.ref
greport -all GCTLOG > cover.LOG; diff cover.LOG cover.ref
noo all.o 

say test10
gct-init $GCTLIB
gct -B$GCTLIB -test-control gct-ctrl.old -o tryit -DTESTING all.c
cc -o tryit -DTESTING all.c gct-write.c gct-ps-defs.c > /dev/null
grestore
tryit > result.LOG; diff result.LOG result.ref
greport -all GCTLOG > cover.LOG; diff cover.LOG cover.ref

say test11
gct-init $GCTLIB
gct -B$GCTLIB -test-control gct-ctrl.old -DTESTING all.c 
cc -DTESTING all.c gct-write.c gct-ps-defs.c > /dev/null
grestore
a.out > result.LOG; diff result.LOG result.ref
greport -all GCTLOG > cover.LOG; diff cover.LOG cover.ref

# Not quite what test conditions ask for, but close as can come
say test12
gct-init $GCTLIB
gct -B$GCTLIB -DTESTING all.c 
a.out > result.LOG; diff result.LOG result.ref
greport -all GCTLOG > cover.LOG; diff cover.LOG cover.ref
noo all.o 

if [ "$GCT_UNPORTABLE_OK" = "true" ]
then
  say mixture
  gct-init $GCTLIB
  gct -B$GCTLIB -test-control gct-ctrl.mix -c -o main.o -DTESTING main.c
  gct -B$GCTLIB -test-control gct-ctrl.mix -c -o noinst.o -DTESTING noinst.c
  gct -B$GCTLIB -test-control gct-ctrl.mix -c -DTESTING second.c
  needo main.o; noo second.o ; needo noinst.o
  cc -c second.c
  gct -B$GCTLIB -test-control gct-ctrl.mix main.o noinst.o second.o 
  grestore
  a.out > result.LOG; diff result.LOG result.ref
  greport -all GCTLOG > cover.LOG; diff cover.LOG cover.ref
  needo main.o ; needo noinst.o ; needo second.o 
else
  echo $INDENT "Tests using -c and -o are not portable"
fi

# Passthrough failures propagate?
say ccexit
gct-init $GCTLIB
/bin/rm -f ccexit-out
gct -B$GCTLIB -test-control gct-ctrl -c noerror.c > ccexit-out 2>&1
if [ $? = 0 ]
then
  echo "FAILURE:  C compiler status not returned by GCC."
  echo "See file ccexit-out"
else
  /bin/rm -f ccexit-out
fi
noo noerror.o

# callcc replacement failures propagate?
say ccexit2 
gct-init $GCTLIB
/bin/rm -f ccexit2-out
touch main.o
chmod a-w main.o
gct -B$GCTLIB -test-control gct-ctrl -c main.c > ccexit2-out 2>&1
if [ $? = 0 ]
then
  echo "FAILURE:  C compiler status not returned by GCC."
  echo "See file ccexit2-out"
else
  /bin/rm -f ccexit2-out
fi
# C compilers seem to delete the output file, though I don't think
# this is required.
# needo main.o
# chmod a-w main.o

if [ "$GCT_UNPORTABLE_OK" = "true" ]
then
  say ccexit3
  gct-init $GCTLIB
  /bin/rm -f ccexit3-out
  gct -B$GCTLIB -test-control gct-ctrl -c error.c > ccexit3-out 2>&1
  if [ $? = 0 ]
  then
    echo "FAILURE:  C compiler status not returned by GCC."
    echo "See file ccexit3-out"
  else
    /bin/rm -f ccexit3-out
  fi
  noo error.o
else
  echo $INDENT "ccexit3 is not portable."
fi

# Pick a few.
if [ "$GCT_UNPORTABLE_OK" = "true" ]
then
	say test1-gcc
	gct-init $GCTLIB
	gct -B$GCTLIB -test-cc gcc -c -DTESTING main.c< second.c noinst.c 2>&1 | hush_gct
	gct -B$GCTLIB -test-cc gcc main.o second.o noinst.o 2>&1 | hush_gct
	./a.out > result.LOG; diff result.LOG result.ref
	greport -all GCTLOG > cover.LOG; diff cover.LOG cover.ref
	needo main.o ; needo second.o ; needo noinst.o

	say test3-gcc
	gct-init $GCTLIB
	gct -B$GCTLIB -test-cc gcc -c -DTESTING main.c 2>&1 | hush_gct
	gct -B$GCTLIB -test-cc gcc -c -DTESTING noinst.c 2>&1 | hush_gct
	gct -B$GCTLIB -test-cc gcc -o tryit main.o second.c noinst.o  2>&1 | hush_gct 
	tryit > result.LOG; diff result.LOG result.ref
	greport -all GCTLOG > cover.LOG; diff cover.LOG cover.ref
	needo main.o ; needo noinst.o ;
	noo second.o

	say test7-gcc
	gct-init $GCTLIB
	gct -B$GCTLIB -test-cc gcc -c -o Xmain.o -DTESTING main.c 2>&1 | hush_gct
	gct -B$GCTLIB -test-cc gcc -c -o Xnoinst.o -DTESTING noinst.c 2>&1 | hush_gct
	gct -B$GCTLIB -test-cc gcc -c -DTESTING second.c 2>&1 | hush_gct
	gct -B$GCTLIB -test-cc gcc Xmain.o Xnoinst.o second.o 2>&1 | hush_gct
	./a.out > result.LOG; diff result.LOG result.ref
	greport -all GCTLOG > cover.LOG; diff cover.LOG cover.ref
	needo Xmain.o ; needo Xnoinst.o ; noo main.o ; needo second.o

	# Not quite what test conditions ask for, but close as can come
	say test12-gcc
	gct-init $GCTLIB
	gct -B$GCTLIB -test-cc gcc -DTESTING all.c 2>&1 | hush_gct
	./a.out > result.LOG; diff result.LOG result.ref
	greport -all GCTLOG > cover.LOG; diff cover.LOG cover.ref
	noo all.o
else 
  echo $INDENT "Skipping remaining tests (not portable)"
fi

exit 0



