#! /bin/sh
#####
# WARNING: the file 'makesim' is generated from a template called 'makesim-dist'
# If you must edit this file, make sure you're editing the template!
#####

#_____________________________________________________________________________
#
#	Bourne shell script to create a Rochester Connectionist Simulator
#
#	author: Nigel Goddard
#	copyright 1989 University of Rochester
#
#       last changed:	10/10/89 Liudvikas Bukys (pre-release 4.2 fixup)
#
#_____________________________________________________________________________

#_____________________________________________________________________________
#
#	initialize parameters
#
#_____________________________________________________________________________

#where things go (or come from)
TEMP_DIR=/tmp
#top level directory
RELEASE_DIR=xxx
DEBUG_DIR=xxx

compile=xxx
ccldflags=xxx
ccldlibs=xxx
load=xxx
namelist=xxx
xincroot=xxx
xusrlibdir=xxx

#intermediate files:
OBJECT_LIST=$TEMP_DIR/connobj$$		#object list
SIM_TYPE=$TEMP_DIR/conntyp$$		#compiled for sim type
UL_FUNCS=$TEMP_DIR/connload$$.o		#ld -r libraries and user code
NM_NAMES=$TEMP_DIR/conname$$		#names nm'd from objects
BINDING=$TEMP_DIR/conn$$		#for binding .c file
trap \
'/bin/rm -f $OBJECT_LIST $SIM_TYPE $NM_NAMES $BINDING;\
	exit $status' 0 1 2 3 12 15

#umask 0

simifl=sim.h				#include file for cc'ing bind.c
usrc=""					#for list of user source files
usob=""					#for list of user object files
uobj=""					#list object files after cc'ing
ulib=""					#list of library files
uinc=""					#list of user include files
dest=sim				#where executable goes
graphics=xxx				#if installed for graphics
xwindows=xxx				#if default is xwindows interface
swindows=xxx                            #if default is suntool interface
gidebug=xxx

#_____________________________________________________________________________
#
#	read in the switches
#
#_____________________________________________________________________________

while test z$1 != z			
do					#get switches
	case $1 in
	-v)				#echo all shell commands
		verbose=yes
		;;
	-D)				#user defined data types file
		udata=$2
		shift 1
		;;
	-I)				#user include file
		uinc="$uinc -I$2"
		shift 1
		;;
	-I*)				#user include file
		uinc="$uinc $1"
		;;
	-L)				#library to search when loading
		ulib="$ulib -L$2"
		shift 1
		;;
	-L*)				#library to search when loading
		ulib="$ulib $1"
		;;
	-R)				#make relocatable instead of executable
		relocatable=yes
		;;
	-S)				#running under suntools
		swindows=yes
		xwindows=
		gidebug=gi		#gi source directory
		;;
	-X)				#running under X windows
		xwindows=yes
		swindows=
		gidebug=xgi		#gi source directory
		;;

	-f)				#compile and load for floating point
		float=yes
		;;
	-g)
		udeb=yes		#compile user files -g
		;;
	-m)				#only compile user files if
		condcomp=yes		#not up to data
		;;
	-ng)				#no graphics interface
		graphics=""
		;;
	-o)				#executable file
		dest=$2
		destset=yes
		shift 1
		;;
	-r)				#run simulator when loaded
		run=yes
		;;
	-t)				#links have delays associated
		delay=yes
		;;
	-x)				#load with -g simulator
		sdeb=yes		#for development
		;;
	*.c)				#user source files
		usrc="$usrc $1"
		usob="$usob `basename $1 .c`.o"
		uobj="$uobj `basename $1 .c`.o"
		;;
	*.o)				#user object files
		uobj="$uobj $1"
		;;
	-l*)				#user library files
		ulib="$ulib $1"
		;;
	*.a)				#user library files
		ulib="$ulib $1"
		;;
	-*)
		echo "unknown option < $1 >"
		option_error=yes
	esac
	shift 1
done

if test $option_error
then
	echo "error in specifying options... exiting"
	exit
fi

if test $sdeb
then
	CONNECT_DIR=$DEBUG_DIR
else
	CONNECT_DIR=$RELEASE_DIR
fi

