#!/bin/sh
#
CPLUSPLUS="CC"
CC_OPTIONS=-I/usr/local/lib/tela/include" -DHAVE_STRING_H=1 -DHAVE_MEMORY_H=1 -DHAVE_ALLOCA_H=1 -DHAVE_ALLOCA=1 -DRETSIGTYPE=void -DTIMEVAL_HAS_USEC=1 -I.. -I../t -O2 -p -mips2"
BINDIR=/usr/local/lib/tela/iris4-bin
VERSION="1.0"
WHEN_COMPILED="CC on IRIX sumppu 4.0.5 06151813 IP17, Wed Aug  3 10:27:41 EDT 1994"
STD_CT_OBJECTS="std.o files.o plotmtv.o la.o fft.o dld.o  "
LIBRARIES="-lreadline -ltermcap /usr/local/lib/libdf3.1.a  /usr/local/lib/liblapack.a -lblas -lI77_mp -lmpc /usr/local/lib/libfftpack.a -lF77 -lI77 -lisam -lU77 -lm -lC -lmalloc"

# --This is not a complete script as yet.
# The Makefile inserts a few lines at the beginning, thus creating
# 'telakka' from this 'telakka.in'.
# Specifically, the variables CPLUSPLUS, CC_OPTIONS, VERSION, WHEN_COMPILED,
# STD_CT_OBJECTS, BINDIR and LIBRARIES must be defined.
#

nm_options=""

CTPP="$BINDIR/ctpp"
compileOnly=false
outputFileSpecified=false
outputFile=tela
echo=":"
VerboseMode=false
ccargs=""
for f in $STD_CT_OBJECTS
do
	ccargs="$ccargs ${BINDIR}/$f"
done

doatexit() {rm -f $TMPDIR/static$$.C $TMPDIR/static$$.o}
trap "rm -f $TMPDIR/static$$.C $TMPDIR/static$$.o" 1 2 3 15

#
# Parse telakka's own options and shift them away
# -----------------------------------------------
cnt=$#
while [ $cnt != 0 ]
do
    case ".$1" in
		.-h|.-help|.--help)
			echo 'Usage: telakka [-c] [-h] [-n] [-o file] [-v] [-V]'
			echo '       [--compile-only] [--help] [--nostandard]'
			echo '       [--output file] [--verbose] [--version]'
			echo '       [CFLAGS] [files]'
			echo 'is the TeLa Kernel Konstruction Accessory.'
			echo 'It compiles files and links them with appropriate libraries'
			echo 'to produce a new Tela executable kernel.'
			echo 'Options:'
			echo '-h, --help           display this message'
			echo '-c, --compile-only   compile only, do not link (see cc (1))'
			echo '-n, --nostandard     do not link "standard" libraries'
			echo '-o file, --output    specify output file; default "tela"'
			echo '-v, --verbose        verbose mode'
			echo '-V, --version        print version and exit'
			echo 'CFLAGS    any additional options for the C++ compiler'
			echo 'files     files ending with .ct, .o or .a.'
			exit
			;;
		.-c|.--compile-only)
			compileOnly=true
		    cnt=`expr $cnt - 1`
		    shift
			;;
		.-n|.--nostandard)
			ccargs=""
		    cnt=`expr $cnt - 1`
		    shift
			;;
		.-v|.--verbose)
			echo="echo"
			VerboseMode=true
		    cnt=`expr $cnt - 1`
		    shift
			;;
		.-V|.--version)
			echo "telakka the TeLa Kernel Konstruction Accessory, Version $VERSION."
			echo "For use with tela $VERSION compiled using $WHEN_COMPILED."
			exit
			;;
		.-o|.--output)
            if [ ".$2" = "." ] ; then
                echo "telakka: -o option but no filename"
                exit 1
            else
                outputFile=$2;
				outputFileSpecified=true
                cnt=`expr $cnt - 1`
                shift
            fi
		    cnt=`expr $cnt - 1`
		    shift
            ;;
        .*)
			cnt=0	# stop parsing of telakka's own options
            ;;
    esac
