#!/bin/bash
export BEMDIR=~/cbem

if [ $# -lt 1 ]
then
   echo "usage: bem {<options>} <inputfile>"
   echo "       options:"
   echo "               -s -i -p <prog> -q -Q"
   echo " "
   exit 0
fi

OPs=0;OPi=0;OPq=0;OPQ=0;OPt=0;OPp=0
while getopts "sip:qQt:" opt
do
   case $opt in
      s) OPs=1;;
      i) OPi=1;;
      q) OPq=1;;
      Q) OPQ=1;;
      p) OPp=1
         parg=$OPTARG;;
      t) OPt=1
         targ=$OPTARG;;
   esac
done

if [ $OPp -eq 1 ]
then
   exec $BEMDIR/sys/$parg $*
fi

ftyp=none
while [ $# -gt 0 ]
do
   case $1 in
      *.m)
         ftyp=m
         fil=$1
         break 2;;
      *.t)
         ftyp=t
         fil=$1
         break 2;;
   esac
   shift
done
if [ $ftyp = "none" ]
then
   echo "input file with .m or .t extension expected"
   exit 1
fi

if [ $OPt -eq 1 ]
then
   $BEMDIR/sys/bem.x $targ $fil 
else
   case $ftyp in
       m) $BEMDIR/sys/bem.x -s - start $fil;;
        t) nam=${fil%.t}
           opt=""
           if [ $OPs -eq 1 ]
           then
              opt=$opt" -s $nam.s"
           fi
           if [ $OPi -eq 1 ]
           then
              opt=$opt" -i $nam.i"
           fi
           if [ $OPq -eq 1 ]
           then
              if [ ! -f $nam.i ]
              then
                 echo "File $nam.i not found."
                 exit 1
              fi
              cp $nam.i $nam.q
              opt=$opt" -q $nam.q"
           fi
           if [ $OPQ -eq 1 ]
           then
              if [ ! -f $nam.i ]
              then
                 echo "File $nam.i not found."
                 exit 1
              fi
              cp $nam.i $nam.q
              opt=$opt" -Q $nam.q"
           fi

           $BEMDIR/sys/bem.x $opt tittra $nam.t $nam.r;;
   esac
fi




