#!/bin/sh
# Edit this file ONLY in cpp/scripts/implement
#
# Copyright (C) 1989, Texas Instruments Incorporated. All rights reserved.
#
# This implements a parameterized class by splitting it up into one file per
# template.
#
# Created: LGO 09/18/89 -- Initial design and implementation
# Changed: LGO 10/11/89 -- Added the -F option
#
#Example:
# implement -g -I. -Llibapp.a -c List.h String.h -X"List<String>"
#
# The default file name is the name of the class with an index
# appended to it (e.g. Pair6.o).  The file name must be unique inside
# the library.  Because of this, you can over-ride the default name with
# the -o option (e.g. -oPairSymGen creates a PairSymGen6.o file).
#
# The -Q option puts us in "QUICK" mode, which
# compiles everything in a single file.
#
CC=CC2
CPP=icedir/cpp/cpp
CCArgs=
cppArgs=-D__cplusplus
Implement=
Includes=
arArgs=
noDelete=
name=
isFast=

rm -f /tmp/CCC.i /tmp/CCC.C
echo "// CCC implementation input file" > /tmp/CCC.C
for Arg do
	case $Arg in
	-o*)	name=`expr "$Arg" : '-o\(.*\)'`
		;;
	-X*)	Implement=`expr "$Arg" : '-X\(.*\)'`
		;;
	*.h)	echo "#include <$Arg>" >> /tmp/CCC.C
		;;
	-Q)	isFast=1
		;;
	-L*)	arArgs=`expr "$Arg" : '-L\(.*\)'`
		;;
	-C)	cppArgs="$cppArgs $Arg"; noDelete=1;
		;;
	-D*)	cppArgs="$cppArgs $Arg"
		;;
	-I*)	cppArgs="$cppArgs $Arg"
		;;
	*)	CCArgs="$CCArgs $Arg"
		;;
	esac
done
if [ "$arArgs" = "" ]; then
  echo You must specify a library with the -L option
  exit
fi
if [ "$Implement" = "" ]; then
  echo You must specify something to implement with the -X option
  exit
fi
# generate a default name
if [ "$name" = "" ]; then
  name=`expr "$Implement" : '\(.*\)<'`
fi
#
# Check for the fast track - a single compile
#
if [ "$isFast" = "1" ]; then
set ImplementWhat `expr "$Implement" : '\(.*\)<'`
echo "#pragma defmacro IMPLEMENT_1 implement delimiter=> lines $ImplementWhat" >> /tmp/CCC.C
echo "IMPLEMENT_1 $Implement" >> /tmp/CCC.C
(cd /tmp
 mv CCC.C $name.C
 cppC=$CPP
 export cppC
 echo $CC $cppArgs $CCArgs $name.C
 $CC $cppArgs $CCArgs $name.C
 echo ar r $arArgs /tmp/$name.o
 ar r $arArgs /tmp/$name.o
)
if [ "$noDelete" = "" ]; then
  rm -f /tmp/$name.C /tmp/$name.o
fi
exit
fi
#
# Else do it slow and efficient - one file per function
#
echo "#pragma defmacro IMPLEMENT_N implement_n delimiter=> lines" >> /tmp/CCC.C
echo 'MACRO EXPANDING EXPAND_IMPLEMENT(count)' >> /tmp/CCC.C
echo '{IMPLEMENT_N count' "$Implement" '}' >> /tmp/CCC.C
echo "EXPAND_IMPLEMENT(Count)" >> /tmp/CCC.C

Count=0
while	file=$name$Count
	cp /tmp/CCC.C /tmp/$file.c
	echo cpp -DCount=$Count $cppArgs /tmp/$file.c
	$CPP -DCount=$Count $cppArgs /tmp/$file.c > /tmp/$file.C
do	
	(cd /tmp; CC2 $CCArgs $file.C)
	echo ar r $arArgs /tmp/$file.o
	ar r $arArgs /tmp/$file.o
	if [ "$noDelete" = "" ]; then
	  rm /tmp/$file.c /tmp/$file.C /tmp/$file.o
	fi
	Count=`expr $Count + 1`
done
if [ "$noDelete" = "" ]; then
  rm -f /tmp/CCC.C /tmp/$file.c /tmp/$file.C
fi
