#!/bin/sh

thread="no"
localdir="no"
usedll="no"
threads_lib="system"
verbose="no"
init="yes"
libraries=""
libpath="-I "
cmd="$0"

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 "  -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; shift ;;
-localdir) localdir=yes; shift ;;
-noinit)  init=no; shift ;;
-verbose) verbose=yes; shift ;;
*)  break ;;
esac
done

if test $localdir = no; then
    libpath="-I /usr/lib/ocaml/lablgtk2 $libpath"
else
    base=`dirname "$cmd"`
    libpath="-I $base $libpath"
    CAML_LD_LIBRARY_PATH=${base}:$CAML_LD_LIBRARY_PATH
    if test $verbose = yes; then
        echo CAML_LD_LIBRARY_PATH=$CAML_LD_LIBRARY_PATH
    fi
fi

if test $thread = yes; then
    libpath="$libpath -I +threads"
    if test "$threads_lib" = no; then
        echo "Threads are not supported on this platform"; exit 2
    elif test "$threads_lib" = system && test $usedll = yes; then
        toplevel="ocaml"
        libraries="unix.cma threads.cma  lablgtk.cma"
        libraries="$libraries gtkThread.cmo"
    else
        toplevel="/usr/lib/ocaml/lablgtk2/lablgtktop"
    fi
    if test $init = yes; then
        libraries="$libraries gtkInit.cmo gtkThInit.cmo"
    fi
else
    if test $usedll = yes; then
        toplevel="ocaml"
        libraries="unix.cma  lablgtk.cma"
    else
        toplevel="/usr/lib/ocaml/lablgtk2/lablgtktop"
    fi
    if test $init = yes; then
        libraries="$libraries gtkInit.cmo"
    fi
fi

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