#!/bin/sh
# mlink-local-client - the mLink desktop client on this Pi's own screen,
# connected to the station daemon running here (127.0.0.1:8488, password mLink).
#
# From the console (log in on the Pi's keyboard and screen, not over ssh):
#     mlink-local-client
# starts a bare X server with only the client in it, filling the screen.
# Close the client to get the console back. Inside an existing X session it
# just opens the window.
# Extra arguments go to the client (mlink-client --help).
CLIENT=/opt/mlink/bin/mlink-client
if [ -n "${DISPLAY:-}" ] || [ -n "${WAYLAND_DISPLAY:-}" ]; then
    exec "$CLIENT" --host 127.0.0.1 "$@"
fi
if ! command -v startx >/dev/null 2>&1; then
    echo "mlink-local-client: the display packages are not installed yet." >&2
    echo "  A card install gets them at the first boot on a network with internet (see /var/log/mlink-firstboot.log);" >&2
    echo "  an install on a running Pi gets them when you answer yes to the screen packages in: sudo ./mlink-pi.sh --pi-install" >&2
    exit 1
fi
case "$(tty)" in
    /dev/tty[0-9]*) ;;
    *) echo "mlink-local-client: run this from the Pi's own console (keyboard and screen), not over ssh." >&2
       echo "  The browser client works from anywhere: http://$(hostname).local:8489/" >&2
       exit 1 ;;
esac
exec startx /opt/mlink/bin/mlink-x-session "$@" -- -nolisten tcp
