#!/bin/bash

EXE=`basename $0`
VERSION=2.0
O2A_VERSION=`oc2aut -version`

function USAGE {
  echo "$EXE $VERSION (oc2aut $O2A_VERSION)"
  echo "usage:"
  echo "$EXE <file.lus> <node> [<options>]"
  echo "
   lus2ec:
      -lv6 : use lustre v6 compiler (instead of pollux)
   ec2oc:
      -unctrl <id>: consider id as uncontrolable input when computing causal assertion
      -bddpage <int> : set the size of a bdds page (in K-units)
      -states <int> : stop after generating <int> states
      -merge : allows mixing state variables and conditions in bdds
"

}

function lv62ec {
   file=$1
   node=$2
   echo "lus2lic -ec $file -n $node -o $node.ec"
   lus2lic -ec "$file" -n $node -o "$node.ec"
}


if [ $# -lt 2 ] ; then
	USAGE
  	exit
fi

zefile=$1
node=$2
shift
shift
LUS2EC=lus2ec
ocargs=""
autargs=""
while [ $# -gt 0 ]; do
   #echo $1
   case "$1" in
      "-lv6")
         LUS2EC=lv62ec
      ;;
      "-unctrl")
         ocargs="$ocargs $1 $2"
         shift
      ;;
      "-bddpage")
         ocargs="$ocargs $1 $2"
         shift
      ;;
      "-states")
         ocargs="$ocargs $1 $2"
         shift
      ;;
      "-merge")
         ocargs="$ocargs $1"
         atgargs="$atgargs $1"
      ;;
      "-v")
         ocargs="$ocargs $1"
         autargs="$autargs $1"
      ;;
      "-vv")
         ocargs="$ocargs $1"
         autargs="$autargs $1"
      ;;
      *)
         autargs="$autargs $1"
      ;;
   esac
   shift
done

echo ocargs=$ocargs
echo autargs=$atgargs
echo LUS2EC=$LUS2EC

$LUS2EC $zefile $node
if [ $? -ne 0 ]; then
   exit 1
fi
ec2oc ${node}.ec -oc2 -pure -S4 -causal -merge -data $ocargs
if [ $? -ne 0 ]; then
   exit 1
fi
/bin/rm -f $node.ec
ocmin $node.oc -v
/bin/mv -f ${node}_min.oc $node.oc
oc2aut $node.oc -o $node $autargs
ok=$?
/bin/rm -f $node.oc
if [ $ok -ne 0 ]; then
   echo "Something where wrong ..."
else
   echo "File $node.aut created"
fi
