#!/bin/bash
# Simple shell script to run a sequence of alliance commands.
# Some simple error checking built in
# Place this script in your home directory and ensure that it is executable

# A global variable ;)
CURRENT_FILE=$2

# a function
helpme() {
echo "Make sure that you are in the correct directory/folder.";
echo "ie the one containing your vhdl & pattern files.";
echo "Execute as ~/`basename $0`  filename";
echo "assuming the script is in your home directory.";
echo "-f runs syf with -a as the encoding algorithm";
echo "-v runs vasy with the default settings (not -H)";

}


# functions to do the dirty work
# The first does the general purpose asimut & boog
# The others call either vasy or syf as needed

do_rest() {
 $ALLIANCE_TOP/bin/asimut -b $CURRENT_FILE $CURRENT_FILE.in $CURRENT_FILE.f.out
 if [ $? -ne 0 ]
 then 
    echo "asimut -b failed"
    exit 1
 fi

 $ALLIANCE_TOP/bin/boog $CURRENT_FILE
 if [ $? -ne 0 ]
 then 
    echo "boog failed"
    exit 1
 fi

 $ALLIANCE_TOP/bin/asimut -b $CURRENT_FILE $CURRENT_FILE.in $CURRENT_FILE.t.out
 if [ $? -ne 0 ]
 then 
    echo "asimut -b failed"
    exit 1
 fi
$ALLIANCE_TOP/bin/xpat -l $CURRENT_FILE.f.out.$VH_PATSFX&
$ALLIANCE_TOP/bin/xpat -l $CURRENT_FILE.t.out.$VH_PATSFX&
}

run_vasy() {
 pwd
 if [ ! -f "./$CURRENT_FILE.vhd" ]  
 then
   echo "no such file $CURRENT_FILE.vhd "
   echo $CURRENT_FILE.vhd
   exit 1
 fi

# ALLIANCE_TOP should be set at login to the install path to 
# the alliance tools. Should be setup automatically when the 
# tools are installed.

 $ALLIANCE_TOP/bin/vasy -Vaop -I vhd $CURRENT_FILE
 if [ $? -ne 0 ]
 then 
    echo "vasy failed"
    exit 1
 fi
 do_rest;
}

run_syf() {
 pwd
 if [ ! -f "./$CURRENT_FILE.fsm" ]  
 then
   echo "no such file $CURRENT_FILE.fsm "
   echo $CURRENT_FILE.fsm
   exit 1
 fi

# ALLIANCE_TOP should be set at login to the install path to 
# the alliance tools. Should be setup automatically when the 
# tools are installed.

 $ALLIANCE_TOP/bin/syf -VaE $CURRENT_FILE $CURRENT_FILE
 if [ $? -ne 0 ]
 then 
    echo "Syf failed"
    exit 1
 fi
$ALLIANCE_TOP/bin/xfsm -l $CURRENT_FILE.fsm&
 do_rest;
}


#
#
# main programme. Test for two values on the command line.
#

if [ $# -lt 1 ]
then 
   echo "Usage: $0 -[fv] filename or $0 --help"
else 
   case $1 in
        --help) helpme ;;
           -f)  run_syf ;;
           -v)  run_vasy;;
   esac
fi
exit 0
