#!/usr/bin/with-contenv bashio
# Headroom optimization proxy — local backend for Claude Desktop MCP and Claude Code.
declare port=8787
declare host=127.0.0.1

# The dashboard is unauthenticated. Keep it container-local by default and bind all
# interfaces only when the user explicitly opts in and maps port 8787.
if bashio::config.true 'expose_headroom_dashboard'; then
    host=0.0.0.0
fi

if bashio::config.true 'install_headroom' && command -v headroom > /dev/null 2>&1; then
    # Kompress (the ONNX compression engine) needs its model in the local HF cache: the
    # proxy's startup preload is deliberately cache-only, and the default HF cache lands
    # under ~/.cache, which the add-on points at tmpfs (/tmp/cache) — wiped on every
    # restart. Without a warm persistent cache the proxy ran forever in "deferred" mode
    # and recorded zero compression savings. Point the cache at persistent storage;
    # nothing else is needed here — the proxy's own request path already downloads a
    # missing model in the background on first use (ensure_background_load) and passes
    # requests through uncompressed until it lands, so this self-heals within a couple of
    # requests on the first boot and loads instantly (eager preload) on every boot after.
    # A synchronous pre-warm was tried here and removed: it blocked the port bind for up
    # to the download's duration, which left the settings-managed ANTHROPIC_BASE_URL
    # (see 82-claude_tools.sh) pointing at a proxy that wasn't listening yet.
    export HF_HOME="${HOME}/.headroom/hf"
    mkdir -p "$HF_HOME"
    chown abc:abc "$HF_HOME" 2> /dev/null || true
    bashio::log.info "svc-headroom: starting local Headroom proxy on ${host}:${port}"
    exec s6-setuidgid abc headroom proxy --host "${host}" --port "${port}" --code-aware
fi

bashio::log.info "svc-headroom: install_headroom disabled or headroom not found; idling"
exec sleep infinity
