#!/bin/sh

camlcomp="ocamlc -custom graphics.cma"

stdlib=/home/pouzet/lib/lucid-synchrone
open=lucy_pervasives 

wd=`pwd`

X=""
S=""
O=""
param="1"

while getopts :xso c
do
	case $c in
	x)	X="-x" ;;
	s)	S="-s" ;;
	o)	O="-o" ;;
	:)      echo $param
		if [ "$param" = "1" ] ; then param = "2"; $node = $c 
 		elif [ "$param" = "2" ] ; then param = "3"; $file = $c
                else { echo "Unknown parameter : $c";
		       exit 2; }
	        fi
	esac
done
shift `expr $OPTIND - 1`

node=$1
file=$2

base=`basename $file .lzi`
base=`basename $base .ls`

if [ "$file" = "$base.ls" -o ! -f "$base.lzi" ]
then
	$stdlib/lucycomp -O $open -I $stdlib -I $wd $base.ls ||
		{ rc=$? ; echo "## Error when compiling $base.ls" ; exit $rc; }
fi
 
cd /tmp

$stdlib/lucysim $X -s $node $wd/$base.lzi || 
	{ rc=$? ; 
	  echo "## Error when generating the simulation node of $node in $file"; cd $wd ; exit $rc; }

$stdlib/lucycomp -O $open -I $stdlib -I $wd $node\_\_sim.ls ||	
	{ rc=$? ;
	  echo "## Error when compiling the simulation node of $node";
	  #rm -f $node* ; 
	  cd $wd ; exit $rc; }

$camlcomp -o $node -I $stdlib xsimul.cmo graph.cmo -I $wd $wd/$base.ml $node\_\_sim.ml $node\_\_loop.ml -cclib "-L/usr/X11R6/lib -lX11 -lgraphics" ||
	{ rc=$? ;
	  echo "## Error when compiling the program $node";
	  #rm -f $node* ; 
	  cd $wd ; exit $rc; }


cp $node $wd
if [ "$S" = "-s" ]
then 
	cp -i $node\_\_sim.ls $node\_\_loop.ml $wd
fi

if [ "$O" = "-o" ]
then 
	cp -i $node\_\_sim.ml $wd
fi

rm -f $node\_\_sim.* $node\_\_loop.*
cd $wd
exit 0