bin=$CONNECT_DIR/bin			#where utilites reside
libdir=$CONNECT_DIR/lib			#general library directory
rel=$CONNECT_DIR/lib			#directory with simulator objects
incdir=$CONNECT_DIR/include		#simulator include directory
simdeb=$CONNECT_DIR/src/uniproc		#simulator source directory
gideb=$CONNECT_DIR/src/$gidebug		#gi source directory
cflag="-c -I$incdir"			#default cc flags

#_____________________________________________________________________________
#
#    the following lines test the flags are not contradictory
#
#_____________________________________________________________________________

if test $relocatable && test "z$destset" = z
then
	dest=sim.o
fi

#_____________________________________________________________________________
#
#the following lines decide which simulator object to load with, and which
#tools to do the name grabbing with.  First set for which processor type.
#
#_____________________________________________________________________________

if test $delay
then
	simobj=tsim
else
	simobj=sim
fi
if test $xwindows
then
	giobj=xgi
else
	giobj=gi
fi

#_____________________________________________________________________________
#
#    then set whether for simulator debug and/or floating point
#
#_____________________________________________________________________________

if test $float
then
	typ=f
	ulib="$ulib $libdir/libbpf.a $libdir/libsimf.a"
else
	typ=""
	ulib="$ulib $libdir/libbp.a $libdir/libsim.a"
fi
if test $sdeb
then
	simobj="$simdeb/$simobj"g.o
	giobj="$gideb/$giobj"g.o
else
	simobj="$rel/$simobj$typ.o"
	giobj="$rel/$giobj$typ.o"
fi

#_____________________________________________________________________________
#
#     set up cflags for compiling user sources
#
#_____________________________________________________________________________

if test $udeb
then
	cflag="$cflag -g"
else
	cflag="$cflag -O"
fi
if test $float
then
	if test $bfly
	then
		cflag=$cflag
	else
		cflag="$cflag -DFSIM"
	fi
fi
if test $udata
then
	if test `expr substr $udata 1 1` = "/"
	then
		if test $condcomp
		then
			file='\"'$udata'\"'
		else
			file='"'$udata'"'
		fi
	else
		if test $condcomp
		then
			file='\"'`pwd`/$udata'\"'
		else
			file='"'`pwd`/$udata'"'
		fi
	fi
	cflag="$cflag -DUDATA=$file"
fi
if test "X$uinc" != "X"
then
	cflag="$cflag $uinc"
fi
if test $delay
then
	cflag="$cflag -DTSIM"
fi

#_____________________________________________________________________________
#
#      if we only want to compile out-of-date sources, make a makefile
#      and use the make command.
#
#_____________________________________________________________________________

if test $condcomp
then
	if test "$usrc"
	then
		echo .SILENT: > $OBJECT_LIST
		echo all: $usob >> $OBJECT_LIST
		echo $usob : $incdir/$simifl >>$OBJECT_LIST
		echo "	echo compiling \$*.c" >> $OBJECT_LIST
		echo "	$compile $cflag \$*.c" >> $OBJECT_LIST
		if test $verbose; then echo making $OBJECT_LIST ; fi
		make -f $OBJECT_LIST
		status=$?
		if test $status != 0; then exit $status; fi
		rm -f $OBJECT_LIST
	fi

	if test $verbose; then echo rm -f $OBJECT_LIST ; fi
	rm -f $OBJECT_LIST
else

#_____________________________________________________________________________
#
#     otherwise compile each source file in the list
#
#_____________________________________________________________________________

	for ufile in $usrc
	do
		echo compiling $ufile
		if test $verbose; then echo $compile $cflag $ufile ; fi
		$compile $cflag $ufile
		status=$?
		if test $status != 0; then exit $status; fi
	done
fi

#_____________________________________________________________________________
#
#	use nm to get the global names out of the object file and into
#	a text file.  Gets global and local names.  To check include files.
#	(but not necessary unless user .o or .c files included).
#_____________________________________________________________________________

if test "z$uobj" != z					# user code
then

	if test $verbose; then echo "rm -f $NM_NAMES" ; fi
	rm -f $NM_NAMES
	for ufile in $uobj
	do
		if test $verbose; then echo "$namelist $ufile >> $NM_NAMES" ; fi
		$namelist $ufile >> $NM_NAMES
		status=$?
		if test $status != 0; then exit $status; fi
	done

