#!/bin/bash
# Optional Mac code-index hook: updates Korium's index of the branch this
# checkout is on when a task ends. Each branch has its own index, so a run on
# one branch never replaces another branch's index.
# Requires Bash, Git, jq and korium-cli 1.0 or later in this user's PATH. When
# this Mac is not signed in, korium-cli returns at once with exit status 3, so
# the task is never held waiting for a sign-in.
# stdout is the hook JSON protocol; CLI messages stay on stderr.
set -u

korium_notice() {
  printf '%s\n' '{"systemMessage":"Korium did not update the code index. Check the hook prerequisites and CLI status on this Mac. The task can finish, but code search may be stale."}'
}

if ! command -v jq >/dev/null 2>&1; then
  korium_notice
  exit 0
fi

korium_event=$(cat)
if ! printf '%s' "$korium_event" | jq -e 'type == "object"' >/dev/null 2>&1; then
  korium_notice
  exit 0
fi

if printf '%s' "$korium_event" | jq -e '.stop_hook_active == true' >/dev/null; then
  printf '%s\n' '{}'
  exit 0
fi

korium_cwd=$(printf '%s' "$korium_event" | jq -er '.cwd | select(type == "string" and length > 0)') || {
  korium_notice
  exit 0
}
korium_repo=$(git -C "$korium_cwd" rev-parse --show-toplevel 2>/dev/null) || {
  korium_notice
  exit 0
}

if ! git -C "$korium_repo" symbolic-ref --quiet --short HEAD >/dev/null 2>&1; then
  printf '%s\n' '{"systemMessage":"Korium indexing skipped: this checkout is not on a branch. Check out a branch and the next task end indexes it."}'
  exit 0
fi
if ! git -C "$korium_repo" rev-parse --verify HEAD >/dev/null 2>&1; then
  printf '%s\n' '{"systemMessage":"Korium indexing skipped: this branch has no commit to index yet."}'
  exit 0
fi
korium_changes=$(git -C "$korium_repo" status --porcelain --untracked-files=normal 2>/dev/null) || {
  korium_notice
  exit 0
}
if [ -n "$korium_changes" ]; then
  printf '%s\n' '{"systemMessage":"Korium indexing skipped: this checkout has uncommitted changes, and this example indexes only a clean checkout. Commit, and the next task end updates the index."}'
  exit 0
fi

if ! command -v korium-cli >/dev/null 2>&1; then
  korium_notice
  exit 0
fi

# Permits removing symbols for files that went away. Does not delete source files.
# Taking the index is not the same as serving it; the next index run checks that.
korium-cli index --repo-root "$korium_repo" --allow-removals --no-wait --from-hook 1>&2
korium_status=$?
if [ "$korium_status" -eq 0 ]; then
  printf '%s\n' '{"systemMessage":"The Korium index command finished. This hook has not checked that search serves the new index yet; search for a symbol you know before relying on it."}'
elif [ "$korium_status" -eq 3 ]; then
  # Exit 3 with --from-hook: this Mac is not signed in. korium-cli has opened a
  # sign-in in the browser, unless one is already open, one was turned down in
  # the last hour, or there is no screen, and indexes once the person finishes.
  printf '%s\n' '{"systemMessage":"Korium did not update the code index because this Mac is not signed in to Korium. If a sign-in opened in your browser, finish it and korium-cli updates the index then."}'
else
  # Do not pass CLI exit 2 through: a Stop hook can read it as a continuation.
  korium_notice
fi
exit 0
