#!/usr/bin/env bash # setup.sh — First-time setup wizard for docker-claude set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ENV_FILE="$SCRIPT_DIR/.env" RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; BOLD='\033[1m'; NC='\033[0m' info() { echo -e "${GREEN}[+]${NC} $*"; } warn() { echo -e "${YELLOW}[!]${NC} $*"; } error() { echo -e "${RED}[✗]${NC} $*" >&2; } step() { echo -e "\n${BOLD}$*${NC}"; } # ─── Platform-specific install hints ───────────────────────────────────────── docker_install_hint() { case "$(uname -s)" in Darwin) echo " Install one of the following (both are free and open source):" echo " • Rancher Desktop (GUI, easiest): https://rancherdesktop.io/" echo " • Colima (CLI): brew install colima docker docker-compose && colima start" ;; Linux) echo " Install Docker Engine (free, no licensing restrictions):" echo " curl -fsSL https://get.docker.com | sh" echo " sudo usermod -aG docker \$USER # then log out and back in" ;; *) # Windows / Git Bash / WSL echo " Install one of the following (both are free and open source):" echo " • Rancher Desktop (GUI, easiest): https://rancherdesktop.io/" echo " • WSL2 + Docker Engine: install Ubuntu from the Microsoft Store," echo " then run: curl -fsSL https://get.docker.com | sh" ;; esac } docker_not_running_hint() { case "$(uname -s)" in Darwin|MINGW*|MSYS*|CYGWIN*) echo " → Open Rancher Desktop (or whichever Docker runtime you installed)" echo " and wait for it to finish starting, then run this setup again." ;; Linux) echo " → Start the Docker daemon: sudo systemctl start docker" ;; *) echo " → Start your Docker runtime and try again." ;; esac } # ─── Check Docker ───────────────────────────────────────────────────────────── check_docker() { step "Checking Docker..." if ! command -v docker &>/dev/null; then error "Docker is not installed." docker_install_hint exit 1 fi if ! docker info &>/dev/null 2>&1; then error "Docker is installed but not running." docker_not_running_hint exit 1 fi if ! docker compose version &>/dev/null 2>&1; then error "Docker Compose is not available." echo " Docker Compose is included with Rancher Desktop and Docker Engine." docker_install_hint exit 1 fi info "Docker is ready." } # ─── Auth setup ─────────────────────────────────────────────────────────────── setup_auth() { step "Authentication" echo " How would you like to sign in to Claude?" echo "" echo " 1) Anthropic API key (pay-per-use)" echo " Get one at: https://console.anthropic.com/settings/keys" echo "" echo " 2) Claude subscription (Claude Pro or Max)" echo " Generates a token from your existing subscription." echo "" echo " 3) Browser login (sign in when Claude first starts)" echo "" read -rp " Choice [1/2/3, default: 3]: " choice choice="${choice:-3}" case "$choice" in 1) echo "" read -rp " Paste your API key (sk-ant-...): " api_key if [[ -z "$api_key" ]]; then error "No API key entered. Run setup again when you have one." exit 1 fi echo "ANTHROPIC_API_KEY=$api_key" > "$ENV_FILE" ;; 2) echo "" echo " You'll need to run 'claude setup-token' on your host to generate a token." echo " If Claude Code is installed natively, run that command now and paste the result." echo " Otherwise choose option 3 (browser login)." echo "" read -rp " Paste your OAuth token: " token if [[ -z "$token" ]]; then error "No token entered. Run setup again when you have one." exit 1 fi echo "CLAUDE_CODE_OAUTH_TOKEN=$token" > "$ENV_FILE" ;; 3) touch "$ENV_FILE" warn "Browser login selected." warn "When Claude starts for the first time, it will print a login URL." warn "Open that URL in your browser to sign in." ;; *) error "Invalid choice: $choice" exit 1 ;; esac } # ─── Main ───────────────────────────────────────────────────────────────────── echo -e "\n${BOLD}docker-claude setup${NC}" echo "────────────────────" if [[ -f "$ENV_FILE" ]]; then warn ".env already exists (setup was already run)." read -rp " Reconfigure authentication? [y/N]: " confirm if [[ "${confirm,,}" != "y" ]]; then info "Setup skipped. Run ./launch.sh to start Claude." exit 0 fi fi check_docker setup_auth echo "" info "Setup complete!" info "→ Run ./launch.sh to start Claude Code."