#!/bin/sh

FLAGS=
LINK=
NATIVE=true

while true; do
 case "$1" in
   -I)
    FLAGS="$FLAGS -I $2"
    shift
    shift
    ;;
   -p)
    FLAGS="$FLAGS -I `ocamlfind query $2`"
    LINK="$LINK -package $2"
    shift
    shift
    ;;
   -l)
    LINK="$LINK $2"
    shift
    shift
    ;;
  -byte)
    NATIVE=false
    shift
    ;;
  *)
    break
    ;;
 esac
done

TARG=$1
PRIMS=$2

if [ "${TARG}" = "" ] || [ "${PRIMS}" = "" ]; then
  echo "Usage: cduce_mktop [(-I path | -p package | -l unit.cmo/cma/cmx/cmxa) ...] <target> <primitive file>"
  exit 2
fi


if [ ${NATIVE} = "true" ]; then
 CAML=ocamlopt
else
 CAML=ocamlc
fi

echo "Effective flags for CDuce: $FLAGS"
echo "Effective flags for OCaml: $LINK"

exec ocamlfind $CAML -package cduce -o $TARG $FLAGS -linkpkg -pp "mlcduce_wrapper $FLAGS" $LINK -impl $PRIMS
