#!/usr/bin/env bash
# One-time bootstrap for the Thermograph LAN dev server on THIS machine.
#
# Sudo-free: the app runs as a Docker Compose stack (see deploy-dev.sh), and
# `linger` keeps the runner/services running across logout/reboot. Re-runnable
# (idempotent).
#
#   bash deploy/provision-dev-lan.sh
set -euo pipefail

APP_DIR="${APP_DIR:-$HOME/thermograph-dev}"
REPO_URL="${REPO_URL:-http://10.10.0.2:3080/emi/thermograph-infra.git}"
BRANCH="${BRANCH:-dev}"
here="$(cd "$(dirname "$0")" && pwd)"

echo "==> Enabling linger so the service survives logout/reboot"
loginctl enable-linger "$USER" \
  || echo "   (couldn't enable linger; the service still runs while you're logged in)"

echo "==> Cloning/refreshing $APP_DIR and starting the service"
APP_DIR="$APP_DIR" REPO_URL="$REPO_URL" BRANCH="$BRANCH" bash "$here/deploy-dev.sh"

cat <<EOF

Done. The dev server runs as the 'thermograph-dev' systemd --user service.
  status:  systemctl --user status thermograph-dev
  logs:    journalctl --user -u thermograph-dev -f
  restart: systemctl --user restart thermograph-dev
  stop:    systemctl --user stop thermograph-dev
EOF
