#!/usr/bin/env bash
set -euo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"

echo "[openlife] build"
npm run build

echo "[openlife] installing autonomous bundle"
node dist/index.js agent install

echo "[openlife] ensuring runtime mode"
node dist/index.js mode set autonomous --host claude-code

if command -v systemctl >/dev/null 2>&1; then
  echo "[openlife] systemd detected. Template em scripts/openlife-agent.service.example"
  echo "[openlife] Para ativar:"
  echo "  sudo cp scripts/openlife-agent.service.example /etc/systemd/system/openlife-agent.service"
  echo "  sudo systemctl daemon-reload && sudo systemctl enable --now openlife-agent"
elif command -v supervisord >/dev/null 2>&1 || command -v supervisorctl >/dev/null 2>&1; then
  echo "[openlife] supervisor detected. Template em scripts/openlife-agent.supervisord.conf.example"
  echo "[openlife] Para ativar, inclua o template no supervisor e rode supervisorctl reread/update"
else
  echo "[openlife] nenhum process manager detectado; usando launcher manual"
  echo "  ./scripts/openlife-agent-start.sh"
fi

echo "[openlife] done"
