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

ATHENA_ROOT="${ATHENA_ROOT:-/opt/athena}"
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
TARGET_BIN="/usr/local/bin/athena"

if [[ "${EUID}" -ne 0 ]]; then
  echo "Please run as root or with sudo."
  exit 1
fi

mkdir -p "${ATHENA_ROOT}"
cp -R "${REPO_ROOT}/athena" "${ATHENA_ROOT}/"
cp -R "${REPO_ROOT}/templates" "${ATHENA_ROOT}/"
cp -R "${REPO_ROOT}/configs" "${ATHENA_ROOT}/"
ln -sf "${ATHENA_ROOT}/athena/cli/athena.py" "${TARGET_BIN}"
ln -sf "${TARGET_BIN}" /usr/local/bin/ath
chmod +x "${ATHENA_ROOT}/athena/cli/athena.py"

install -m 0644 "${REPO_ROOT}/configs/motd.txt" /etc/motd

cat >/etc/profile.d/athenaos.sh <<'EOF'
export ATHENA_WORKSPACE="${ATHENA_WORKSPACE:-/workspace/projects}"
alias ath='athena'
alias projects='cd /workspace/projects'
EOF

mkdir -p /workspace/projects /workspace/agents /workspace/sandbox /workspace/archives

echo "AthenaOS shell integration installed."