#_____________________________________________________________________________
#
#	use grep to find lines with _si_sim_type_ , which will tell us which
#	include files were used, and hence the type of simulator compiled for
#_____________________________________________________________________________

	
	if test $verbose; then echo \
	    "grep _si_sim_type_ $NM_NAMES | sed 's/ //g' > $SIM_TYPE" ; fi
	grep "_si_sim_type_" $NM_NAMES > $SIM_TYPE
	status=$?
	if test $status = 0
		then

#_____________________________________________________________________________
#
#	use grep to get the types out of the lines
#
#_____________________________________________________________________________

		typ=""
		posstyp="float integer bflycon bflysim"
		for atyp in $posstyp
		do
			if test "X`grep _si_sim_type_$atyp $SIM_TYPE`" != X
			then		
				if test $verbose; then \
					echo adding $atyp to types found; fi
				if test "z$typ" = z
				then
					typ=$atyp
				else
					typ="$atyp $typ"
				fi
			fi
		done
		if test $verbose; then echo "rm -f $SIM_TYPE"; fi
		rm -f $SIM_TYPE
		status=$?
		if test $status != 0; then exit $status; fi
	else
		typ="notyp"
	fi
fi

#_____________________________________________________________________________
#
#	set up the type requested by the flags in a parameter
#
#_____________________________________________________________________________

if test $float
then
	oktyp=float
else
	oktyp=integer
fi

if test "z$uobj" = z || test $typ = "notyp"
then
	typ=$oktyp
fi

#_____________________________________________________________________________
#
#	check type requested in flags is type compiled for in user objects
#
#_____________________________________________________________________________

if test "$typ" != "$oktyp"
then
	echo "your files compiled for wrong simulator type(s): $typ"
	echo "you requested a $oktyp simulator - recompile or change switches"
	exit
else
	echo "simulator type is $typ"
fi

#_____________________________________________________________________________
#
#	use nm to get the global names out of the object file and into
#	a text file.  Gets only global names.  To find user function names.
#	first link all objects to suck in library function definitions.
#_____________________________________________________________________________

if test $verbose; then echo "rm -f $NM_NAMES"; fi
rm -f $NM_NAMES
status=$?
if test $status != 0; then exit $status; fi

uobj="$uobj $rel/null.o"
if test $verbose; then echo "$load -r $uobj $ulib -o $UL_FUNCS"; fi
$load -r $uobj $ulib -o $UL_FUNCS
status=$?
if test $status != 0; then exit $status; fi

if test $verbose; then echo "$namelist -g $UL_FUNCS > $NM_NAMES"; fi
$namelist -g $UL_FUNCS > $NM_NAMES
status=$?
if test $status != 0; then exit $status; fi

#_____________________________________________________________________________
#
#	if graphics wanted, get graphics function name into global names file
#
#_____________________________________________________________________________

if test $graphics
then
	if test $verbose; then echo "$namelist -g $rel/gistart.o >> $NM_NAMES" ; fi
	$namelist -g $rel/gistart.o >> $NM_NAMES
	status=$?
	if test $status != 0; then exit $status; fi
fi

#_____________________________________________________________________________
#
#	grab the function names out of the global names file, and pass them
#	to makebind, which builds the function binding .c file
#
#_____________________________________________________________________________

echo "loading function names"
if test $verbose; then echo "$bin/grabnames < $NM_NAMES | $bin/makebind $BINDING" ; fi
$bin/grabnames < $NM_NAMES | $bin/makebind $BINDING
rm -f $NM_NAMES
status=$?
if test $status != 0; then exit $status; fi

#_____________________________________________________________________________
#
#	set up flags for compiling the function binding .c file,
#	and compile it.
#
#_____________________________________________________________________________

if test $sdeb
then
	cflag="-g -c -I$incdir"
else
	cflag="-O -c -I$incdir"
fi
ext=""
comput=" -o $BINDING.bind.o"

if test $verbose; then echo "(cd $TEMP_DIR; $compile $cflag $BINDING.bind.c$ext)"; fi
(cd $TEMP_DIR; $compile $cflag $BINDING.bind.c$ext)
status=$?
if test $status != 0; then exit $status; fi

