#!/bin/sh
#
# new_link_templates:
#
#	This script generates template instantiation files instantiating
#       collections and containers for a given data type ($object_type).
#
#	Instantiated Templates:
#		ElementOps<Collection<$object_type> >
#		ElementOps<MSetBase<$object_type, SortedList<$object_type> > >
#		ElementOps<MSet<$object_type> >
#		ElementOps<SetBase<$object_type>, SortedList<$object_type> > >
#		ElementOps<Set<$object_type> >
#		ElementOps<SequenceBase<$object_type>, List<$object_type> > >
#		ElementOps<Sequence<$object_type> >
#		ElementOps<Container<$object_type> >
#		ElementOps<List<$object_type> >
#		ElementOps<DList<$object_type> >
#		ElementOps<SortedList<$object_type> >
#		ElementOps<Array<$object_type> >
#		ElementOps<SortedArray<$object_type> >
#
#		Collection<$object_type>
#		MSetBase<$object_type, SortedList<$object_type> >
#		MSetBase<$object_type, List<$object_type> >
#		MSet<$object_type>
#		SetBase<$object_type>, SortedList<$object_type> >
#		Set<$object_type>
#		SequenceBase<$object_type>, List<$object_type> >
#		Sequence<$object_type>
#		Container<$object_type>
#		SimpleContainer<$object_type>
#		List<$object_type>
#		DList<$object_type>
#		SortedList<$object_type>
#		Array<$object_type>
#		SortedArray<$object_type>
#		Iterator<$object_type>
#
#		ElementOps<$object_type>  (unless the user says not to)
#					  for example, if $object_type is
#					  itself a container or collection,
#					  then ElementOps<$object_type> was
#					  instantiated by the call to this
#					  script with the element type stored
#					  by the container or collection
#
#	The script takes various options to handle special cases that arise
#	during the instantiation of collection and container templates
#
#
if [ $# -lt 2 ]
then
	echo -n "usage: new_link_templates [-instantiate-attribute-only] [-instantiate-elementops-only] -t <object type> -d <directory> "
	echo "[-element-outputop-prototype]"
	echo "[-I \"full path of object include file\"]*"
	echo "[-p \"full function prototype\"]*"
	echo "ex   : new_link_templates -t \"Vertex*\" -d graph -I \"LINK/graph/Vertex.h\""
	echo "ex   : new_link_templates -t \"int\" -d basic"
	echo "ex   : new_link_templates -instantiate-attribute-only -t \"int\" -d basic"
	exit
fi

object_type=""
extra_include=""
extra_prototype=""
directory=""
inst_attribute_only="no"
inst_elementops_only="no"
inst_elementops="yes"
element_outputop_prototype="no"

while [ $# -gt 0 ]
do
	case "$1" in
	-instantiate-elementops-only)	inst_elementops_only="yes";;
	-dont-instantiate-elementops)	inst_elementops="no";;
	-instantiate-attribute-only)	inst_attribute_only="yes";;
	-element-outputop-prototype)	element_outputop_prototype="yes";;
	-t)	shift; object_type="$1 ";;
	-d) 	shift; directory=$1;;
	-I)	shift; extra_include="-I $1 $extra_include";;
	-p)	shift; extra_prototype="-p \"$1\" $extra_prototype";;
	esac
	shift
done

quoted_object_type="\"$object_type\""

if [ $element_outputop_prototype = "yes" ]
then
	extra_prototype="-p \"ostream& operator<<(ostream&, const $object_type&);\" $extra_prototype"
fi

