#!/bin/sh

# GCT places instrumented source in a temporary like caa343.c.
# What it wants to do is compile that into an object file:
#
# cc -o original.o -c caa343.c
# 
# Unfortunately, many C compilers don't handle this correctly.
# We fake it with this routine.
#
# The first argument is the C compiler to call.
# The second argument is -o.
# The third argument is the intended .o file.
# The fourth argument is the intermediate .o file from the compiler.
# The remaining arguments are passed to the compiler.
#
# callcc cc -o original.o caa343.o -c caa343.c
#
# Note:  this routine should not be called if a temporary file was
# not used.

COMPILER=$1
shift

if [ "$1" != "-o" ]
then
  echo "GCT program error:  -o not passed."
  exit 1
fi
shift

INTENDED=$1
shift

INTERMEDIATE=$1
shift 

if [ "$INTERMEDIATE" = "" ]
then
  echo "GCT program error:  callcc called with too few arguments."
  exit 1
fi

if [ "$INTERMEDIATE" = "$INTENDED" ]
then
  echo "GCT program error:  callcc called when intermediate .o not needed."
  exit 1
fi


$COMPILER "$@"
STATUS=$?
if [ $STATUS != 0 ]
then
  exit $STATUS
elif [ -f "$INTENDED" -a ! -w "$INTENDED" ] 
then
  echo "$INTENDED cannot be overwritten" 1>&2
  exit 1
else
  # Use cp because GCT is expected to delete the temporary.
  /bin/cp $INTERMEDIATE $INTENDED
  exit
fi
