#!/bin/sh -f

usage() {
   echo "Usage: $0 [-f tmp] [-keep] [-Ddefine] file(s).c"
   exit 1;
}

keep=0

prog=program

args=""

tmpfile=drv.sh

while [ ! -z $1 ];
  do case x"$1" in 
    x-D*)  args="$args $1"
      shift ;;
    x-f)  shift
      if [ -z $1 ] 
         then usage; fi
      tmpfile=$1
      shift ;;
    x-p)  shift
      if [ -z $1 ] 
          then usage; fi
      prog=$1
      shift;;
    x-keep) keep=1
      shift;;
    x-*) 
      echo "Bad option $1"; 
      usage;;
    *) break;;
  esac
done

if [ -z $1 ] 
    then usage; fi
if [ $# -eq 1 ]
    then if [ $prog = "program" ]
       then prog=`basename $1 .c`
    fi
fi

infile=$1
   
suf=`echo $infile | sed 's/^.*\.//'`
prog=`basename $infile .$suf`

cat <<EOF > $tmpfile
import convertsuif1to2
convertsuif1to2 $infile
save ${prog}.s1s2
import transforms
build_field_access_expressions
import usefulpasses
recycle_trash
insert_struct_final_padding
avoid_label_collisions
avoid_external_collisions
name_all_symbols
save ${prog}.s2
import s2c
s2c -D ARCH=${ARCH} ${prog}.s2.c
EOF

echo suifdriver -f $tmpfile
suifdriver -f $tmpfile
if [ $? != 0 ]
   then echo "failed s2 c-gen"
   exit 1
fi
if [ $keep -eq 0 ]
  then echo rm -f $prog.s1s2 $prog.s2
  rm -f $prog.s1s2 $prog.s2
fi
