#!/bin/bash
# Start Claude Code on the Anthropic API KEY instead of the subscription's
# OAuth token — the second leg of the fallback (KOHEN "Claude Code behind
# the gate — fallback"). For when /login fails or the OAuth refresh died
# (2026-10-15 21:08Z: the daemon's proactive refresh failed, every session
# on the VM showed "Login expired", and the browser flow took three tries
# from a remote shell).
#
# Billing: the API console account (pay per token), not the Max plan.
# The key is read from the upstream keys file at launch and never written
# anywhere. Direct to Anthropic, whatever settings.json says.
KEY=$(awk '$1=="anthropic"{print $2}' "$HOME/.config/shorashim/upstream_keys")
[ -n "$KEY" ] || { echo "no 'anthropic <key>' line in ~/.config/shorashim/upstream_keys" >&2; exit 1; }
export ANTHROPIC_API_KEY="$KEY"
unset ANTHROPIC_BASE_URL ANTHROPIC_CUSTOM_HEADERS
exec claude --settings '{"env":{"ANTHROPIC_BASE_URL":"https://api.anthropic.com","ANTHROPIC_CUSTOM_HEADERS":""}}' "$@"
