#!/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"
exec_cli="$release_root/bin/tamcoder-exec.mjs"
sessions_cli="$release_root/bin/tamcoder-sessions.mjs"
review_cli="$release_root/bin/tamcoder-review.mjs"
mcp_cli="$release_root/bin/tamcoder-mcp.mjs"
plugin_cli="$release_root/bin/tamcoder-plugin.mjs"
worktree_cli="$release_root/bin/tamcoder-worktree.mjs"
setup_cli="$release_root/bin/tamcoder-setup.mjs"
background_cli="$release_root/bin/tamcoder-background.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"
    ;;
  --help|-h)
    cat <<'EOF'
Tamcoder TypeScript CLI

Usage:
  tamcoder                         Open the terminal in this directory
  tamcoder DIR                     Open the terminal in DIR
  tamcoder -C DIR                  Choose the working directory
  tamcoder --model ID              Choose the gateway route
  tamcoder --permission-mode MODE  auto | manual | plan | read-only | auto-super
  tamcoder --plan                  Read-only start that proposes a plan first
  tamcoder --yolo, --auto-super    No approval prompts
  tamcoder --no-prompt-suggestions Turn off the suggested next message (on by default; Tab takes it)
  tamcoder --continue              Reopen the last session for this directory
  tamcoder --resume [ID]            Reopen a saved session or choose from the list
  tamcoder --image PATH            Attach an image to the first message (repeatable)
  tamcoder --alt-screen            Draw in the alternate screen (PgUp/PgDn page it here)
  tamcoder --no-alt-screen         Draw inline in terminal scrollback
  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
  tamcoder exec [options] PROMPT   Run one prompt non-interactively (--output-format json|stream-json)
  tamcoder -p [options] PROMPT     Same as exec, Claude Code style
  tamcoder sessions [--archived]   List saved conversations (also --json, --cwd, --limit)
  tamcoder agents [list] | bg [list] | background [list] | attach ID | logs ID | stop ID
                                                                  Control a detached /bg continuation
  tamcoder fork|archive|unarchive ID
  tamcoder delete ID --yes         Remove a conversation and its history
  tamcoder doctor [--json]         Check the sandbox, tools, credentials and budget
  tamcoder completion bash|zsh|fish  Print a shell completion script
  tamcoder worktree remove PATH --yes  Remove a worker agent's worktree (branch kept)
  tamcoder --init-only | --init    Run the Setup hooks (--init-only exits; --init then opens the terminal)
  tamcoder mcp list|get|add|remove  Manage MCP servers in config.toml
  tamcoder plugin list|validate|install|remove  Manage plugins (skills, hooks, MCP servers)
  tamcoder review [--base REF]     Review the uncommitted changes (read-only), print the problems found
  tamcoder features list|enable|disable NAME   Inspect or change feature flags in config.toml
  tamcoder apply TASK_ID           Apply a task's latest diff to this working tree with git apply
  tamcoder queue --thread T --message TEXT   Queue a message for an existing session
  tamcoder schedule add|list|remove|run-due  Recurring local tasks (run-due is the cron tick)
  tamcoder migrate-rollouts [--apply]   Inspect or migrate legacy sessions to paginated history
  tamcoder remote bridge           Let the TamfisGPT web workspace run commands in this folder
  tamcoder remote-control [start|stop|pair]   Manage the app-server daemon with remote control enabled
  tamcoder exec-server [forward]   Run the standalone exec-server, or register a remote one
  tamcoder cloud [exec|status|list|apply|diff]   Browse and apply Tamfis cloud tasks
  tamcoder debug models|app-server|prompt-input   Debugging tools

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
    ;;
  exec)
    shift
    exec /usr/bin/node "$exec_cli" "$@"
    ;;
  sessions|fork|archive|unarchive|delete)
    exec /usr/bin/node "$sessions_cli" "$@"
    ;;
  agents|bg|background|attach|logs|stop)
    exec /usr/bin/node "$background_cli" "$@"
    ;;
  review)
    shift
    exec /usr/bin/node "$review_cli" "$@"
    ;;
  doctor)
    shift
    exec /usr/bin/node "$release_root/bin/tamcoder-doctor.mjs" "$@"
    ;;
  remote|remote-control|debug|apply|queue|migrate-rollouts|cloud|exec-server|features|schedule)
    exec /usr/bin/node "$release_root/bin/tamcoder-verb.mjs" "$@"
    ;;
  completion)
    shift
    exec /usr/bin/node "$release_root/bin/tamcoder-completion.mjs" "$@"
    ;;
  worktree)
    shift
    exec /usr/bin/node "$worktree_cli" "$@"
    ;;
  --init-only)
    shift
    exec /usr/bin/node "$setup_cli" "$@"
    ;;
  --init)
    shift
    /usr/bin/node "$setup_cli" || exit $?
    exec /usr/bin/node "$interactive" "$@"
    ;;
  mcp)
    shift
    exec /usr/bin/node "$mcp_cli" "$@"
    ;;
  plugin)
    shift
    exec /usr/bin/node "$plugin_cli" "$@"
    ;;
  -p|--print)
    exec /usr/bin/node "$exec_cli" "$@"
    ;;
  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
