#!/bin/sh

# work around for e#290395 / LP: #458703
# https://bugs.eclipse.org/bugs/show_bug.cgi?id=290395
# https://bugs.launchpad.net/bugs/458703
export GDK_NATIVE_WINDOWS=true

export MOZILLA_FIVE_HOME="@XULRUNNER_PATH@"

ECLIPSE=/usr/lib/eclipse/eclipse

inject_update_site(){
    if [ ! -e "$1" ] ; then
	echo "W: Cannot find $1" 2>&1
	return 1
    fi
    cat - >>"$1" <<EOF
repositories/http\:__download.eclipse.org_releases_galileo/enabled=true
repositories/http\:__download.eclipse.org_releases_galileo/isSystem=false
repositories/http\:__download.eclipse.org_releases_galileo/nickname=Galileo Update Site
repositories/http\:__download.eclipse.org_releases_galileo/uri=http\://download.eclipse.org/releases/galileo/
EOF

}


if [ ! -d ~/.eclipse/ ] ; then
    $ECLIPSE -clean -initialize || exit $?
    settings=`echo ~/.eclipse/org.eclipse.platform_*/p2/org.eclipse.equinox.p2.engine/profileRegistry/PlatformProfile.profile/.data/.settings/`
    if [ ! -d "$settings" ] ; then
	echo "W: Cannot inject update-sites, cannot find the correct config." 2>&1
    else
	( inject_update_site "$settings/org.eclipse.equinox.p2.metadata.repository.prefs" && \
	  inject_update_site "$settings/org.eclipse.equinox.p2.artifact.repository.prefs" && \
	  echo "I: Injected update sites" ) || echo "W: Could not inject update sites." 2>&1
    fi
fi

exec $ECLIPSE "$@"
