#!/usr/bin/env bash
# SPDX-FileCopyrightText: 2025 Jiri Vyskocil
# SPDX-License-Identifier: Apache-2.0
# terok:container — this file is deployed into task containers, not used on the host.

# ACP wrapper for GitHub Copilot.
#
# Sets up per-agent git identity and injects terok system instructions
# before exec-ing the native copilot ACP adapter.
#
# Instructions: Copilot discovers user-level instruction files matching
# *.instructions.md from directories listed in COPILOT_CUSTOM_INSTRUCTIONS_DIRS.
# We create a per-task subdir under /home/dev/.terok/ with a correctly named
# copy of instructions.md and point the env var there.  This avoids touching
# ~/.copilot (shared volume) or workspace files.
# Note: this feature is reportedly unstable upstream (issues #1982, #2181).
#
# Unrestricted mode: COPILOT_ALLOW_ALL env var is set at container level
# by task_runners.py.  copilot reads it regardless of --acp mode.
# Alternative: --yolo/--allow-all flags also work with --acp.

set -euo pipefail

_AGENT_NAME="Copilot"
_AGENT_EMAIL="noreply@github.com"
. /usr/local/share/terok/terok-acp-env.sh

_TEROK_INSTR="/home/dev/.terok/instructions.md"
_COPILOT_INSTR_DIR="/home/dev/.terok/copilot-instructions"

if [[ -f "$_TEROK_INSTR" ]]; then
    mkdir -p "$_COPILOT_INSTR_DIR"
    cp "$_TEROK_INSTR" "$_COPILOT_INSTR_DIR/terok.instructions.md"
    export COPILOT_CUSTOM_INSTRUCTIONS_DIRS="$_COPILOT_INSTR_DIR"
fi

exec copilot --acp "$@"