con_op_prot='-p "ostream&operator<<(ostream&,const Container<$object_type>&);"'
col_op_prot='-p "ostream&operator<<(ostream&,const Collection<$object_type>&);"'
ElementOps_incl='"LINK/basic/ElementOps.h"'
SimpleContainer_incl='"LINK/basic/Container.h"'
Container_incl='"LINK/basic/Container.h"'
Collection_incl='"LINK/basic/Collection.h"'
MSet_incl='"LINK/basic/MSet.h"'
MSetBase_incl='"LINK/basic/MSetBase.h"'
Set_incl='"LINK/basic/Set.h"'
SetBase_incl='"LINK/basic/SetBase.h"'
Sequence_incl='"LINK/basic/Sequence.h"'
SequenceBase_incl='"LINK/basic/SequenceBase.h"'
DList_incl='"LINK/basic/DList.h"'
List_incl='"LINK/basic/List.h"'
SortedList_incl='"LINK/basic/SortedList.h"'
Array_incl='"LINK/basic/Array.h"'
SortedArray_incl='"LINK/basic/SortedArray.h"'
Iterator_incl='"LINK/basic/Iterator.h"'
Attribute_incl='"LINK/graph/Attribute.h"'

Collection_obj='"Collection<$object_type>"'
MSet_obj='"MSet<$object_type>"'
Set_obj='"Set<$object_type>"'
Sequence_obj='"Sequence<$object_type>"'
MSetBase_obj='"MSetBase<$object_type>"'
SetBase_obj='"SetBase<$object_type>"'
SequenceBase_obj='"SequenceBase<$object_type>"'
Container_obj='"Container<$object_type>"'
SimpleContainer_obj='"SimpleContainer<$object_type>"'
List_obj='"List<$object_type>"'
DList_obj='"DList<$object_type>"'
SortedList_obj='"SortedList<$object_type>"'
Array_obj='"Array<$object_type>"'
SortedArray_obj='"SortedArray<$object_type>"'

if [ $inst_attribute_only = "yes" ]
then
cmd="$LINK_BASE/bin/instantiate -t $quoted_object_type -c Attribute \
             -II $Attribute_incl -d $directory  \
             $extra_include $extra_prototype"
eval $cmd
exit
fi

if [ $inst_elementops = "yes" ]
then
cmd="$LINK_BASE/bin/instantiate -t $quoted_object_type -c ElementOps \
             -II $ElementOps_incl -d $directory  \
             $extra_include $extra_prototype"
eval $cmd
fi

if [ $inst_elementops_only = "yes" ]
then
	exit
fi


cmd="$LINK_BASE/bin/instantiate -t $Container_obj -c ElementOps \
             -II $ElementOps_incl -d $directory $con_op_prot
             -I $Container_incl $extra_include $extra_prototype"
eval $cmd
cmd="$LINK_BASE/bin/instantiate -t $Collection_obj -c ElementOps \
             -II $ElementOps_incl -d $directory $col_op_prot \
             -I $Collection_incl $extra_include $extra_prototype"
eval $cmd
cmd="$LINK_BASE/bin/instantiate -t $MSet_obj -c ElementOps \
             -II $ElementOps_incl -d $directory $col_op_prot \
             -I $MSet_incl $extra_include $extra_prototype"
eval $cmd
cmd="$LINK_BASE/bin/instantiate -t $Set_obj -c ElementOps \
             -II $ElementOps_incl -d $directory $col_op_prot \
             -I $Set_incl $extra_include $extra_prototype"
eval $cmd
cmd="$LINK_BASE/bin/instantiate -t $Sequence_obj -c ElementOps \
             -II $ElementOps_incl -d $directory $col_op_prot \
             -I $Sequence_incl $extra_include $extra_prototype"
eval $cmd
cmd="$LINK_BASE/bin/instantiate -t $List_obj -c ElementOps \
             -II $ElementOps_incl -d $directory $con_op_prot \
             -I $List_incl $extra_include $extra_prototype"
eval $cmd
cmd="$LINK_BASE/bin/instantiate -t $SortedList_obj -c ElementOps \
             -II $ElementOps_incl -d $directory $con_op_prot \
             -I $SortedList_incl $extra_include $extra_prototype"
eval $cmd
cmd="$LINK_BASE/bin/instantiate -t $Array_obj -c ElementOps \
             -II $ElementOps_incl -d $directory $con_op_prot \
             -I $Array_incl $extra_include $extra_prototype"
eval $cmd
cmd="$LINK_BASE/bin/instantiate -t $SortedArray_obj -c ElementOps \
             -II $ElementOps_incl -d $directory $con_op_prot \
             -I $SortedArray_incl $extra_include $extra_prototype"
