#!/bin/sh

thread="no"
localdir="no"
usedll="yes"
threads_lib="system"
verbose="no"
extra="no"
init="yes"
toplevel="ocaml"
libraries=""
libpath=""
lablgtkdir="/usr/lib/ocaml/lablgtk2"
initobjs="gtkInit.cmo"
thinitobjs="gtkThInit.cmo"
cmd="$0"

if test -n ""; then
  libpath="-I "
fi

while test -n "$1"; do
case $1 in
-help)
    echo "Usage: lablgtk2 <options> <ocaml options> [script-file]"
    echo "  -thread   use the threaded version of the toplevel"
    echo "  -all      load all extensions"
    echo "  -noinit   do not initialize gtk"
    echo "  -localdir use libraries in the same directory (before install)"
    echo "  -verbose  show actions executed"
    exit 0
    ;;
-thread)   thread=yes ;;
-localdir) localdir=yes ;;
-all)      extra=yes ;;
-noinit)   init=no ;;
-nothinit) thinitobjs="" ;;
-verbose)  verbose=yes ;;
*)  break ;;
esac
shift
done

if test $localdir = yes; then
    lablgtkdir=`dirname "$cmd"`
    CAML_LD_LIBRARY_PATH=${lablgtkdir}:$CAML_LD_LIBRARY_PATH
    if test $verbose = yes; then
        echo CAML_LD_LIBRARY_PATH=$CAML_LD_LIBRARY_PATH
    fi
fi
libpath="-I $lablgtkdir $libpath"

if test $extra = yes; then
    libraries=" lablgtk.cma lablglade.cma lablrsvg.cma lablgtksourceview2.cma"
else
    libraries="lablgtk.cma"
fi

if test $thread = yes; then
    case "$threads_lib" in
    no)     echo "Threads are not supported on this platform"; exit 2 ;;
    system) libpath="$libpath -I +threads"
            libraries="unix.cma threads.cma $libraries gtkThread.cmo" ;;
    *)      libpath="$libpath -I +vmthreads"
            usedll="no" ;; # use different stdlib.cma
    esac
    initobjs="$initobjs $thinitobjs"
elif test $extra = yes; then
    case "$threads_lib" in
    system) libraries="unix.cma $libraries";;
    *)      usedll="no";; # some libraries require posix threads
    esac
fi

if test $usedll = no; then
    toplevel="$lablgtkdir/lablgtktop"
    libraries=""
fi

if test $init = yes; then
    libraries="$libraries $initobjs"
fi

if test $verbose = yes; then echo $toplevel -w s $libpath $libraries $*; fi
exec $toplevel -w s $libpath $libraries $*
