#! /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) ; } '`
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_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=$JOGL_BUILD_DIR/lib
export JOGL_LIB_DIR

JOGL_ALL_AWT_CLASSPATH=$(concat_jogl_list $JOGL_BUILD_DIR $JOGL_JAR_ALL $JOGL_TEST)
JOGL_ALL_NOAWT_CLASSPATH=$(concat_jogl_list $JOGL_BUILD_DIR $JOGL_JAR_ALL_NOAWT $JOGL_TEST)
JOGL_MOBILE_CLASSPATH=$(concat_jogl_list $JOGL_BUILD_DIR $JOGL_JAR_ALL_MOBILE $JOGL_TEST)
JOGL_SWT_CLASSPATH=$(concat_jogl_list $JOGL_BUILD_DIR $JOGL_JAR_SWT $JOGL_TEST)
export JOGL_ALL_AWT_CLASSPATH JOGL_ALL_NOAWT_CLASSPATH JOGL_MOBILE_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