#!/bin/bash
##############################################################################
# Time-stamp: <Wed Dec 22 2004 21:01:32 Stardate: [-29]3224.17 hwloidl>
#
# Usage: check_it [options] <camelot-prg>
#
# Take Camelot program and check that it fulfills the infered heap consumption.
# NB: No proof file is needed any more. The camelot compiler creates the producer
#     side certificate (plus .class file, of course) e.g.
#       camelot -C -lfd I.cmlt
#     and the gf disassembler generates the consumer side certificate
#     (plus the .gr file, of course), e.g.
#       gf -t VCG I
#
# Hack MYPROGS if your copies of camelot and gf are somewhere else.
#
# Options:
#  -G  ... name of gdf assembler
#  -g  ... options passed to gdf
#  -F  ... name of gf disassembler
#  -f  ... options passed to gf
#  -C  ... name of camelot compiler
#  -c  ... options passed to camelot
#  -i  ... specify isabelle image
#  -v  ... be verbose
#  -h  ... help message
##############################################################################

progname="`basename $0`"
args="$*"

# where to find the progs/ dir
MYPROGS=../../..
# an isabelle heap of VCG logic
IMG=${HOME}/isabelle/heaps/polyml-4.1.3_x86-linux/VCG_LRT2

mygf=${MYPROGS}/Grail/gf/src/gf
mygdf=${MYPROGS}/Grail/gdf/src/gdf
myc=${MYPROGS}/Camelot/src/camelot

mygdf_opts="-t VCG"
mygf_opts="-t VCG"
myc_opts="-C -lfd"
ISABELLE="isabelle"

verb=0
help=0
debug=0
proof_file=""

getopts "hvdo:i:G:g:F:f:C:c:" name
while [ "$name" != "?" ] ; do
  case $name in
   h) help=1;;
   v) verb=1;;
   o) apfile="$OPTARG";;
   i) IMG=$OPTARG;;
   G) mygdf=$OPTARG;;
   g) mygdf_opts=$OPTARGS;;
   F) mygf=$OPTARG;;
   f) mygf_opts=$OPTARGS;;
   C) myc=$OPTARG;;
   f) myc_opts=$OPTARGS;;
  esac 
  getopts "hvdo:i:G:g:F:f:C:c:" name
done

if [ $verb -eq 1 ]
    then verb_opt="-v "
    else verb_opt=""
fi

shift $[ $OPTIND - 1 ]

if [ $help -eq 1 ]
 then no_of_lines=`cat $0 | awk 'BEGIN { n = 0; } \
                                 /^$/ { print n; \
                                        exit; } \
                                      { n++; }'`
      echo "`head -$no_of_lines $0`"
      exit 
fi

if [ -z "$1" ]
 then echo "Usage: $progname [options] file[.cmlt]"
      echo "Use -h option for details"
      exit 1;
fi

# 1. Producer Side: compile the Camelot file down to a JVM class file + producer-side certificate
f="`basename $1 .cmlt`"
if [ "$f" != "$1" ]
  then # it's a Camelot file
       if [ $verb -eq 1 ]
         then echo "Compiling Camelot file $1"
       fi
       $myc $1 
  else # assume it's a class file that we want to verify
       f="`basename $1 .class`"
fi        

prg_thy="${f}Prog.thy"
proof_thy="${f}Certificate1.thy"

if [ $verb -eq 1 ]
  then echo ".. Basename of input file: $f"
       if [ -f "$f.class" ]
         then echo ".. JVM class file: $f.class"
	 else echo "** Cannot find JVM class file $f.class"
       fi
       echo ".. camelot is $myc $myc_opts"
       echo ".. gdf/opts: $mygdf $mygdf_opts"
       echo ".. gf/opts: $mygf $mygf_opts"
       echo ".. Isabelle image file: $IMG"
fi


# 2. Consumer Side: generate .thy file and consumer-side certificate
# cmlt2thy
if [ $debug -eq 1 ]
  then echo "Doing: |$mygf $mygf_opts $f.class > $f.gr|"
fi
$mygf $mygf_opts $f.class > $f.gr

# #mv $f.thy $prg_thy
# # post-process prg.thy file
# tmp_file="`basename ${prg_thy} .thy`.tmp"
# if [ $verb -eq 1 ]
#   then echo "Generated prog_thy file is ${f}.thy" #; tmp file is ${tmp_file}; prg file is $prg_thy"
# fi

#cat "${f}.thy" | sed -e "s/= DAss_rulesU + NILList/= DAss_rulesU2/g;s/$f/${f}Prog/g" > $tmp_file
#lines=$[ `wc -l $tmp_file | awk '{print $1}'` - 6 ]
#echo "Lines: $lines"
#head -${lines} $tmp_file > $prg_thy
#echo "end" >> $prg_thy
# cp $tmp_file $prg_thy

# 3. generate ROOT.ML for the proof file
echo "use_thy \"${f}Certificate3\";" > ROOT.ML

if [ $debug -eq 1 ]
  then echo "ROOT.ML looks like this"
       cat ROOT.ML
fi

# 4. run isabelle to check the proof
if [ $debug -eq 1 ]
  then echo "Starting Isabelle; if successful this should finish with val it = () : unit and prompt"
fi

# first check whether we have an image
if [ ! -f "${IMG}" ]
  then echo "Freaking out: no isabelle image found at ${IMG}"
       exit
fi
${ISABELLE} -r ${IMG} DUMMY < ROOT.ML

