#!/bin/bash

function getJavaVersion()
{
    local JAVA="$1"

    local STR=$( $JAVA --version 2>&1 | head -n 1 )
    local VERSION=-1

    local COUNT=$( echo "$STR" | egrep " version \"" )
    if (( COUNT == 1 ))
    then
        VERSION=$( echo "$STR" | sed -e 's/^.*"\([^"\.]*\)\.[^"]*".*$/\1/g' )
    fi
    echo $VERSION
}

function getJVMoptions()
{
    local JAVA="$1"
    local JVM_OPTIONS=

    local VERSION=$( getJavaVersion "$JAVA" )
    if (( VERSION >= 16 ))
    then
        JVM_OPTIONS="--add-exports=java.desktop/sun.awt=ALL-UNNAMED --add-exports=java.desktop/javax.swing=ALL-UNNAMED  --add-exports=java.desktop/sun.swing=ALL-UNNAMED"
    fi

    echo "$JVM_OPTIONS"
}

JAVA="java"
JVM_OPTIONS="$( getJVMoptions "$JAVA" )"

if (( $# < 1 ))
then
    JAR_NAME=$( ls | egrep "\.jar" | head -n 1 )
else
    JAR_NAME="$1"
fi

if [[ "$JAR_NAME" == "" ]]
then
    echo "Jar \"$JAR_NAME\" not found. Exiting ..."
    exit 1
fi


$JAVA $JVM_OPTIONS -jar $JAR_NAME
