#!/bin/sh
#
# Detects the platform and architecture, and starts the IDE with the right launcher
#
IDEDIR=`dirname $0`/../ide
PLATFORM=`uname -sm`
LAUNCHER=omnetpp

echo Starting the OMNeT++ IDE...

if java -version 2>&1 | grep -i "libgcj" >/dev/null 2>/dev/null; then
  echo "A compatible JRE is required to run the IDE. "
  echo "Found GNU GIJ which is not supported. Please use Oracle JRE or Open JRE 1.8+"
  echo "If you have several JDKs installed on you machine, change the default JVM."
  echo "You can switch between Java VMs with the 'sudo update-alternatives --config java' command."
  exit 1;
fi

if test ! -d $IDEDIR/configuration -a ! -d $IDEDIR/$LAUNCHER.app/Contents/Eclipse/configuration; then
  echo "The OMNeT++ IDE is not installed!"
  exit 1;
fi

#set default language so GCC will report errors in english. see bug #3
export LANG=en_US.UTF-8
# workaround for corruped maouse + missing tristate bitmaps: https://bugs.eclipse.org/bugs/show_bug.cgi?id=467983
export SWT_GTK3=0
# arguments needed to initially correctly show the default 'samples' workspace
DEFAULT_WORKSPACE_ARGS="-vmargs -Dosgi.instance.area.default=$IDEDIR/../samples"

case $PLATFORM in
*MINGW*)
	$IDEDIR/${LAUNCHER}.exe $* $DEFAULT_WORKSPACE_ARGS 2>$IDEDIR/error.log &
        ;;
*Linux*)
	$IDEDIR/${LAUNCHER} $* $DEFAULT_WORKSPACE_ARGS 2>$IDEDIR/error.log &
        ;;
*Darwin*)
	# remove the quarantine extended bit so the IDE will not be copied to a private dir on macOS sierra and later
	xattr -d com.apple.quarantine $IDEDIR/${LAUNCHER}.app 2>/dev/null
	# starting the executable directly allows to avoid unsigned app warnings showing up
	$IDEDIR/${LAUNCHER}.app/Contents/MacOS/${LAUNCHER} $* $DEFAULT_WORKSPACE_ARGS 2>$IDEDIR/error.log &
        ;;
*)
	echo OMNeT++ IDE is supported only on: Linux, Windows and macOS
	exit 1
        ;;
esac