eval $cmd
cmd="$LINK_BASE/bin/instantiate -t $DList_obj -c ElementOps \
             -II $ElementOps_incl -d $directory $con_op_prot \
             -I $DList_incl $extra_include $extra_prototype"
eval $cmd

#---------------------------------------------------------------------------
#-------- Containers and Collections ---------------------------------------
#---------------------------------------------------------------------------
#-------------------------------------------ElementOps *is* instantiated ---
#-------------------------------------------along with each.             ---
#---------------------------------------------------------------------------

cmd="$LINK_BASE/bin/instantiate -t $quoted_object_type -c SimpleContainer \
             -II $Container_incl -d $directory \
             $extra_include $extra_prototype"
eval $cmd
cmd="$LINK_BASE/bin/instantiate -t $quoted_object_type -c Container \
             -II $Container_incl -d $directory \
             -instantiate-output-operator $extra_include $extra_prototype"
eval $cmd
cmd="$LINK_BASE/bin/instantiate -t $quoted_object_type -c Collection \
             -II $Collection_incl -d $directory \
             -instantiate-output-operator $extra_include $extra_prototype"
eval $cmd
cmd="$LINK_BASE/bin/instantiate -t $quoted_object_type -c MSet \
             -II $MSet_incl -d $directory \
             $extra_include $extra_prototype"
eval $cmd
cmd="$LINK_BASE/bin/instantiate -t $quoted_object_type -c Set \
             -II $Set_incl -d $directory \
             $extra_include $extra_prototype"
eval $cmd
cmd="$LINK_BASE/bin/instantiate -t $quoted_object_type -c Sequence \
             -II $Sequence_incl -d $directory \
             $extra_include $extra_prototype"
eval $cmd
cmd="$LINK_BASE/bin/instantiate -t $quoted_object_type \
             -t2 \"SortedList<$object_type>\" -c MSetBase \
             -II $MSetBase_incl -I $SortedList_incl -d $directory \
             $extra_include $extra_prototype"
eval $cmd
cmd="$LINK_BASE/bin/instantiate -t $quoted_object_type \
             -t2 \"List<$object_type>\" -c MSetBase \
             -II $MSetBase_incl -I $List_incl -d $directory \
             $extra_include $extra_prototype"
eval $cmd
cmd="$LINK_BASE/bin/instantiate -t $quoted_object_type \
             -t2 \"SortedList<$object_type>\" -c SetBase \
             -II $SetBase_incl -I $SortedList_incl -d $directory \
             $extra_include $extra_prototype"
eval $cmd

cmd="$LINK_BASE/bin/instantiate -t $quoted_object_type \
             -t2 \"List<$object_type>\" -c SequenceBase \
             -II $SequenceBase_incl -I $List_incl -d $directory \
             $extra_include $extra_prototype"
eval $cmd
cmd="$LINK_BASE/bin/instantiate -t $quoted_object_type -c List \
             -II $List_incl -d $directory \
             -instantiate-node-class $extra_include $extra_prototype"
eval $cmd
cmd="$LINK_BASE/bin/instantiate -t $quoted_object_type -c DList \
             -II $DList_incl -d $directory \
             -instantiate-node-class $extra_include $extra_prototype"
eval $cmd
cmd="$LINK_BASE/bin/instantiate -t $quoted_object_type -c SortedList \
             -II $SortedList_incl -d $directory \
             $extra_include $extra_prototype"
eval $cmd
cmd="$LINK_BASE/bin/instantiate -t $quoted_object_type -c Array \
             -II $Array_incl -d $directory \
             $extra_include $extra_prototype"
eval $cmd
cmd="$LINK_BASE/bin/instantiate -t $quoted_object_type -c SortedArray \
             -II $SortedArray_incl -d $directory \
             $extra_include $extra_prototype"
eval $cmd
cmd="$LINK_BASE/bin/instantiate -t $quoted_object_type -c Iterator \
             -II $Iterator_incl -d $directory \
             $extra_include $extra_prototype"
eval $cmd
