#!/bin/sh
############################################################################
# Time-stamp: <Mon Dec 06 2004 20:19:35 Stardate: [-29]3144.02 hwloidl>
# $Id: cmlt2thy,v 1.3 2004/12/06 19:35:09 a1hloidl Exp $
#
# Usage: cmlt2thy <file>
#
# Translate the Camelot program in <file> to a Isabelle theory file, encoding
# the Grail program produced by the Camelot compiler.
#
###########################################################################

dry_run=0
mrgroot=${MRGROOT:-${HOME}/mrg}
help=0
verb=0

getopts "hvnm:" name
while [ "$name" != "?" ] ; do
  case $name in
   h) help=1;;
   v) verb=1;;
   n) dry_run=1;;
   m) mrgroot="$OPTARG";;
  esac 
  getopts "hvnm:" name
done

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

# right version of gdf
gdf="$mrgroot/Grail/gdf/src/gdf";
gdf_flags="-t dal"
# this is LMU specific, sorry!
# mygf='/soft/TCS/lang/j2sdk1.4.1/bin/java -cp $mrgroot/progs/Grail/gf/gf.zip:$mrgroot/progs/Grail/gf/bcel.jar:/soft/TCS/lang/j2sdk1.4.1/lib:. gf'
gf="$mrgroot/Grail/gf/src/gf";
gf_flags=""
# Camelot compiler
camelot="$mrgroot/Camelot/src/camelot"
camelot_flags="-g -n"

if [ $verb -eq 1 ]
    then 
      if [ ${dry_run} -eq 1 ]
	  then echo "Dry run only"
      fi
      echo "MRG root dir is $mrgroot"
      echo "gdf command: |$gdf|; gdf flags: |$gdf_flags|"
      echo "gf command: |$gf|; gf flags: |$gf_flags|"
      echo "camelot command: |$camelot|; camelot flags: |$camelot_flags|"
fi

file="$1"
name="`echo $file | sed 's/\.cmlt//'`"
grfile="$name.gr"
thyfile="$name.thy"
tmpfile="ttt"

if [ -f "$tmpfile" ] ;
  then rm "$tmpfile"
fi

echo "mangling $file via $grfile into $thyfile"
# the main thing
if [ ${dry_run} -eq 1 ]
  then 
    echo "$camelot $file"
    echo "$gf ${gf_flags} $name > $grfile"
    echo "mv $tmpfile $grfile"
  else
    $camelot ${camelot_flags} $file
    $gf ${gf_flags} $name > $grfile
    #cat $grfile | sed -e "s/null\[\]/null[$name\$dia_0]/" >	$tmpfile 
    #mv $tmpfile $grfile
fi

# shouldn't be needed anymore!!
# number of -l options might need tinkering:
#            for camelot demo-y2 branch with std layout
#  -l        for camelot demo-y2 branch swapping hd and tl fields
#  -l -l     for camelot main branch with std layout
#  -l -l -l  for camelot main branch swapping hd and tl fields

if [ ${dry_run} -eq 1 ]
  then 
    echo "$gdf ${gdf_flags} $grfile"
  else
    $gdf ${gdf_flags}  $grfile
fi

echo "Isabelle .thy file in $thyfile"