#!/usr/bin/env bash
# Install Souveraine as the desktop system: binary, systemd user unit,
# quickshell surface. Idempotent — safe to re-run for upgrades.
set -euo pipefail

REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

echo "== build =="
cargo build --release --manifest-path "$REPO/Cargo.toml"

echo "== binary =="
install -Dm755 "$REPO/target/release/souveraine" "$HOME/.local/bin/souveraine"

echo "== systemd user unit =="
install -Dm644 "$REPO/packaging/souveraine.service" \
    "$HOME/.config/systemd/user/souveraine.service"
if command -v systemctl >/dev/null; then
    systemctl --user daemon-reload
    systemctl --user enable souveraine.service
    systemctl --user restart souveraine.service || true
fi

echo "== quickshell surface =="
if [[ -d "$HOME/.config/quickshell/ii" ]]; then
    "$REPO/surfaces/quickshell/deploy.sh"
else
    echo "  (no illogical-impulse config found — skipping surface deploy)"
fi

echo "== done =="
echo "server: systemctl --user status souveraine"
