#!/bin/sh
# /usr/bin/tnx-agent: the seller's command. The real program is /usr/lib/tnx-agent/tnx-agent and runs as
# the tnx-agent user, so the key and counters in /var/lib/tnx-agent always stay readable by the service.
set -e
BIN=/usr/lib/tnx-agent/tnx-agent
CFG=/etc/tnx-agent/agent.json
SVC=tnx-agent.service
cmd=${1:-}
as_agent() {
    if [ "$(id -u)" = 0 ]; then exec runuser -u tnx-agent -- "$BIN" "$@"; fi
    if [ "$(id -un)" = tnx-agent ]; then exec "$BIN" "$@"; fi
    echo "Run it with sudo:  sudo tnx-agent $*" >&2; exit 1
}
need_root() { [ "$(id -u)" = 0 ] || { echo "Run it with sudo:  sudo tnx-agent $*" >&2; exit 1; }; }
has_cfg() { for a in "$@"; do case "$a" in --config|--config=*) return 0;; esac; done; return 1; }
case "$cmd" in
  ""|-h|--help|help)
    cat <<'TXT'
TunnelNet Exchange seller app
  sudo tnx-agent enrol CODE         join with the code from your TunnelNet Exchange page
  sudo tnx-agent status             what is shared, today and this month, and estimated earnings
  sudo tnx-agent share off | on     stop or start sharing (takes effect within a second)
  sudo tnx-agent set monthly-gb N   your own monthly limit in GB (0 = none)
  sudo tnx-agent set daily-gb N     your own daily limit in GB (0 = none)
  sudo tnx-agent set hours 22:00-07:00 | any
  sudo tnx-agent set timezone America/Chicago
  tnx-agent version
TXT
    exit 0;;
  version|uplinks) exec "$BIN" "$@";;
  enrol|enroll)
    need_root "$@"; shift
    [ -n "${1:-}" ] || { echo "usage: sudo tnx-agent enrol CODE" >&2; exit 2; }
    systemctl stop "$SVC" 2>/dev/null || true
    if runuser -u tnx-agent -- "$BIN" enrol "$@" --config "$CFG"; then
        systemctl enable --now "$SVC" >/dev/null 2>&1
        echo "Done. Sharing runs in the background and starts again by itself after a restart."
        echo "See it any time:  sudo tnx-agent status"
    else
        if [ -f /var/lib/tnx-agent/state.json ] && grep -q '"seller_id"' /var/lib/tnx-agent/state.json; then
            systemctl start "$SVC" 2>/dev/null || true   # already enrolled before: keep sharing
        fi
        exit 1
    fi;;
  set)
    need_root "$@"
    if has_cfg "$@"; then out=$("$BIN" "$@") || exit 1; else out=$("$BIN" "$@" --config "$CFG") || exit 1; fi
    printf '%s\n' "$out" | grep -v '^To use it now' || true
    if systemctl is-active --quiet "$SVC"; then systemctl restart "$SVC"; echo "Applied."; fi;;
  status)
    if [ "$(id -u)" = 0 ] && ! has_cfg "$@"; then
        if ! grep -qs '"seller_id"' /var/lib/tnx-agent/state.json; then
            echo "Not enrolled yet. To start sharing:  sudo tnx-agent enrol YOUR-CODE"; exit 0
        fi
        if ! systemctl is-active --quiet "$SVC"; then
            echo "Sharing is not running. Start it:  sudo systemctl start tnx-agent"; exit 0
        fi
    fi
    if has_cfg "$@"; then as_agent "$@"; else as_agent "$@" --config "$CFG"; fi;;
  share|keygen)
    if has_cfg "$@"; then as_agent "$@"; else as_agent "$@" --config "$CFG"; fi;;
  run|service) exec "$BIN" "$@";;
  *) exec "$BIN" "$@";;
esac
