#!/usr/bin/env bash
# Installs Promethean Terminal: downloads kitty, wires up the config and icon,
# and registers the desktop entry in the app menu and on the Desktop.
set -euo pipefail

# This script lives at <repo>/bin/promethean/install.sh; its own dir holds the
# kitty conf + desktop entry it installs.
SELF_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
KITTY_BIN="$HOME/.local/kitty.app/bin/kitty"
DESKTOP_FILE="$SELF_DIR/promethean-terminal.desktop"
CONF="$SELF_DIR/kitty.conf"
NEXUS_ROOT="$(cd "$SELF_DIR/../.." && pwd)"
RCFILE="$NEXUS_ROOT/.promethean_bashrc"
ICON="$NEXUS_ROOT/assets/promethean-terminal.png"

# ── 1. Download kitty ────────────────────────────────────────────────────────
if [[ -x "$KITTY_BIN" ]]; then
    echo "kitty already at $KITTY_BIN — skipping download (run with --update to force)"
    if [[ "${1:-}" == "--update" ]]; then
        echo "→ Updating kitty..."
        curl -fsSL https://sw.kovidgoyal.net/kitty/installer.sh | sh /dev/stdin launch=n
    fi
else
    echo "→ Downloading kitty to ~/.local/kitty.app/ ..."
    curl -fsSL https://sw.kovidgoyal.net/kitty/installer.sh | sh /dev/stdin launch=n
fi

echo "   kitty $("$KITTY_BIN" --version)"

# ── 2. Ensure .promethean_bashrc exists ──────────────────────────────────────
if [[ ! -f "$RCFILE" ]]; then
    echo "→ Creating $RCFILE (sourcing ~/.bashrc) ..."
    mkdir -p "$(dirname "$RCFILE")"
    cat > "$RCFILE" <<'EOF'
# Promethean Terminal shell init
[[ -f ~/.bashrc ]] && source ~/.bashrc
export VIRTUAL_ENV_DISABLE_PROMPT=1
if [[ -f "$HOME/nexus-core/Promethean/bin/activate" ]]; then
    source "$HOME/nexus-core/Promethean/bin/activate"
fi
export PS1='\[\e[0;35m\](Promethean)\[\e[0m\] ${debian_chroot:+($debian_chroot)}\[\e[0;37m\]\u@\h\[\e[0m\]:\[\e[1;32m\]\w\[\e[0m\]\$ '
printf '\e]0;Promethean Terminal\a'
EOF
fi

# ── 3. Write the .desktop file (repo copy + installed copies) ────────────────
write_desktop() {
    local dest="$1"
    cat > "$dest" <<EOF
[Desktop Entry]
Type=Application
Name=Promethean Terminal
Comment=Launch a terminal with the Promethean environment
Exec=$KITTY_BIN --config $CONF --class promethean-terminal -e bash --rcfile $RCFILE -i
Icon=$ICON
Terminal=false
Categories=Utility;TerminalEmulator;
StartupNotify=false
StartupWMClass=promethean-terminal
EOF
}

echo "→ Writing desktop entry..."
write_desktop "$DESKTOP_FILE"
write_desktop "$HOME/.local/share/applications/promethean-terminal.desktop"

# ── 4. Desktop icon ──────────────────────────────────────────────────────────
DESKTOP_DIR="$HOME/Desktop"
mkdir -p "$DESKTOP_DIR"
cp "$DESKTOP_FILE" "$DESKTOP_DIR/promethean-terminal.desktop"
chmod +x "$DESKTOP_DIR/promethean-terminal.desktop"

# Mark trusted so XFCE/Nemo doesn't show the "untrusted" banner
gio set "$DESKTOP_DIR/promethean-terminal.desktop" metadata::trusted true 2>/dev/null \
    || xfce4-file-manager --quit 2>/dev/null || true

# ── 5. Refresh app menu ──────────────────────────────────────────────────────
update-desktop-database "$HOME/.local/share/applications" 2>/dev/null || true

echo ""
echo "Done. Promethean Terminal is installed."
echo "  Binary : $KITTY_BIN"
echo "  Config : $CONF"
echo "  Icon   : $ICON"
echo "  Desktop: $DESKTOP_DIR/promethean-terminal.desktop"
