#!/bin/bash
# /usr/local/java/bin/jarwrapper - the wrapper for binfmt_misc/jar

if [ $# -lt 1 ]
then
  echo "Use this program to execute a Java jar file, usage:"
  echo -e "\t$0 <jar file>.jar"
  echo
  echo Press key to exit ...
  read
  exit 1;
fi


# bnc#469332 - be sure, that we could work with file:// URL's too
# usefull fot desktop environments, especially GNOME
jar=`echo ${1} | sed 's#file://##'`
echo "executing:"
echo -e "java -jar ${jar}"
echo
echo
java -jar ${jar} 
ret=$?
if [ $ret -ne 0 ]
then
  X_MESSAGE="The execution of 'java -jar ${jar} failed'.
Maybe it doesn't containt the Main-Class in MANIFEST.MF
Run the above command in terminal to check the error output"

    # try any of graphical dialogs
    if [ ! -z $DISPLAY ]; then
        if which kdialog > /dev/null 2>&1; then
            kdialog --error "$X_MESSAGE"
            exit $ret
        fi
        if which zenity > /dev/null 2>&1; then
            zenity --error --text="$X_MESSAGE"
            exit $ret
        fi
        if which Xdialog > /dev/null 2>&1; then
            Xdialog --msgbox "${X_MESSAGE}" 11 80
            exit $ret
        fi
    fi

  echo "The execution of ${jar} failed. The reason is probably that ${jar} doesn't contain the MainClass in MANIFEST.MF. "
  echo "If you want to unpack ${jar}, call:                                                           "
  echo " jar -xf ${jar}                                                                               "
fi
exit $ret
