#!/bin/sh

# Copyright (C) 2009 Mehdi Dogguy <dogguy@pps.jussieu.fr>
# You have permission to copy, modify, and redistribute under the
# terms of the GPL-3.0. For full license terms, see gpl-3.0.txt.

set -e

jocamlc -I /usr/lib/jocaml/jocamltoploop/ -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 "$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
