#! /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`
if [ "$ARCH" = "i86pc" ] ; then
    ARCH="x86"
fi

KERNEL=`uname -s | awk ' { printf "%s",tolower($0) ; } '`
if [ "$KERNEL" = "sunos" ] ; then
    KERNEL="solaris"
fi

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_JAR_SWT="atomic/jogl-swt.jar"

JOGL_JAR_OCULUSVR="atomic/oculusvr.jar"

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

ATOMICS_NATIVEWINDOW="atomic/nativewindow-core.jar atomic/nativewindow-os-x11.jar atomic/nativewindow-os-win.jar atomic/nativewindow-os-osx.jar"
#ATOMICS_JOGL="atomic/jogl-core.jar atomic/jogl-gldesktop.jar atomic/jogl-util.jar atomic/jogl-os-x11.jar atomic/jogl-os-win.jar atomic/jogl-os-osx.jar "
ATOMICS_JOGL="atomic/jogl-core.jar atomic/jogl-gldesktop.jar atomic/jogl-os-x11.jar atomic/jogl-os-win.jar atomic/jogl-os-osx.jar "
ATOMICS_NEWT="atomic/newt-core.jar atomic/newt-ogl.jar atomic/newt-driver-x11.jar atomic/newt-driver-win.jar atomic/newt-driver-osx.jar atomic/newt-driver-bcm-vc.jar"
JOGL_JAR_ATOMICS_NOAWT="$ATOMICS_NATIVEWINDOW $ATOMICS_JOGL $ATOMICS_NEWT"

export JOGL_JAR_ALL JOGL_JAR_ALL_NOAWT JOGL_JAR_ALL_MOBILE JOGL_JAR_ATOMICS_NOAWT
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="$JOGL_BUILD_DIR"/lib
export JOGL_LIB_DIR

JOGL_ALL_AWT_CLASSPATH=$(concat_jogl_list "$JOGL_BUILD_DIR" $JOGL_JAR_ALL $JOGL_JAR_OCULUSVR $JOGL_TEST)
JOGL_ALL_NOAWT_CLASSPATH=$(concat_jogl_list "$JOGL_BUILD_DIR" $JOGL_JAR_ALL_NOAWT $JOGL_JAR_OCULUSVR $JOGL_TEST)
JOGL_MOBILE_CLASSPATH=$(concat_jogl_list "$JOGL_BUILD_DIR" $JOGL_JAR_ALL_MOBILE $JOGL_JAR_OCULUSVR $JOGL_TEST)
JOGL_ATOMICS_NOAWT_CLASSPATH=$(concat_jogl_list "$JOGL_BUILD_DIR" $JOGL_JAR_ATOMICS_NOAWT $JOGL_JAR_OCULUSVR $JOGL_TEST)
JOGL_SWT_CLASSPATH=$(concat_jogl_list "$JOGL_BUILD_DIR" $JOGL_JAR_SWT $JOGL_JAR_OCULUSVR $JOGL_TEST)
export JOGL_ALL_AWT_CLASSPATH JOGL_ALL_NOAWT_CLASSPATH JOGL_MOBILE_CLASSPATH JOGL_ATOMICS_NOAWT_CLASSPATH JOGL_SWT_CLASSPATH

if [ ! -z "$JOGL_PROFILE" ] ; then
    case "$JOGL_PROFILE" in
        JOGL_ALL)
            JOGL_CLASSPATH=$JOGL_ALL_AWT_CLASSPATH
            ;;
        JOGL_ALL_NOAWT)
            JOGL_CLASSPATH=$JOGL_ALL_NOAWT_CLASSPATH
            ;;
        JOGL_ALL_MOBILE)
            JOGL_CLASSPATH=$JOGL_MOBILE_CLASSPATH
            ;;
    esac
else
    JOGL_CLASSPATH=$JOGL_ALL_AWT_CLASSPATH
fi
export JOGL_CLASSPATH

if [ "$KERNEL" = "linux" ] ; then
    SWT_SUB="gtk"
elif [ "$KERNEL" = "solaris" ] ; then
    SWT_SUB="gtk"
elif [ "$KERNEL" = "darwin" ] ; then
    SWT_SUB="cocoa"
    KERNEL="macosx"
    ARCH="x86_64"
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