#!/usr/bin/env bash
# Capture a FAILING endpoint start / rc attempt.
#   ./lia-capture.sh before
#   ./lia-capture.sh after <endpoint> [label]
#
# Slices TWO logs by byte offset (both are large; only the bytes written between
# the calls are evidence):
#   daemon.stderr.log      - broker/daemon side
#   logs/obs/rc-establish.log - CLIENT side; RC_ESTABLISH lines carry the stream
#                               id, which is the ONLY way to correlate a
#                               stream-sub-attach (those lines carry no endpoint).
set -u
HOME_DIR="${SPT_HOME_DIR:-/c/Users/decid/AppData/Local/spt-core}"
DLOG="$HOME_DIR/logs/daemon.stderr.log"
RLOG="$HOME_DIR/logs/obs/rc-establish.log"
P="$(dirname "$0")"; STATE="$P/.lia-capture.state"

case "${1:-}" in
before)
  { wc -c < "$DLOG"; wc -c < "$RLOG"; } | tr -d ' ' | paste -sd' ' - > "$STATE"
  echo "baseline: $(cat "$STATE")  (daemon rc)"
  echo "now reproduce the failure, then:  $0 after <endpoint> [label]"
  ;;
after)
  [ -s "$STATE" ] || { echo "FATAL: run '$0 before' first" >&2; exit 2; }
  EP="${2:?usage: $0 after <endpoint> [label]}"; LAB="${3:-$EP}"
  read -r DB RB < "$STATE"
  TS=$(date -u +%Y%m%dT%H%M%SZ)
  DS="$P/${LAB}-daemon-$TS.log"; RS="$P/${LAB}-rc-$TS.log"
  tail -c "+$((DB+1))" "$DLOG" > "$DS"; tail -c "+$((RB+1))" "$RLOG" > "$RS"
  echo "daemon slice $(wc -c < "$DS" | tr -d ' ')B -> $DS"
  echo "rc slice     $(wc -c < "$RS" | tr -d ' ')B -> $RS"

  echo; echo "--- client attempts for '$EP' ---"
  grep -a "endpoint=$EP" "$RS" || echo "  (none — the rc client logged no RC_ESTABLISH)"
  SIDS=$(grep -a "endpoint=$EP" "$RS" | grep -oa 'stream=[0-9]*' | cut -d= -f2 | sort -u)

  echo; echo "--- THE DISCRIMINATOR: did the subscriber seat install? ---"
  if [ -z "$SIDS" ]; then
    echo "  no stream ids -> the client never got far enough to log one."
    echo "  VERDICT: AMBIGUOUS (client-side, or refused before any seat)."
  else
    for s in $SIDS; do
      n=$(grep -ac "event=stream-sub-attach stream=$s" "$DS")
      printf "  stream %-10s stream-sub-attach=%s\n" "$s" "$n"
    done
    TOT=$(for s in $SIDS; do grep -ac "event=stream-sub-attach stream=$s" "$DS"; done | paste -sd+ - | bc)
    DEC=$(grep -ac "SUBSCRIBE_DECISION" "$DS")
    echo "  SUBSCRIBE_DECISION in slice: $DEC"
    if [ "${TOT:-0}" -gt 0 ] && [ "$DEC" -eq 0 ]; then
      echo "  VERDICT: SEAT INSTALLS, failure is DOWNSTREAM."
      echo "           The bounce is justified and bd3a337b's SUBSCRIBE_REFUSED doors will speak."
    elif [ "${TOT:-0}" -eq 0 ]; then
      echo "  VERDICT: AMBIGUOUS — no seat installed. Cannot split refused / no-stream /"
      echo "           client-side from here; only the bounce splits it."
    else
      echo "  VERDICT: seat installed AND a decision landed — this attempt may not be a failure."
    fi
  fi

  echo; echo "--- context ---"
  for t in SUBSCRIBE_DECISION ATTACH_INTENT_CHOSEN stream-sub-writer-poison \
           ENDPOINT_AUTOSTART ENDPOINT_ALREADY_LIVE; do
    printf "  %-26s %s\n" "$t" "$(grep -ac "$t" "$DS")"
  done
  grep -aE "endpoint=$EP" "$DS" | tail -15

  echo; echo "NOTE: the four stream-sub-(refused|displace|no-stream) panel doors do NOT exist in"
  echo "the INSTALLED daemon. Their absence here is STRUCTURAL, never evidence. They can only be"
  echo "read after the panel binary is installed and the daemon bounced."
  ;;
*) sed -n '2,12p' "$0"; exit 2 ;;
esac
