#!/usr/bin/env bash set -euo pipefail REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" find_lab_root(){ if [[ -n "${AZEROTHAI_PLAYABLE_ROOT:-}" && -d "$AZEROTHAI_PLAYABLE_ROOT" ]]; then printf '%s' "$AZEROTHAI_PLAYABLE_ROOT"; return fi for root in "$HOME/azerothai-pc-lab" "$HOME/azerothai-playable"; do if [[ -d "$root" ]]; then printf '%s' "$root"; return; fi done printf '%s' "$HOME/azerothai-pc-lab" } LAB_ROOT="$(find_lab_root)" RUNTIME_ENV="$LAB_ROOT/runtime.env" NODE_BIN="$(command -v node)" RUN_USER="$(id -un)" RUN_GROUP="$(id -gn)" SERVICE_NAME="azerothai-autonomy.service" SERVICE_FILE="/etc/systemd/system/$SERVICE_NAME" if [[ ! -f "$RUNTIME_ENV" ]]; then echo "Missing $RUNTIME_ENV" >&2 exit 2 fi if ! grep -q '^AUTONOMY_TOKEN=' "$RUNTIME_ENV"; then echo "AUTONOMY_TOKEN is missing from $RUNTIME_ENV" >&2 exit 3 fi mkdir -p "$LAB_ROOT" touch "$LAB_ROOT/autonomy.log" chown "$RUN_USER:$RUN_GROUP" "$LAB_ROOT/autonomy.log" # Stop any legacy nohup worker before handing ownership to systemd. if [[ -f "$LAB_ROOT/autonomy.pid" ]]; then OLD_PID="$(cat "$LAB_ROOT/autonomy.pid" 2>/dev/null || true)" if [[ -n "$OLD_PID" ]] && kill -0 "$OLD_PID" 2>/dev/null; then kill "$OLD_PID" 2>/dev/null || true fi fi pkill -f "$REPO_ROOT/scripts/autonomy-worker.mjs" 2>/dev/null || true echo "==> Installing persistent AzerothAI autonomy service" sudo tee "$SERVICE_FILE" >/dev/null </dev/null sudo systemctl restart "$SERVICE_NAME" sleep 1 PID="$(sudo systemctl show -p MainPID --value "$SERVICE_NAME")" echo "$PID" > "$LAB_ROOT/autonomy.pid" if ! sudo systemctl is-active --quiet "$SERVICE_NAME"; then echo "Autonomy service failed to start." >&2 sudo systemctl status "$SERVICE_NAME" --no-pager -l >&2 || true exit 4 fi echo "Autonomy service: active (PID $PID)" echo "It will restart automatically after crashes and VM reboots."