#!/bin/sh

ws="server"
#ws="watch"

SCRIPTNAME="$(readlink -f "$0")"
SCRIPTDIR="$(dirname "${SCRIPTNAME}")"
SCRIPTDIRBASE="$(basename "${SCRIPTDIR}")"
if [ "${SCRIPTDIRBASE}" != "bin" ]; then
    echo "Script location error: ${SCRIPTNAME}" 1>&2
    exit 1
fi
INSTALLDIR="$(dirname "${SCRIPTDIR}")"
MINIMDIR="$(dirname "${INSTALLDIR}")"
MINIMDIRBASE="$(basename "${MINIMDIR}")"
if [ "${MINIMDIRBASE}" != "minim${ws}" ]; then
    echo "Script location error: ${SCRIPTNAME}" 1>&2
    exit 1
fi

if [ -f "${MINIMDIR}/etc/minimstart.conf" ]; then
    tab=$(printf '\t')
    JAVA="$(grep "^java[ $tab]*=" "${MINIMDIR}/etc/minimstart.conf" | head -n 1 | sed 's/\r//' | cut -f2- -d'=' | sed 's/^ *//')"
    JAVAOPTS="$(grep "^javaopts[ $tab]*=" "${MINIMDIR}/etc/minimstart.conf" | head -n 1 | sed 's/\r//' | cut -f2- -d'=' | sed 's/^ *//')"
    MINIMLANG="$(grep "^minimlang[ $tab]*=" "${MINIMDIR}/etc/minimstart.conf" | head -n 1 | sed 's/\r//' | cut -f2- -d'=' | sed 's/^ *//')"
    LANGFILE="$(grep "^langfile[ $tab]*=" "${MINIMDIR}/etc/minimstart.conf" | head -n 1 | sed 's/\r//' | cut -f2- -d'=' | sed 's/^ *//')"
    LANGKEY="$(grep "^langkey[ $tab]*=" "${MINIMDIR}/etc/minimstart.conf" | head -n 1 | sed 's/\r//' | cut -f2- -d'=' | sed 's/^ *//')"
fi
if [ -z "${JAVA}" ]; then
    JAVA="java"
fi
if [ -n "${MINIMLANG}" ]; then
    MINIM_LANG=${MINIMLANG};
elif [ -n "${LANGFILE}" -a -n "${LANGKEY}" ]; then
    MINIM_LANG="$(grep "^${LANGKEY}" "${LANGFILE}" | head -n 1 | cut -f2- -d'=' | sed 's/^ *//')"
fi

LD_LIBRARY_PATH="${MINIMDIR}/libsys" MINIM_LANG="${MINIM_LANG}" "${JAVA}" ${JAVAOPTS} -jar "${MINIMDIR}/lib/mscript.jar" "$@"

exit
