#!/bin/bash

if [ "$OSTYPE" == "darwin"* ]; then
    cd $(dirname $_)
else
    cd $(dirname $0)
fi
JAVA_VERSION=`echo "$(java -version 2>&1)" | grep "version" | awk '{ print substr($3, 2, length($3)-2); }'`

jv=$JAVA_VERSION

for (( i=0; i<${#jv}; i++ )); do
  if [ "${jv:$i:1}" == "." ]; then
	DOT_INDEX=$i
	break
  fi
done

if [ "$DOT_INDEX" -eq 0 ]; then
	JAVA_VER=$JAVA_VERSION
else
	JAVA_VER=${JAVA_VERSION%%.*}
fi

# could be 1.8, 1.6, 1.7 etc
if [ "$JAVA_VER" -eq 1 ]; then

	JAVA_VER=${JAVA_VERSION:2:1}
fi

if [ "$JAVA_VER" -lt 6 ]; then 
    echo Java 1.6 or higher required
    exit 1;
elif [ "$JAVA_VER" -gt 10 ]; then
    java -cp "./ix/*:./lib/*:./imageio/*:./nashorn/*:./rhino8/*" -splash:icon/splash.jpg photogrok.PhotoGrok &
elif [ "$JAVA_VER" -eq 6 ]; then
    java -cp "./ix/*:./lib/*:./rhino6/*" -splash:icon/splash.jpg photogrok.PhotoGrok &
elif [ "$JAVA_VER" -eq 7 ]; then
    java -cp "./ix/*:./lib/*:./rhino6/*" -splash:icon/splash.jpg com.javafx.main.Main &
else
    java -cp "./ix/*:./lib/*:./imageio/*:./rhino8/*" -splash:icon/splash.jpg photogrok.PhotoGrok &
fi
