# Nix-Vibe task runner (see AGENTS.md for the underlying workflows)

set shell := ["zsh", "-c"]

# Apply a host's NixOS config, then relaunch QuickShell so it loads the freshly
# built shell.qml (deployed to ~/.config/quickshell as a store symlink — the
# live-reload watcher misses the new path, so a manual restart is required).
# Usage: just [hostname]  — defaults to x1carbon.
default host='x1carbon':
    #!/usr/bin/env bash
    set -euo pipefail
    sudo nixos-rebuild switch --flake ".#{{host}}"
    if pgrep -f "[H]yprland" >/dev/null 2>&1; then
      # comm name is ".quickshell-wra" (kernel-truncated from .quickshell-wrapped);
      # -x avoids matching this recipe's own argv.
      pkill -x .quickshell-wra || true
      sleep 1
      QS=quic"kshell"
      command "$QS" >/dev/null 2>&1 &
      disown || true
      echo "quickshell relaunched"
    else
      echo "no Hyprland session on this host; skipped quickshell restart"
    fi

# ===== pre-commit workflow =====

# Format the tree (nixfmt, as declared by the flake formatter).
format:
    nix fmt

# Stage new files (flake only evaluates git-tracked files), then flake check.
check:
    git add -A
    nix flake check

# Push to BOTH remotes:
#   origin (full history, ssh://git@homeserver:2222)
#   gitea  (public mirror: a clean squashed BASELINE commit, then real commits
#           cherry-picked on top so history is visible from now on)
# How the mirror works:
#   - `gitea-sync` branch holds what Gitea should show (starts at the baseline).
#   - `sync-last` tracks the last `main` commit already mirrored.
#   - Each run cherry-picks the new main commits onto `gitea-sync` and pushes,
#     so old history (and historical secrets) never appear on gitea.edley.me.
# NOTE: gitea push authenticates via ~/.git-credentials (the gitea-mcp-token).
# Run from a clean tree; commit first.
push-all:
    #!/usr/bin/env bash
    set -euo pipefail
    git push origin main
    NEW_COMMITS=$(git rev-list --reverse sync-last..main)
    if [ -z "$NEW_COMMITS" ]; then
      echo "no new commits since last mirror; gitea is current"
      exit 0
    fi
    git checkout -q gitea-sync
    echo "cherry-picking onto gitea-sync:"
    git cherry-pick $NEW_COMMITS
    git push gitea gitea-sync:main
    git checkout -q main
    git update-ref refs/heads/sync-last main
    echo "pushed main -> origin (full) and $(echo "$NEW_COMMITS" | wc -l) new commit(s) -> gitea"

# ===== deployment helpers (no quickshell restart) =====

# Build and test a config without activating it.
# Usage: just dry-build [hostname]
dry-build host='x1carbon':
    sudo nixos-rebuild dry-build --flake .#{{host}}

# Apply a config without switching (for servers / remote hosts).
# Usage: just test [hostname]
test host='x1carbon':
    sudo nixos-rebuild test --flake .#{{host}}

# Relaunch QuickShell on the current Hyprland session (picks up a rebuilt
# shell.qml without a full deploy).
restart-qs:
    #!/usr/bin/env bash
    set -euo pipefail
    if ! pgrep -f "[H]yprland" >/dev/null 2>&1; then
      echo "not in a Hyprland session; nothing to restart" >&2
      exit 1
    fi
    # comm name is ".quickshell-wra" (kernel-truncated from .quickshell-wrapped);
    # -x avoids matching this recipe's own argv.
    pkill -x .quickshell-wra || true
    sleep 1
    QS=quic"kshell"
    command "$QS" >/dev/null 2>&1 &
    disown || true
    echo "quickshell relaunched"