#_____________________________________________________________________________
#
#	set up the flags for loading all the objects together, and load them
#
#_____________________________________________________________________________

if test $udeb || test $sdeb
then
	cflag="-g"
else
	cflag="-O"
fi
if test $graphics
then
	simobj="$simobj $giobj"
	if test $xwindows
	then
		gflag=" -lm -L$xusrlibdir -lXaw -lXmu -lXt -lX11"
		echo "X11 graphics interface"
	else
		gflag=" -lm -lsuntool -lsunwindow -lpixrect"
		echo "SunView graphics interface"
	fi
else
	gflag="-lm"
	echo "no graphics interface"
fi

#_____________________________________________________________________________
#
#	compile debug simulator if requested
#
#_____________________________________________________________________________

if test z$verbose = z
then
	makflags="-s"
else
	makflags=""
fi

if test $sdeb
then
	if test $float
	then
		if test $delay
		then
			testype=test-fdelay
		else
			testype=test-float
		fi
	else
		if test $delay
		then
			testype=test-delay
		else
			testype=test-int
		fi
	fi
	if test $verbose; then echo "make $makflags DTYPE=$testype simdebug"; fi
	dir=`pwd`; cd $CONNECT_DIR; make $makflags DTYPE=$testype simdebug; \
	status=$?; if test $status != 0; then exit $status; fi; \
	cd $dir
	if test $graphics
	then
		if test $swindows
		then
			if test $verbose; then
				 echo "make $makflags DTYPE=$testype sdebug"; fi
			dir=`pwd`; cd $CONNECT_DIR;\
			make $makflags DTYPE=$testype sdebug; \
			status=$?; if test $status != 0; then exit $status; fi; \
			cd $dir
		elif test $xwindows
		then
			if test $verbose; then
				 echo "make $makflags DTYPE=$testype xdebug"; fi
			dir=`pwd`; cd $CONNECT_DIR;\
			make $makflags DTYPE=$testype xdebug; \
			status=$?; if test $status != 0; then exit $status; fi; \
			cd $dir
		fi
	fi
fi

#_____________________________________________________________________________
#
#	load all the object files
#
#_____________________________________________________________________________

echo "creating simulator $dest"

if test $relocatable
then
	if test $verbose; then echo $load -r $UL_FUNCS $BINDING.bind.o$ext $simobj -o $dest ; fi
	$load -r $UL_FUNCS $BINDING.bind.o$ext $simobj -o $dest
	status=$?
else
	if test $verbose; then echo $compile $cflag $ccldflags $UL_FUNCS $BINDING.bind.o$ext $simobj $rel/simalone.o -o $dest $ccldlibs $gflag ; fi
	$compile $cflag $ccldflags $UL_FUNCS $BINDING.bind.o$ext $simobj $rel/simalone.o -o $dest $ccldlibs $gflag
	status=$?
fi
if test $status != 0; then exit $status; fi

if test $verbose; then echo "rm -f $UL_FUNCS"; fi
rm -f $UL_FUNCS
status=$?
if test $status != 0; then exit $status; fi

#_____________________________________________________________________________
#
#	if simulator debugging was requested, leave binding .c file around
#	otherwise delete it.  Anyway delete binding .o file
#
#_____________________________________________________________________________

if test $sdeb
then
	debinc="-I$TEMP_DIR"
else
	if test $verbose; then echo rm -f $BINDING.bind.c ; fi
	rm -f $BINDING.bind.c
fi
if test $verbose; then echo rm -f $BINDING.bind.o ; fi
rm -f $BINDING.bind.o

#_____________________________________________________________________________
#
#	if running the simulator was requested, run it.
#
#_____________________________________________________________________________

if test $run
then
	if test $verbose; then echo $dest ; fi
	$dest
fi

#_____________________________________________________________________________
#
#	remove all the intermediate files
#
#_____________________________________________________________________________

if test $sdeb
then
	if test $verbose; then echo rm -f $BINDING.bind.c ; fi
	rm -f $BINDING.bind.c
fi

rm -f $OBJECT_LIST $SIM_TYPE $NM_NAMES $BINDING
