#!/bin/bash
# paystation - CLI wrapper for the Debian 12 install
# Provides parity with the Windows GUI (start/stop/status/printer-setup)

set -u

usage() {
    cat <<EOF
PayStation 1.0.4 (Debian 12) - service control

Usage: paystation <command>

  status              Show status of all paystation-* services
  start               Start all backend services
  stop                Stop all backend services
  restart             Restart all backend services
  logs [service]      Tail logs for a service (default: all)
  health              Curl http://localhost:16385/health + /printers
  setup-printer       Discover and configure the POS-80 CUPS queue
  kiosk-start         Launch the Chromium kiosk now (restart lightdm)
  kiosk-stop          Stop the Chromium kiosk (lightdm logout)
  config              Print active configuration path and key values

Services: paystation-print  paystation-bridge  paystation-itl
Kiosk:    lightdm autologin -> /usr/share/xsessions/paystation-kiosk.desktop
EOF
}

SERVICES=(paystation-print paystation-bridge paystation-itl)

cmd="${1:-}"
case "$cmd" in
    status)
        for s in "${SERVICES[@]}"; do
            systemctl --no-pager --lines=0 status "$s" 2>&1 | head -n 4
            echo "---"
        done
        echo "kiosk (lightdm session):"
        loginctl list-sessions 2>&1 | grep -E "paystation|seat0" || echo "  (no active kiosk session)"
        ;;
    start)
        sudo systemctl start "${SERVICES[@]}"
        ;;
    stop)
        sudo systemctl stop "${SERVICES[@]}" 2>/dev/null || true
        ;;
    restart)
        sudo systemctl restart "${SERVICES[@]}"
        ;;
    logs)
        svc="${2:-}"
        if [ -n "$svc" ]; then
            journalctl -u "paystation-$svc" -f --no-pager
        else
            journalctl -u 'paystation-*' -f --no-pager
        fi
        ;;
    health)
        echo "== /health =="
        curl -sS http://localhost:16385/health || echo "(print server unreachable)"
        echo ""
        echo "== /printers =="
        curl -sS http://localhost:16385/printers || echo "(print server unreachable)"
        echo ""
        ;;
    setup-printer)
        sudo /usr/lib/paystation/setup-printer.sh "${@:2}"
        ;;
    kiosk-start)
        sudo systemctl restart lightdm
        ;;
    kiosk-stop)
        # End the active graphical session; lightdm greeter takes over.
        sudo loginctl terminate-user paystation 2>/dev/null \
            || sudo pkill -u paystation chromium 2>/dev/null \
            || true
        ;;
    config)
        echo "Config file: /etc/paystation/config.json"
        echo "Bridge env:  /etc/paystation/bridge.env"
        echo ""
        python3 -c "
import json
c = json.load(open('/etc/paystation/config.json'))
print('Version :', c['system']['version'])
print('Webapp  :', c['webapp']['url'])
print('Bridge  : https://%s:%d' % (c['bridge']['host'], c['bridge']['port']))
print('Print   : http://%s:%d  (printer: %s)' % (
    c['print_servers']['local']['host'],
    c['print_servers']['local']['port'],
    c['print_servers']['local']['printer_name']))
print('ITL API : http://%s:%d' % (c['itl_api']['host'], c['itl_api']['port']))
"
        ;;
    -h|--help|help|"")
        usage
        ;;
    *)
        echo "Unknown command: $cmd" >&2
        usage
        exit 2
        ;;
esac
