#!/bin/sh
# repowise-hook-start
# Auto-syncs repowise wiki after each commit (background, non-blocking).
# Installed by: repowise hook install
{
  ROOT=$(git rev-parse --show-toplevel 2>/dev/null) || exit 0
  [ -d "$ROOT/.repowise" ] || exit 0
  HEAD=$(git rev-parse HEAD 2>/dev/null) || HEAD=""
  TS=$(date +%s 2>/dev/null) || TS=""
  if [ -n "$TS" ]; then
    printf '{"target_commit":"%s","queued_at":%s}\n' "$HEAD" "$TS" \
      > "$ROOT/.repowise/.update.queued" 2>/dev/null || true
  fi
  LOG="$ROOT/.repowise/.update.log"
  {
    printf '\n--- post-commit hook fired at %s for HEAD %s ---\n' \
      "$(date 2>/dev/null)" "$HEAD"
  } >> "$LOG" 2>/dev/null || true
  (
    cd "$ROOT" || exit 1
    if command -v repowise >/dev/null 2>&1; then
      repowise update >> "$LOG" 2>&1
    elif command -v uv >/dev/null 2>&1; then
      uv run repowise update >> "$LOG" 2>&1
    fi
  ) &
} >/dev/null 2>&1
# repowise-hook-end
