#!/usr/bin/sh

if [ -z $1 ]; then
    configURI=http://www.runescape.com/k=5/l=\$\(Language:0\)/jav_config.ws
else
    configURI=$1
    shift
fi

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH${LD_LIBRARY_PATH:+:}/opt/runescape-nxt-libs/"
export PULSE_PROP_OVERRIDE="application.name='RuneScape' application.icon_name='runescape' media.role='game'"
export SDL_VIDEO_X11_WMCLASS="RuneScape"
export PULSE_LATENCY_MSEC=200

# Some older graphics drivers may require this to be uncommented, but it will restrict the available graphics settings and performance.
#export MESA_GL_VERSION_OVERRIDE=3.0

unset XMODIFIERS
/usr/share/games/runescape-launcher/runescape --configURI $configURI "$@" &

if [ -z "${SHOW_LAUNCHER}" ]; then
    # The game won't be up instantly. So wait until we see 2 visible RuneScape windows.
    # While we're waiting, grab the first window ID. This will be the launcher window.
    until [ $(xdotool search --all --onlyvisible --classname "RuneScape" | wc -l) -eq 2 ]; do
        [ "$launcherId" ] || launcherId=$(xdotool search --all --onlyvisible --classname "RuneScape")
        sleep 2
    done

    # Unmap the launcher window from X. This keeps it running, but hides it from view.
    # It will close automatically when the game is closed by the user.
    xdotool windowunmap "$launcherId"
fi

exit 0
