#!/bin/sh
# JoyRemote - the viewer, and the name on your PATH.
#
# The frozen application lives in /opt/joyremote and is the same executable
# whichever half you want: no argument is the viewer, --host is the host.
set -e

# A system-wide default for the directory server this computer registers with,
# seeded into the account's own settings the first time it runs.
#
# JoyRemote reads service.json from the folder its device key lives in, which
# off Windows is under the home directory - so a file in /etc cannot be the one
# it reads. Copying it once, rather than symlinking or exporting a variable,
# means an account can then change its mind and keep the change: the package
# supplies a default, it does not hold the setting. Delete the copy to take the
# default again.
DEFAULTS=/etc/joyremote/service.json
SETTINGS="${HOME:-/nonexistent}/JoyRemote/JoyRemote"
if [ -r "$DEFAULTS" ] && [ ! -e "$SETTINGS/service.json" ]; then
    if mkdir -p "$SETTINGS" 2>/dev/null; then
        cp "$DEFAULTS" "$SETTINGS/service.json" 2>/dev/null || true
        chmod 600 "$SETTINGS/service.json" 2>/dev/null || true
    fi
fi

exec /opt/joyremote/JoyRemote "$@"
