#!/bin/sh
# ct — talk to CoreTerm from the shell.  https://coreterm.io
# Works only inside a CoreTerm pane (CORETERM_SOCKET / CORETERM_PANE_ID are set by the app).

VERSION="1.0"

usage() {
  cat <<'USAGE'
ct — CoreTerm command line helper

  ct open <file>...          open files in the CoreTerm editor (next to this pane)
  ct split [-h] [command]    split this pane (vertical, or -h horizontal) and run a command
  ct new [-n name] [dir] [-- command]   new session (in dir, default: here)
  ct name <text>             rename this session ("" restores the automatic name)
  ct color <red|orange|yellow|green|teal|blue|purple|pink|none>
  ct group <name>            move this session to a group ("" = no group)
  ct notify <title> [body]   show a macOS notification
  ct status <working|blocked|done|error|idle|clear> [message]   set this pane's agent status
  ct claude [args]           start Claude Code in a new pane next to this one
  ct help | version

Examples:
  ct open src/main.swift README.md
  make build; ct notify "Build" "finished with code $?"
  ct split -h "npm run dev"
USAGE
}

fail() { echo "ct: $*" >&2; exit 1; }

send() {
  # Fields are joined with tabs; one request per connection.
  [ -n "$CORETERM_SOCKET" ] && [ -S "$CORETERM_SOCKET" ] || fail "not running inside CoreTerm"
  req=$(printf '%s' "$1"); shift
  for f in "$CORETERM_PANE_ID" "$@"; do req=$(printf '%s\t%s' "$req" "$f"); done
  reply=$(printf '%s\n' "$req" | /usr/bin/nc -U "$CORETERM_SOCKET" 2>/dev/null)
  case "$reply" in
    ok*) return 0 ;;
    "") fail "CoreTerm did not answer" ;;
    *) fail "${reply#error }" ;;
  esac
}

abspath() {
  case "$1" in
    /*) printf '%s' "$1" ;;
    *) printf '%s/%s' "$(pwd -P)" "$1" ;;
  esac
}

cmd="$1"; [ $# -gt 0 ] && shift
case "$cmd" in
  open|edit|o)
    [ $# -gt 0 ] || fail "usage: ct open <file>..."
    for f in "$@"; do
      [ -d "$f" ] && fail "$f is a folder"
      [ -e "$f" ] || : > "$f" || fail "cannot create $f"
      send open "$(abspath "$f")" || exit 1
    done ;;
  split)
    dir=v
    if [ "$1" = "-h" ]; then dir=h; shift; fi
    send split "$dir" "$(pwd -P)" "$*" ;;
  new)
    name=""
    if [ "$1" = "-n" ]; then name="$2"; shift 2; fi
    d="$(pwd -P)"
    if [ $# -gt 0 ] && [ "$1" != "--" ]; then d="$(cd "$1" 2>/dev/null && pwd -P)" || fail "no such folder: $1"; shift; fi
    [ "$1" = "--" ] && shift
    send new "$d" "$name" "$*" ;;
  name|title|rename) send name "$*" ;;
  color|colour) send color "$1" ;;
  group) send group "$*" ;;
  notify) send notify "$1" "$2" ;;
  status) send status "$1" "$2" ;;
  claude) send split v "$(pwd -P)" "claude $*" ;;
  claude-hook)
    # Called by Claude Code hooks (see Settings → Integrations). Never fails the hook.
    [ -n "$CORETERM_SOCKET" ] || exit 0
    cat >/dev/null 2>&1   # hook JSON on stdin is not needed
    send status "$1" "" >/dev/null 2>&1
    exit 0 ;;
  version|-v|--version) echo "ct $VERSION (CoreTerm)" ;;
  help|-h|--help|"") usage ;;
  *) fail "unknown command '$cmd' (try: ct help)" ;;
esac
