#!/bin/sh
cd "$(dirname "$0")"
chmod +x ./node 2>/dev/null
RUNTIME="$HOME/.skitz-pc-agent/runtime.json"

stop_agent() {
  if [ -f "$RUNTIME" ]; then
    pid=$(sed -n 's/.*"pid": \([0-9]*\).*/\1/p' "$RUNTIME" | head -1)
    if [ -n "$pid" ]; then kill "$pid" 2>/dev/null || true; fi
  fi
  echo "Stopped."
  exit 0
}

if [ "$1" = "--stop" ]; then stop_agent; fi
if [ "$1" = "--uninstall" ]; then exec python3 ./skitz-pc-agent-gui.py --uninstall; fi
exec python3 ./skitz-pc-agent-gui.py
