#!/bin/zsh
# Run Claude or Codex in a Deck-owned tmux session.
#
# Usage:
#   deck pair ABCDE-FGHIJ
#   deck claude [--new] [--name lane] [directory] [agent arguments...]
#   deck codex  [--new] [--name lane] [directory] [agent arguments...]
#   cct [directory] [Claude arguments...]   # compatibility shortcut
#   dct [directory] [Codex arguments...]    # compatibility shortcut
set -eu

provider="${1:-}"
case "$provider" in
  pair)
    code="${2:-}"
    [[ -n "$code" ]] || {
      print -u2 "usage: deck pair ABCDE-FGHIJ"
      exit 2
    }
    root="${${0:A}:h:h}"
    exec "$root/install.sh" --relay-url "https://deck-relay-c6b560.fly.dev" --pair "$code"
    ;;
  claude) binary="claude"; prefix="cc" ;;
  codex) binary="codex"; prefix="cx" ;;
  *)
    print -u2 "usage: deck <pair|claude|codex> [arguments...]"
    exit 2
    ;;
esac
shift

# --new always starts a fresh lane instead of attaching to an existing session
# for this directory, so two lanes in one folder stay separate. --name labels
# the tmux session with a name you choose instead of the folder name. Several
# lanes started in the same folder would otherwise show in Deckra under that
# folder's name with -2 and -3 added, with no way to tell them apart on a
# phone.
force_new=0
name_override=""
while [[ $# -gt 0 ]]; do
  case "${1:-}" in
    --new)  force_new=1; shift ;;
    --name) name_override="${2:?--name needs a value}"; shift 2 ;;
    *)      break ;;
  esac
done

command -v "$binary" >/dev/null 2>&1 || {
  print -u2 "Deck could not find $binary on PATH."
  exit 127
}
command -v tmux >/dev/null 2>&1 || {
  print -u2 "cct, dct and deck $provider need tmux. Install it with: brew install tmux"
  exit 127
}

export TMUX_TMPDIR=/tmp
export LANG="${LANG:-en_US.UTF-8}"

# Attach through iTerm2 control mode when we are starting from a plain iTerm
# tab. Control mode makes iTerm render each tmux window as a native tab, so
# iTerm keeps ownership of the pointer, drag selection, scrollback and
# Command+C. A plain `tmux attach` would hand all of that to tmux copy mode.
# The panes are ordinary tmux panes either way, which is what lets Deck read
# them with `tmux capture-pane` instead of spawning an osascript per capture.
# Set DECK_NO_CC=1 to force a plain attach.
cc=()
if [[ -z "${TMUX:-}" && "${TERM_PROGRAM:-}" == "iTerm.app" && -z "${DECK_NO_CC:-}" ]]; then
  cc=(-CC)
fi

dir="${PWD:A}"
if [[ $# -ge 1 && -d "$1" ]]; then
  dir="${1:A}"
  shift
fi

base="${name_override:-${dir:t}}"
base="${${base//[^a-zA-Z0-9_-]/-}:l}"
name="${prefix}-${base:-home}"

if [[ $# -eq 0 && $force_new -eq 0 ]] && tmux has-session -t "=$name" 2>/dev/null; then
  existing_dir="$(tmux display-message -p -t "=$name" '#{session_path}' 2>/dev/null || true)"
  if [[ -n "$existing_dir" && "${existing_dir:A}" == "$dir" ]]; then
    if [[ -n "${TMUX:-}" ]]; then exec tmux switch-client -t "=$name"; fi
    exec tmux "${cc[@]}" attach-session -t "=$name"
  fi
fi

n="$name"; i=2
while tmux has-session -t "=$n" 2>/dev/null; do
  n="${name}-$i"
  ((i++))
done

args=""
if [[ $# -gt 0 ]]; then
  args=" ${(j: :)${(q)@}}"
fi
if [[ -n "${TMUX:-}" ]]; then
  tmux new-session -d -s "$n" -c "$dir" -- /bin/zsh -lc "exec ${binary}${args}"
  exec tmux switch-client -t "=$n"
fi
exec tmux "${cc[@]}" new-session -s "$n" -c "$dir" -- /bin/zsh -lc "exec ${binary}${args}"