done

#
# Recognize other options and gather them in $ccoptions
# -----------------------------------------------------
ccoptions=""
for f in $*
do
	case "$f"
	in
	-l*|-L*) ;;
	-*)
		ccoptions="$ccoptions $f"
		;;
	*)	;;
	esac
done

#
# Compile all .ct files
# ---------------------
for f in $*
do
	case "$f"
	in
	*.ct)
		base=`basename $f .ct`
		dir=`dirname $f`
		currdir=`pwd`;
		cd $dir
		$echo $CTPP ${base}.ct
		$CTPP ${base}.ct
		cfile=${base}-.c
		$echo $CPLUSPLUS -c ${CC_OPTIONS} $ccoptions $cfile
		if $CPLUSPLUS -c ${CC_OPTIONS} $ccoptions $cfile; then :; else doatexit(); exit 1; fi
		rm $cfile
		if [ "$outputFileSpecified" = "true" -a "$compileOnly" = "true" ]; then
			mv ${base}-.o $currdir/$outputFile
		else
			mv ${base}-.o $currdir/${base}.o
		fi
		cd $currdir
		;;
	*)	;;
	esac
done

if [ "$compileOnly" = "true" ]; then exit; fi

#
# Gather everything into $ccargs, except that .ct -> .o
# -----------------------------------------------------
surely_ctobjects=""
for f in $*
do
	case "$f"
	in
	*.ct)
		basef=`basename $f .ct`
		ccargs="$ccargs ${basef}.o"
		surely_ctobjects="$surely_ctobjects $basef"
		;;
	*)
		ccargs="$ccargs $f"
		;;
	esac
done

#
# Create and compile temporary static.C
# -------------------------------------
#   We must use nm to find out whether a given object file
#   contains the symbol fninfo_objfilename or not.
ctobjects=""
staticC=${TMPDIR:=/tmp}/static$$.C
echo '#include "ctinfo.H"' >$staticC
echo 'extern "C" {' >>$staticC
for obj in $ccargs
do
	case "$obj"
	in
	*.o)
		bareobj=`basename $obj .o`
		# Determine if $bareobj.o has been compiled from a .ct file or not.
		# Algorithm: 1) if it is in list $surely_ctobjects, it is.
		# 2) otherwise use nm to find out.
		# The nm test is slow, therefore we try to avoid it.
		isctobject=false
		for surely_ctobject in $surely_ctobjects
		do
			if [ "$bareobj" = "$surely_ctobject" ]; then
				isctobject=true
				break
			fi
		done
		if [ "$isctobject" = "false" ]; then
			if nm $nm_options $obj | grep fninfo_$bareobj >/dev/null; then
				isctobject=true
				if [ "$VerboseMode" = "true" ]; then echo "$obj is C-tela file"; fi
			else
				if [ "$VerboseMode" = "true" ]; then echo "$obj is not C-tela file"; fi
			fi
		fi
		if [ "$isctobject" = "true" ]; then
			echo "extern TCFunctionInfo fninfo_${bareobj}[];" >>$staticC;
			ctobjects="$ctobjects $bareobj"
		fi
		;;
	*) ;;
	esac
done
echo "}" >>$staticC
echo '#define INSTALL(name) Install(fninfo_##name,(const unsigned char*)#name)' >>$staticC
echo 'void InstallStatic() {' >>$staticC
for obj in $ctobjects
do
	echo "INSTALL(${obj});" >>$staticC
done
echo '}' >>$staticC
(cd $TMPDIR; $CPLUSPLUS -c ${CC_OPTIONS} $ccoptions -I${BINDIR}/.. static$$.C)
rm $staticC

#
# Finally link everything together
# --------------------------------
$echo $CPLUSPLUS -o $outputFile $BINDIR/libtela.a $TMPDIR/static$$.o $ccargs $LIBRARIES
$CPLUSPLUS -o $outputFile $BINDIR/libtela.a $TMPDIR/static$$.o $ccargs $LIBRARIES

rm $TMPDIR/static$$.o

