#!/bin/sh
# PerliStream Server tray icon (per desktop user). The server is the
# perlistream-server system service; this only shows its state and controls it.
#   perlistream-server-tray [--port N] [--status]
#   perlistream-server-tray --menu-state | --set-sharing on|off
#                           | --restart-server | --open-setup | --open-libraries
#                           | --set-autostart on|off
# The second line are the headless verbs the PerliStream DESKTOP app uses to
# put the server half in its own tray; the tray stands down while that app is
# running so the product shows one icon.
# Uses the distro python3 (GTK + AppIndicator bindings are distro packages:
# python3-gi, gir1.2-ayatanaappindicator3-0.1). The app folder is APPENDED to
# sys.path so none of its modules can shadow the standard library.
SELF="$(readlink -f "$0" 2>/dev/null || echo "$0")"
PREFIX="$(cd "$(dirname "$SELF")/.." && pwd)"
PY=/usr/bin/python3
[ -x "$PY" ] || PY="$(command -v python3)"
if ! "$PY" -c 'import gi' >/dev/null 2>&1; then
    echo "perlistream-server-tray: python3-gi is not installed (sudo apt install python3-gi gir1.2-ayatanaappindicator3-0.1)" >&2
    exit 1
fi
exec "$PY" -I -c 'import sys, runpy; sys.path.append(sys.argv.pop(1)); sys.argv[0] = "perlistream-server-tray"; runpy.run_module("tray", run_name="__main__", alter_sys=True)' "$PREFIX/app" "$@"
