#!/bin/sh
#
# Starlab make helper.
#
# Compile a C++ source file $1.C iff it is non-null.  Compile
# as ...$1 to avoid overwriting any existing object file.
# Second argument is where to put the executable.
# Third argument gives libraries to link with.
# Other arguments give compile command, switches, etc.
#
# NOTE: It might be best to make this a Bourne-shell script, for
#       the sake of efficiency.
#
# As of June 1998, we pass the full pathname of the source file to
# the compiler, so that run-time help information can be embedded
# in the source code.  The macro _SRC_ now becomes a reserved keyword.
#
# $PWD doesn't seem to do the right thing under csh and Dec UNIX
# at Komaba.  Using tcsh instead...  Alternatively, $cwd seems OK.

if [ $# -lt 3 ]; then
    echo "Usage: $0 srcfilestem  outfile.o  compileroption  compiler CCargs..." >&2
    exit 1
fi

#set -e

srcstem="$1"
outfile="$2"
huh="$3"
shift; shift; shift

real_src="$srcstem.C"
if [ -f "$real_src" -a -s "$real_src" ]; then   # if nonempty, compile it!

#   DON'T use PWD here!! -- still broken on some (Compaq) OSs... (Steve, 7/01)

    pwd=`pwd`

    src=...$real_src
    /bin/cp $real_src $src

#   Strip the $STARLAB_PATH piece of the source file, to make code
#   a little more portable.

    trunc_src=`echo $pwd/$real_src | sed -e "s@^${STARLAB_PATH}/@@"`

    date=`date +%a_%b_%d_%Y,_%T_%Z`

    echo "$* $src ($real_src) -o $outfile $huh -D_SRC_=\"$trunc_src\" \
					       -D_COMPILE_DATE_=\"$date\""
    $*           $src   -o "$outfile" $huh -D_SRC_=\"$trunc_src\" \
					     -D_COMPILE_DATE_=\"$date\"

#   (Doesn't seem to be easy to combine these repeated arguments
#    into a single string...)

    if [ $? = 0 ]; then
	touch $srcstem
    fi
    /bin/rm $src
else
    /bin/rm -f "$real_src"
fi
