#!/bin/sh -f

# set -x; # Enable debugging

ncibin=@NCIHOME@/bin
ncisolib=@NCIHOME@/solib
if [ x$LD_LIBRARY_PATH  = x"" ]
    then LD_LIBRARY_PATH=$ncisolib
else
    LD_LIBRARY_PATH=$ncisolib:$LD_LIBRARY_PATH
fi

usage() {
   echo "Usage: $0 [-o outputfile] [-keep] file.suif"
   echo "  Generate Esterel from a SUIF file."
   exit 1;
}

keep=0

prog=program
outputfile="-"
pipeline=""
require=""
default_ext=.out.strl

defines=""

tmpfile=$$.drv
verbose=0

while [ ! -z "$1" ];
  do case x"$1" in
    x-o) shift
      if [ -z "$1" ]
        then usage; fi
      outputfile="$1"
      shift ;;
    x-v)  verbose=1
      shift ;;
    x-keep) keep=1
      shift ;;
    x-*) 
      echo "Bad option $1"; 
      usage ;;
    *) break ;;
  esac
done

if [ -z "$1" ] 
    then usage; fi
if [ $# -ne 1 ]
    then echo "Error: Too many files in the input line"
    usage
fi

base=`basename $1 .suif`
if [ `basename $1` != $base.suif ]
    then echo "Error: Input file '$1' does not have the .suif suffix"
    usage
fi

inputfile="$1"

cat <<EOF > $tmpfile
${require}
import basicnodes suifnodes
import suif2strl
load $inputfile
suif2strl
EOF

if [ $verbose = 1 ]
    then echo $ncibin/suifdriver -f $tmpfile
fi
if [ $outputfile = "-" ]
  then $ncibin/suifdriver -f $tmpfile
else
  $ncibin/suifdriver -f $tmpfile > $outputfile
fi
if [ $? != 0 ]
   then echo "FAILED: $ncibin/suifdriver -f $tmpfile"
   exit 1
fi
if [ $keep -eq 0 ]
  then if [ $verbose = 1 ]
    then echo rm -f $tmpfile
  fi
  rm -f $tmpfile
fi


