#!/usr/bin/env bash

# SPDX-FileCopyrightText: 2026 Jiri Vyskocil
# SPDX-License-Identifier: Apache-2.0
# terok:container — this file is deployed into task containers, not used on the host.

# Unified ACP wrapper for OpenCode-based providers (Blablador, KISSKI, etc.).
# Derives the provider name from argv[0] (e.g. blablador-acp → blablador)
# and points OpenCode's ACP server to the provider-specific config directory.
#
# Per-agent git identity is set via terok-acp-env.sh.
# Unrestricted mode: OPENCODE_PERMISSION env var is set at container level
# by task_runners.py.  opencode reads process.env directly in ACP mode.

set -euo pipefail

PROVIDER="${0##*/}"
PROVIDER="${PROVIDER%-acp}"

# Per-agent git identity (provider name is used as the agent display name).
_AGENT_NAME="${PROVIDER^}"
_AGENT_EMAIL="noreply@${PROVIDER}.localhost"
. /usr/local/share/terok/terok-acp-env.sh

# Read config dir from host-injected env var if available, else use convention.
_env_var="TEROK_OC_${PROVIDER^^}_CONFIG_DIR"
if [[ -n "${!_env_var:-}" ]]; then
    CONFIG_DIR="${!_env_var}"
else
    CONFIG_DIR=".${PROVIDER}"
fi

export OPENCODE_CONFIG="$HOME/$CONFIG_DIR/opencode/opencode.json"
exec opencode acp "$@"
