#!/bin/bash

# Having any sort of classpath causes massive breakage, with Kaffe at least.
unset CLASSPATH; export CLASSPATH
unset WORKSPACE; export WORKSPACE

# Allow the user to specify their own Java home, we check for it later.
#unset JAVA_HOME; export JAVA_HOME

CMDLINEARGS=""

# Make sure this directory exists.
if [ ! -d ~/.eclipse ]; then
    mkdir ~/.eclipse > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        zenity \
            --error \
            --title="Could not launch Eclipse Platform" \
            --text="Could not create settings directory at ~/.eclipse."
    fi
fi

# Just in case Eclipse tries to put something in the home directory.
cd ~

# Load default settings from the user's configuration file.
if [ -f ~/.eclipse/eclipserc ]; then
    source ~/.eclipse/eclipserc
fi

# Process the command line options. These override the eclipserc file, so we do
# them after parsing that file.
while [ "$1" ]; do
    if [ "$1" = "-h" -o "$1" = "--help" ]; then
        usage
        exit 0
    elif [ "$1" = "-vm" ]; then
        shift
        JAVA_HOME="$1"
        shift
    elif [ "$1" = "-data" ]; then
        shift
        WORKSPACE="$1"
        shift
    else
        CMDLINEARGS="${CMDLINEARGS} $1"
        shift
    fi
done

# If the user has specified a custom JAVA, we check it for validity.
# JAVA defines the virtual machine that Eclipse will use to launch itself.
if [ -n "${JAVA_HOME}" ]; then
    echo "using specified vm: ${JAVA_HOME}"
    if [ ! -x "${JAVA_HOME}/bin/java" ]; then
        zenity \
            --error \
            --title="Could not launch Eclipse Platform" \
            --text="The custom VM you have chosen is not a valid executable."
        exit 1
    fi
fi

# If the user has not set JAVA_HOME, cycle through our list of compatible VM's
# and pick the first one that exists.
if [ -z "${JAVA_HOME}" ]; then
    echo "searching for compatible vm..."
    while read JAVA_HOME; do
        echo -n "  testing ${JAVA_HOME}..."
        if [ -x "${JAVA_HOME}/bin/java" ]; then
            export JAVA_HOME
            echo "found"
            break
        else
            echo "not found"
        fi
    done < <(cat /etc/eclipse/java_home | grep -v '^#' | grep -v '^$')
fi

# If we don't have a JAVA_HOME yet, we're doomed.
if [ -z "${JAVA_HOME}" ]; then
    zenity \
        --error \
        --title="Could not launch Eclipse Platform" \
        --text="A suitable Java Virtual Machine for running the Eclipse Platform could not be located."
    exit 1
fi

# Set path for the Mozilla SWT binding
if [ -a /usr/lib/mozilla ]; then
    export MOZILLA_FIVE_HOME=/usr/lib/mozilla
fi

# If the user has not overridden the default workspace, set the default.
#if [ -z "${WORKSPACE}" ]; then
#    WORKSPACE=~/eclipse
#    export WORKSPACE
#fi

#if [ ! -d "${WORKSPACE}" ]; then
#    mkdir "${WORKSPACE}" > /dev/null 2>&1
#    if [ $? -ne 0 ]; then
#        zenity \
#            --error \
#            --title="Could not launch Eclipse Platform" \
#            --text="Could not create workspace at ${WORKSPACE}."
#    fi
#fi

# Do the actual launch of Eclipse with the selected VM.
/usr/lib/eclipse/eclipse \
    -vm "${JAVA_HOME}/bin/java" \
    -install /usr/lib/eclipse "${CMDLINEARGS}" \
    -vmargs -Djava.library.path=/usr/lib/jni \
            -Dgnu.gcj.precompiled.db.path=/var/lib/gcj-4.0/classmap.db \
            -Dosgi.locking=none

exit $?
