#!/bin/bash

# compile check-oast with the SaveUnmarshal ocaml compiler
# need to adopt the its location in OCAML_TY_DIR below

#set -x

#native=no

OCAML_TY_DIR=/usr/local/stow/ocaml-ty-2006-06-22/bin
OCAMLC=$OCAML_TY_DIR/ocamlc.opt
OCAMLOPT=$OCAML_TY_DIR/ocamlopt.opt

if [ ${native:="yes"} = "yes" ] ; then
    OCAML_OBJ_EXT="cmx"
    OCAMLCC=$OCAMLOPT
else
    OCAML_OBJ_EXT="cmo"
    OCAMLCC=$OCAMLC
fi

makevars="OCAMLC=$OCAMLC OCAMLOPT=$OCAMLOPT OCAMLCC=$OCAMLCC OCAML_OBJ_EXT=$OCAML_OBJ_EXT"


# first try if cmi are available at all
file ../elsa/*cmi 2>&1 >/dev/null

if [ $? = 0 ] ; then
    version=$(file ../elsa/*cmi | head -1 | \
	sed -e ' s/.*file (\([^)]*\)) (Version \([0-9]*\).*/\1-\2/')
else
    version=""
fi

#echo version = $version

if [ ${version:=".cmi-010"} = ".cmi-010" ] ; then
    (cd ../elsa; rm -f *cmi *cmo *cmx)
    (cd ../ast; rm -f *cmi *cmo *cmx)
    rm -f *cmi *cmo *cmx

    make -C ../ast $makevars ast_ml_objects
    make -C ../elsa $makevars elsa_ml_objects
fi


make $makevars check-oast

