#!/bin/bash

# Find the base directory
pushd `dirname $0` > /dev/null
BASE=`pwd -P`
LIB=${BASE}/build/install/itchy/lib
popd > /dev/null

NATIVE_DIR=${BASE}/build/natives/linux_`uname -m`
CLASSPATH=${LIB}/itchy-0.4.jar:${LIB}/jame-1.1.jar:${LIB}/jython-standalone-2.5.2.jar:${LIB}/groovy-all-2.4.5.jar
MAIN=uk.co.nickthecoder.itchy.Launcher

#
# Register ".itchy" files, so that games can be run by double clicking their ".itchy" file.
#
if [[ "${BASE}/resources/Launcher/template.desktop" -nt "${BASE}/resources/Launcher/nickthecoder-itchy.desktop" ]]; then
    echo "Registering .itchy files"
    COMMAND="java -Ditchy.base=\"${BASE}\" -Djava.library.path=\"${NATIVE_DIR}\" -classpath \"${CLASSPATH}\" \"${MAIN}\""
    cp "${BASE}/resources/Launcher/template.desktop" "${BASE}/resources/Launcher/nickthecoder-itchy.desktop" > /dev/null 2>&1
    echo "Icon=${BASE}/resources/icon32.png" >> "${BASE}/resources/Launcher/nickthecoder-itchy.desktop"
    echo "Exec=${COMMAND} %u" >> "${BASE}/resources/Launcher/nickthecoder-itchy.desktop"
    xdg-icon-resource install --mode user --context mimetypes --size 32 "${BASE}/resources/icon32.png" application-x-itchy  > /dev/null 2>&1
    xdg-mime install --mode user "${BASE}/resources/Launcher/nickthecoder-itchy.xml" > /dev/null 2>&1
    xdg-desktop-menu install --mode user "${BASE}/resources/Launcher/nickthecoder-itchy.desktop" > /dev/null 2>&1
    xdg-mime default nickthecoder.itchy.desktop application/x-itchy  > /dev/null 2>&1
fi

java -Ditchy.base="${BASE}" -Djava.library.path="${NATIVE_DIR}" -classpath "${CLASSPATH}" "${MAIN}" "$@"

