#!/bin/sh
# Installed Tamcoder entrypoint. This release is TypeScript-only: an unsupported command fails
# closed and can never fall through to a Rust or Go executable left on the host.
set -eu

launcher=$(readlink -f -- "$0")
release_root=$(CDPATH= cd -- "$(dirname -- "$launcher")/.." && pwd)
interactive="$release_root/bin/tamcoder-interactive.mjs"
app_server="$release_root/bin/tamcoder-app-server.mjs"
auth="$release_root/bin/tamcoder-auth.mjs"

case "${1-}" in
  --version|-V)
    version=$(sed -n "s/^export const TAMCODER_VERSION = '\\([^']*\\)';$/\\1/p" "$release_root/dist/version.js")
    if [ -z "$version" ]; then
      echo "tamcoder: release version is missing" >&2
      exit 1
    fi
    echo "tamcoder $version (TypeScript)"
    ;;
  --help|-h)
    cat <<'EOF'
Tamcoder TypeScript CLI

Usage:
  tamcoder                         Open the terminal in this directory
  tamcoder DIR                     Open the terminal in DIR
  tamcoder --model ID              Choose the gateway route
  tamcoder --permission-mode MODE  auto | plan | read-only | auto-super
  tamcoder --plan                  Read-only start that proposes a plan first
  tamcoder --yolo                  No approval prompts
  tamcoder --continue              Reopen the last session for this directory
  tamcoder --resume ID             Reopen a saved session
  tamcoder --alt-screen            Draw in the alternate screen (PgUp/PgDn page it here)
  tamcoder --no-color              Plain text, no escape sequences
  tamcoder login                   Sign in with a browser code (or --with-api-key)
  tamcoder logout                  Forget this machine's credential
  tamcoder status                  Report the signed-in account
  tamcoder update                  Check for a newer TypeScript release
  tamcoder resume [ID]             Open the terminal on the most recent conversation, or on ID
  tamcoder continue                Open the terminal on the last conversation for this directory
  tamcoder app-server --listen stdio:// [options]
  tamcoder sandbox -P :workspace -C DIR -- COMMAND [ARG...]
  tamcoder auth token

Inside the terminal: /help lists the commands, ! runs a shell command here, Esc interrupts.
EOF
    ;;
  "")
    exec /usr/bin/node "$interactive"
    ;;
  resume)
    if [ "$#" -ge 2 ]; then
      id=$2
      shift 2
      exec /usr/bin/node "$interactive" --resume "$id" "$@"
    fi
    exec /usr/bin/node "$interactive" --resume
    ;;
  continue)
    exec /usr/bin/node "$interactive" --continue
    ;;
  login|logout|status)
    exec /usr/bin/node "$auth" "$@"
    ;;
  update)
    shift
    exec /usr/bin/node "$release_root/bin/tamcoder-update.mjs" "$@"
    ;;
  auth)
    if [ "${2-}" != "token" ]; then
      echo "usage: tamcoder auth token" >&2
      exit 2
    fi
    exec /usr/bin/node "$auth" token
    ;;
  app-server|sandbox)
    exec /usr/bin/node "$app_server" "$@"
    ;;
  *)
    # A flag, an absolute or relative path, or a phrase with a space in it is the terminal's
    # business; anything else is a verb this build does not have, and saying so beats guessing.
    case "$1" in
      -*|/*|.*|~*|*' '*) exec /usr/bin/node "$interactive" "$@" ;;
      *)
        echo "tamcoder: unsupported TypeScript command: ${1-none}" >&2
        exit 2
        ;;
    esac
    ;;
esac
