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

INSTANCE="${1:-athenaos}"
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

if ! command -v limactl >/dev/null 2>&1; then
  echo "limactl is required. Install Lima first: brew install lima"
  exit 1
fi

limactl copy "${REPO_ROOT}/athena" "${INSTANCE}:/tmp/athena"
limactl copy "${REPO_ROOT}/templates" "${INSTANCE}:/tmp/templates"
limactl copy "${REPO_ROOT}/configs" "${INSTANCE}:/tmp/configs"

limactl shell "${INSTANCE}" sudo bash -s <<'EOF'
set -euo pipefail

rm -rf /opt/athena/athena /opt/athena/templates /opt/athena/configs
mkdir -p /opt/athena
mv /tmp/athena /opt/athena/athena
mv /tmp/templates /opt/athena/templates
mv /tmp/configs /opt/athena/configs
ln -sf /opt/athena/athena/cli/athena.py /usr/local/bin/athena
ln -sf /usr/local/bin/athena /usr/local/bin/ath
chmod +x /opt/athena/athena/cli/athena.py
install -m 0644 /opt/athena/configs/motd.txt /etc/motd
EOF

echo "Athena CLI installed in Lima instance '${INSTANCE}'."
echo "Try: limactl shell ${INSTANCE} athena doctor"
