#!/bin/sh

set -e

jocamlc -linkall toplevellib.cma "$@" topstart.cmo

if [[ $? -eq 0 ]] ; then
  TOP=`echo $@ | sed -n 's/.*-o \([^ ]*\).*/\1/gp'`
  if [[ "$TOP" == "" ]] ; then
    TOP="a.out"
  fi
  if [ ! -f "a.out" ] ; then
    echo "$0: -o option should have an argument"
    exit 1
  else if [ ! -f "$TOP" ] ; then
    echo "$0: The generated toplevel doesn't exist"
    exit 1
  else
    TMP=`mktemp`
    mv $TOP $TMP
    /usr/lib/jocaml/expunge $TMP $TOP -v Parsetree Env Types
    rm -f $TMP
  fi
  fi
fi
