#!/usr/bin/env bash
# hq-lanes-drain.sh — PreToolUse drain for a running `hq lanes` worker (spec §6.5).
#
# Registered once under $GROK_HOME/hooks/. Resolves the lane from HQ_LANE_ID.
# The no-op path is the most important behaviour: this hook runs on every grok
# session on this box. If HQ_LANE_ID is absent or empty, exit 0 with no output.
set -u

if [ -z "${HQ_LANE_ID:-}" ]; then
  exit 0
fi

set -eo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MJS="$SCRIPT_DIR/hq-lanes-drain.mjs"
if [ ! -f "$MJS" ]; then
  printf 'hq-lanes-drain: missing %s\n' "$MJS" >&2
  exit 1
fi
exec node "$MJS"
