#!/bin/sh

paths="
/usr/jdk/instances/jdk1.6.0
/usr/lib/jvm/java-6-sun
/usr/lib/jvm/java-6-openjdk
/usr/lib/jvm/default-java
"

if [ -r /etc/default/idea ]
then
  . /etc/default/idea
fi

if [ -z "$JDK_HOME" ]
then
  for path in $paths
  do
    if [ -x $path/bin/java ]
    then
      JDK_HOME=$path
      break
    fi
  done
fi

if [ -z "$JDK_HOME" ]
then
  echo "Could not find a suitable JDK installation amongst:" >/dev/stderr
  for path in $paths
  do
    echo $path >/dev/stderr
  done
  echo "Either install a JDK in one of those locations or configure JDK_HOME in /etc/default/idea:" >/dev/stderr
  exit 1
fi

echo "JDK_HOME=$JDK_HOME"
export JDK_HOME

if [ -z "$IDEA_VM_OPTIONS" ]
then
  if [ -r "$HOME/.idea.vmoptions" ]
  then
    IDEA_VM_OPTIONS="$HOME/.idea.vmoptions"
    export IDEA_VM_OPTIONS
  else
    if [ -r "/etc/intellij-idea/idea.vmoptions" ]
    then
      IDEA_VM_OPTIONS="/etc/intellij-idea/idea.vmoptions"
      export IDEA_VM_OPTIONS
    fi
  fi
fi

exec /usr/share/jetbrains/intellij-idea/bin/idea.sh "$@"

