#!/bin/sh
# Dreamy CLI installer. Usage:
#   curl -fsSL https://dreamy.so/install.sh | sh
#   curl -fsSL https://dreamy.so/install.sh | sh -s -- --api https://api.example --key WORKER_KEY
# Options: --api URL (also connects when --key is given and downloads the CLI that API serves),
#          --key KEY (a worker's key from Workspace settings -> Workers), --dir DIR (default ~/.local/bin),
#          --source URL (CLI download URL).
# Environment fallbacks: DREAMY_API, DREAMY_INSTALL_DIR, DREAMY_SOURCE.
# Needs python3 (3.8+) and curl or wget. Never prints the key; `dreamy connect` stores it privately.
set -eu

API="${DREAMY_API:-}"
KEY=""
DIR="${DREAMY_INSTALL_DIR:-$HOME/.local/bin}"
SOURCE="${DREAMY_SOURCE:-}"
DEFAULT_SOURCE="https://dreamy.so/dreamy"

while [ $# -gt 0 ]; do
    case "$1" in
        --api) API="$2"; shift 2 ;;
        --code) echo "install.sh: pairing codes are gone; connect with the worker's key: --key KEY" >&2; exit 2 ;;
        --key) KEY="$2"; shift 2 ;;
        --dir) DIR="$2"; shift 2 ;;
        --source) SOURCE="$2"; shift 2 ;;
        -h|--help) sed -n '2,8p' "$0" 2>/dev/null || echo "See https://dreamy.so/agents.md"; exit 0 ;;
        *) echo "install.sh: unknown option $1" >&2; exit 2 ;;
    esac
done

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

command -v python3 >/dev/null 2>&1 || fail "python3 is required (3.8 or newer)."
python3 -c 'import sys; sys.exit(0 if sys.version_info >= (3, 8) else 1)' || fail "python3 3.8 or newer is required."

if [ -z "$SOURCE" ]; then
    # The CLI served by the connected API always matches that API's protocol.
    if [ -n "$API" ]; then SOURCE="${API%/}/agent/tools/dreamy"; else SOURCE="$DEFAULT_SOURCE"; fi
fi

TMP="$(mktemp "${TMPDIR:-/tmp}/dreamy-cli.XXXXXX")"
trap 'rm -f "$TMP"' EXIT
if command -v curl >/dev/null 2>&1; then
    curl -fsSL "$SOURCE" -o "$TMP" || fail "download failed: $SOURCE"
elif command -v wget >/dev/null 2>&1; then
    wget -qO "$TMP" "$SOURCE" || fail "download failed: $SOURCE"
else
    fail "curl or wget is required."
fi
head -c 200 "$TMP" | grep -q 'Dreamy worker CLI' || fail "unexpected content from $SOURCE"
python3 - "$TMP" <<'PY' || fail "downloaded CLI does not compile"
import sys; compile(open(sys.argv[1]).read(), 'dreamy', 'exec')
PY

mkdir -p "$DIR"
cp "$TMP" "$DIR/dreamy"
chmod 755 "$DIR/dreamy"
echo "Installed $DIR/dreamy ($("$DIR/dreamy" --version))"

# Put the install directory on PATH for future shells, once.
case ":$PATH:" in
    *":$DIR:"*) ;;
    *)
        SHELL_NAME="$(basename "${SHELL:-sh}")"
        case "$SHELL_NAME" in
            zsh) RC="$HOME/.zshrc" ;;
            bash) RC="$HOME/.bashrc" ;;
            fish) RC="$HOME/.config/fish/config.fish" ;;
            *) RC="$HOME/.profile" ;;
        esac
        mkdir -p "$(dirname "$RC")"
        if [ "$SHELL_NAME" = fish ]; then LINE="fish_add_path \"$DIR\""; else LINE="export PATH=\"$DIR:\$PATH\""; fi
        if ! grep -qsF "$LINE" "$RC"; then
            printf '\n# Dreamy CLI\n%s\n' "$LINE" >> "$RC"
            echo "Added $DIR to PATH in $RC; restart your shell or run: $LINE"
        else
            echo "Restart your shell so $DIR is on PATH (already configured in $RC)."
        fi
        ;;
esac

if [ -n "$API" ] && [ -n "$KEY" ]; then
    "$DIR/dreamy" connect "$API" --key "$KEY"
elif [ -n "$API" ]; then
    echo "Next: dreamy connect $API   (paste a worker's key from Workspace settings -> Workers)"
else
    echo "Next: dreamy connect API_URL --key WORKER_KEY, then dreamy agent --harness claude"
fi
