#! /bin/sh

function print_usage()
{
    echo "Usage: $0 {JOGL_ALL|JOGL_ALL_NOAWT|JOGL_ALL_MOBILE} [jogl-build-dir]"
}

function concat_jogl_list()
{
    ITEMDIR=$1
    shift
    for j in $ITEMDIR $ITEMDIR/jar ; do
        for i in $* ; do
            if [ -e $j/$i ] ; then 
                if [ -z "$JOGL_LIST" ] ; then
                    JOGL_LIST=$j/$i
                else
                    JOGL_LIST=$JOGL_LIST:$j/$i
                fi
            fi
        done
    done
    echo $JOGL_LIST
}

ARCH=`uname -m`
KERNEL=`uname -s | awk ' { printf "%s",tolower($0) ; } '`

uname -a | grep -i LINUX && OSS=x11
uname -a | grep -i Darwin && OSS=osx
uname -a | grep -i CYGWIN && OSS=win

JOGL_TEST="jogl.test.jar"

JOGL_JAR_ALL="jogl.all.jar"
JOGL_JAR_ALL_NOAWT="jogl.all-noawt.jar"
JOGL_JAR_ALL_MOBILE="jogl.all-mobile.jar"

JOGL_LIB_ALL="libnativewindow_x11.so libnativewindow_awt.so libjogl_desktop.so libjogl_mobile.so libjogl_cg.so libnewt.so"
JOGL_LIB_ALL_NOAWT="libnativewindow_x11.so libjogl_desktop.so libjogl_mobile.so libjogl_cg.so libnewt.so"
JOGL_LIB_ALL_MOBILE="libjogl_mobile.so libnewt.so"

export JOGL_JAR_ALL JOGL_JAR_ALL_NOAWT JOGL_JAR_ALL_MOBILE
export JOGL_LIB_ALL JOGL_LIB_ALL_NOAWT JOGL_LIB_ALL_MOBILE

THISDIR=`dirname $0`

JOGL_PROFILE="$1"
shift

if [ -z "$JOGL_PROFILE" ] ; then
    echo JOGL PROFILE missing
    print_usage
fi

JOGL_BUILD_DIR="$1"
shift

AUTOBUILD=0

if [ ! -z "$JOGL_BUILD_DIR" -a -e "$JOGL_BUILD_DIR" ] ; then
    AUTOBUILD=0
elif [ -e $THISDIR/jar ] ; then
    # Autobuild
    JOGL_BUILD_DIR=$THISDIR/jar
    AUTOBUILD=1
else
    echo invalid JOGL_BUILD_DIR $JOGL_BUILD_DIR and no AUTOBUILD
    print_usage
fi 
export JOGL_BUILD_DIR

JOGL_LIB_DIR=""
if [ $AUTOBUILD -eq 0 ] ; then
    JOGL_LIB_DIR=$JOGL_BUILD_DIR/nativewindow/obj:$JOGL_BUILD_DIR/jogl/obj:$JOGL_BUILD_DIR/newt/obj
else
    # Autobuild
    JOGL_LIB_DIR=$THISDIR/lib
fi
export JOGL_LIB_DIR

if [ ! -z "$JOGL_PROFILE" ] ; then
    case "$JOGL_PROFILE" in
        JOGL_ALL)
            JOGL_CLASSPATH=$(concat_jogl_list $JOGL_BUILD_DIR $JOGL_JAR_ALL $JOGL_TEST)
            ;;
        JOGL_ALL_NOAWT)
            JOGL_CLASSPATH=$(concat_jogl_list $JOGL_BUILD_DIR $JOGL_JAR_ALL_NOAWT)
            ;;
        JOGL_ALL_MOBILE)
            JOGL_CLASSPATH=$(concat_jogl_list $JOGL_BUILD_DIR $JOGL_JAR_ALL_MOBILE)
            ;;
    esac

    export JOGL_CLASSPATH
fi

if [ "$KERNEL" = "linux" ] ; then
    SWT_SUB="gtk"
elif [ "$KERNEL" = "darwin" ] ; then
    SWT_SUB="cocoa"
fi
if [ -z "$SWT_CLASSPATH" ] ; then
    SWT_CLASSPATH_0=$JOGL_BUILD_DIR/../make/lib/swt/$SWT_SUB-$KERNEL-$ARCH/swt-debug.jar
    SWT_CLASSPATH_1=$HOME/.java/swt.jar
    if [ ! -e "$SWT_CLASSPATH_0" ] ; then 
        echo Info: NO JOGL SWT $SWT_CLASSPATH_0
        if [ ! -e "$SWT_CLASSPATH_1" ] ; then 
            echo Info: NO Users SWT $SWT_CLASSPATH_1
        else
            export SWT_CLASSPATH=$SWT_CLASSPATH_1
            echo Info: Using Users SWT $SWT_CLASSPATH
        fi
    else
        export SWT_CLASSPATH=$SWT_CLASSPATH_0
        echo Info: Using JOGL SWT $SWT_CLASSPATH
    fi
    if [ ! -z "$SWT_CLASSPATH" ] ; then
        export SWT_CLASSPATH
    fi
fi

if [ -z "$JOGL_CLASSPATH" ] ; then
    print_usage
fi

