#!/bin/sh
# Prime Gate Agent installer for Linux gateways.
#   curl -fsSL https://rms.primerp.ai/sdk/install.sh | sudo sh -s -- --code ENROLL-CODE [--server https://rms.primerp.ai]
# Debian/Ubuntu/Raspberry Pi OS/Moxa/Advantech: needs python3 (3.9+) and python3-venv.
set -e
SERVER="https://rms.primerp.ai"; CODE=""; PKG_URL=""
while [ $# -gt 0 ]; do
  case "$1" in
    --code) CODE="$2"; shift 2;;
    --server) SERVER="$2"; shift 2;;
    --package) PKG_URL="$2"; shift 2;;
    *) echo "unknown option $1"; exit 1;;
  esac
done
[ "$(id -u)" = "0" ] || { echo "run as root (sudo)"; exit 1; }
command -v python3 >/dev/null || { echo "python3 is required"; exit 1; }
PYV=$(python3 -c 'import sys;print("%d.%d"%sys.version_info[:2])')
echo "python $PYV detected"
if command -v apt-get >/dev/null && ! python3 -c 'import venv' 2>/dev/null; then
  apt-get update -qq && apt-get install -y -qq python3-venv >/dev/null
fi
mkdir -p /opt/prime-gate /etc/prime-gate /var/lib/prime-gate
python3 -m venv /opt/prime-gate/venv
PKG_URL="${PKG_URL:-$SERVER/sdk/prime-gate-agent-latest.tar.gz}"
echo "installing agent from $PKG_URL"
/opt/prime-gate/venv/bin/pip install -q --upgrade pip >/dev/null 2>&1 || true
/opt/prime-gate/venv/bin/pip install -q "prime-gate-agent[all] @ $PKG_URL"
ln -sf /opt/prime-gate/venv/bin/prime-gate /usr/local/bin/prime-gate
if [ ! -f /etc/prime-gate/agent.yaml ]; then
  cat > /etc/prime-gate/agent.yaml <<EOF
# Prime Gate Agent configuration. Full reference: $SERVER/sdk/
server_url: $SERVER
gateway_name: $(hostname)
poll_interval_sec: 60
fast_poll_interval_sec: 5
heartbeat_interval_sec: 300
devices: []
points:
  - { key: rms.cpu_pct,  driver: sysinfo, name: Gateway CPU,        unit: "%", domain: rms, params: { metric: cpu_pct } }
  - { key: rms.mem_pct,  driver: sysinfo, name: Gateway memory,     unit: "%", domain: rms, params: { metric: mem_pct } }
  - { key: rms.disk_pct, driver: sysinfo, name: Gateway disk,       unit: "%", domain: rms, params: { metric: disk_pct } }
  - { key: rms.temp_c,   driver: sysinfo, name: Gateway temperature, unit: C, domain: rms, params: { metric: temp_c } }
  - { key: rms.uplink_ms, driver: sysinfo, name: Uplink latency,    unit: ms, domain: rms, params: { metric: ping_ms, host: 8.8.8.8 } }
EOF
  echo "wrote /etc/prime-gate/agent.yaml (edit it to add your Modbus/SNMP/GPIO points)"
fi
cat > /etc/systemd/system/prime-gate.service <<'EOF'
[Unit]
Description=Prime Gate Agent (Prime RMS)
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
ExecStart=/opt/prime-gate/venv/bin/prime-gate run
Restart=always
RestartSec=5
User=root
Environment=PYTHONUNBUFFERED=1
# Hardening
NoNewPrivileges=true
ProtectSystem=full
ReadWritePaths=/var/lib/prime-gate /etc/prime-gate
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
if [ -n "$CODE" ]; then
  prime-gate enroll "$CODE"
fi
systemctl enable --now prime-gate
sleep 2
systemctl --no-pager --lines=5 status prime-gate || true
echo "done. Logs: journalctl -u prime-gate -f   Config: /etc/prime-gate/agent.yaml   Bench: prime-gate read"
