#!/usr/bin/env bash
# protect-pane.sh — re-apply tmux protection to the sendblue-channel sidebar pane
# Idempotent: safe to run multiple times or after tmux restart.
#
# Finds the sidebar pane (marked with @sidebar=true) in the mbp session,
# then applies protections so the pane survives process exits and accidental kills.
#
# Usage:
#   bash ~/openclaw/tools/sendblue-channel/protect-pane.sh
#   # or from any dir:
#   bash protect-pane.sh

set -euo pipefail

# Tmux session containing the sendblue sidebar pane. Different hosts use
# different conventions (e.g. "mbp" on the MacBook, "exult" on the iMac).
SESSION="${SENDBLUE_TMUX_SESSION:-mbp}"
# CHANNEL_DIR defaults to this script's directory so it works across checkout
# locations. Override with SENDBLUE_CHANNEL_DIR if invoking from elsewhere.
CHANNEL_DIR="${SENDBLUE_CHANNEL_DIR:-$(cd "$(dirname "$0")" && pwd)}"

# Strategy 1: find the pane via @sidebar flag (set by ensure-sidebar-pane.sh)
sidebar_pane=$(tmux list-panes -s -t "$SESSION" -F '#{pane_id} #{@sidebar}' 2>/dev/null \
  | awk '$2 == "true" {print $1}' \
  | head -1)

# Strategy 2: fall back to finding a pane whose cwd is the sendblue-channel directory
if [ -z "$sidebar_pane" ]; then
  sidebar_pane=$(tmux list-panes -s -t "$SESSION" -F '#{pane_id} #{pane_current_path}' 2>/dev/null \
    | awk -v dir="$CHANNEL_DIR" '$2 == dir {print $1}' \
    | head -1)
  # If found via path, retroactively set the @sidebar flag for future lookups
  if [ -n "$sidebar_pane" ]; then
    tmux set-option -p -t "$sidebar_pane" @sidebar "true" 2>/dev/null
    tmux select-pane -t "$sidebar_pane" -T "main-iMessage-agent" 2>/dev/null
    echo "[protect-pane] Found sendblue pane by cwd, set @sidebar flag."
  fi
fi

if [ -z "$sidebar_pane" ]; then
  echo "[protect-pane] No sendblue-channel pane found in session '$SESSION'."
  echo "  Checked: @sidebar flag, pane cwd ($CHANNEL_DIR)"
  exit 1
fi

# Verify the pane is still alive
if ! tmux display-message -t "$sidebar_pane" -p '#{pane_id}' >/dev/null 2>&1; then
  echo "[protect-pane] Sidebar pane $sidebar_pane exists in metadata but is dead."
  exit 1
fi

echo "[protect-pane] Found sendblue sidebar pane: $sidebar_pane"

# Get the window containing this pane
window=$(tmux display-message -t "$sidebar_pane" -p '#{session_name}:#{window_index}')

# -- Pane-level protection --
# remain-on-exit: keeps the pane visible (shows "Pane is dead") if the process crashes,
# instead of destroying the pane entirely. The run-channel.sh auto-restart loop
# means this mainly protects against unexpected SIGKILL or OOM.
tmux set-option -p -t "$sidebar_pane" remain-on-exit on 2>/dev/null
echo "  [ok] pane remain-on-exit on"

# destroy-unattached off: prevents destruction when the client detaches.
# This is the global default but we set it explicitly for safety.
tmux set-option -p -t "$sidebar_pane" destroy-unattached off 2>/dev/null
echo "  [ok] pane destroy-unattached off"

# -- Window-level protection --
# Ensures the entire window (and all its panes) survives if the active process exits.
tmux set-option -w -t "$window" remain-on-exit on 2>/dev/null
echo "  [ok] window $window remain-on-exit on"

# -- Verify --
pane_remain=$(tmux show-options -p -t "$sidebar_pane" -v remain-on-exit 2>/dev/null)
window_remain=$(tmux show-options -w -t "$window" -v remain-on-exit 2>/dev/null)

if [ "$pane_remain" = "on" ] && [ "$window_remain" = "on" ]; then
  echo "[protect-pane] All protections verified for $sidebar_pane in $window."
else
  echo "[protect-pane] WARNING: verification failed (pane=$pane_remain, window=$window_remain)"
  exit 1
fi
