# =============================================================================
#  AI Agents Sandbox — GitHub Copilot + Gemini CLI + Claude Code
#  Base : openSUSE Tumbleweed — rootless, no embedded credentials
#  Auth : runtime only, persisted via home volume
# =============================================================================

FROM registry.opensuse.org/opensuse/tumbleweed:latest

ARG AGENT="copilot gemini claude opencode antigravity hermes-agent"
ARG IMG_TAG="0.0.0"
ARG AI_USER=aiuser
ARG AI_UID=1000
ARG AI_GID=1000
ARG PKGS=""
ARG HOOKS_BUILD="hooks/build"

LABEL maintainer="val4oss" \
      description="AI Agents sandbox - ${AGENT}, rootless" \
      version="${IMG_TAG}" \
      security.caps="none" \
      security.network="pasta" \
      security.credentials="runtime-only"

# ==============
# System Package
# --------------
RUN zypper --non-interactive refresh && \
    zypper --non-interactive update && \
    zypper --non-interactive install --no-recommends \
        bash \
        ca-certificates \
        coreutils \
        curl \
        diffutils \
        figlet \
        file \
        findutils \
        gawk \
        grep \
        git \
        gzip \
        iproute2 \
        iputils \
        jq \
        less \
        nodejs \
        npm \
        patch \
        procps \
        python3 \
        python3-pip \
        sed \
        shadow \
        ShellCheck \
        tar \
        toilet \
        unzip \
        util-linux \
        vim-small \
        wget \
        which \
        xclip \
        ${PKGS} && \
    zypper clean --all && \
    rm -rf /var/cache/zypp/* /tmp/* /var/tmp/*

RUN zypper --non-interactive addrepo \
        https://download.opensuse.org/repositories/home:/val4oss/openSUSE_Tumbleweed/ \
        home:val4oss && \
    zypper --non-interactive --gpg-auto-import-keys refresh home:val4oss && \
    zypper --non-interactive install --no-recommends customize-ps1 && \
    zypper clean --all && \
    rm -rf /var/cache/zypp/*

# ===============
# GitHub CLI (gh) - installed when AGENT contains copilot
# ---------------
RUN case " $AGENT " in \
    *\ copilot\ *) \
        zypper --non-interactive addrepo \
            https://cli.github.com/packages/rpm/gh-cli.repo && \
        zypper --non-interactive --gpg-auto-import-keys refresh gh-cli && \
        zypper --non-interactive install --no-recommends gh && \
        zypper clean --all && \
        rm -rf /var/cache/zypp/* \
    ;; \
    esac

# ================
# Google Cloud SDK - installed when AGENT contains claude or opencode
# ----------------
RUN case " $AGENT " in \
    *\ claude\ *|*\ opencode\ *) \
        REPO_ARCH=$(uname -m) && \
        rpm --import https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg && \
        zypper refresh && \
        zypper --non-interactive addrepo \
            https://packages.cloud.google.com/yum/repos/cloud-sdk-el10-${REPO_ARCH} \
            google-cloud-cli && \
        zypper --non-interactive --gpg-auto-import-keys refresh google-cloud-cli && \
        zypper --non-interactive install --no-recommends google-cloud-cli && \
        zypper clean --all && \
        rm -rf /var/cache/zypp/* \
    ;; \
    esac

# ====================
# Create non-root user
# --------------------
RUN groupadd --gid $AI_GID $AI_USER && \
    useradd \
      --uid $AI_UID \
      --gid $AI_GID \
      --home-dir /home/$AI_USER \
      --shell /bin/bash \
      --create-home \
      $AI_USER

# =========
# AI agents - installed selectively based on AGENT list
# (trusted vs untrusted distinction: hermes-agent is untrusted)
# filtering happens in glaipnir.sh script
# ---------
RUN case " $AGENT " in *" gemini "*) \
        npm install -g @google/gemini-cli ;; esac && \
    case " $AGENT " in *" claude "*) \
        npm install -g @anthropic-ai/claude-code ;; esac && \
    case " $AGENT " in *" opencode "*) \
        npm install -g opencode-ai ;; esac && \
    case " $AGENT " in *" copilot "*) \
        wget -qO- https://gh.io/copilot-install | bash ;; esac && \
    case " $AGENT " in *" antigravity "*) \
        curl -fsSL https://antigravity.google/cli/install.sh | \
        bash -s -- -d "/usr/local/bin" ;; esac && \
    case " $AGENT " in *" hermes-agent "*) \
        curl -fsSL https://hermes-agent.nousresearch.com/install.sh | \
        bash -s -- --skip-setup --non-interactive --skip-browser ;; esac

# ============
# Copy scripts
# ------------
COPY --chmod=555 ${HOOKS_BUILD}/ /usr/local/bin/ai-agents-sandbox-build-hooks/
COPY --chmod=755 scripts/entrypoint.sh /usr/local/bin/entrypoint.sh

# ====================================
# Copy setup and skel for intial setup
# ------------------------------------
COPY --chown=$AI_UID:$AI_GID --chmod=744 \
    agents /usr/share/ai-agents-sandbox/agents
COPY --chown=$AI_UID:$AI_GID --chmod=744 \
    skel   /usr/share/ai-agents-sandbox/skel

# =======================
# Config for krun microvm (needed for crun version < 1.27)
# -----------------------
# TODO: use krun_vm.json only for crun < 1.27 in running time.
# The configuration file will fail the running stage with "Bad file descriptor"
# For now better to remove it, as during the build stage, crun could be at a
# different version from the running stage. 
#COPY --chmod=644 .krun_vm.json          /.krun_vm.json

# =============
# RUN Build hooks
# -------------
RUN set -e;\
    for script in $(find /usr/local/bin/ai-agents-sandbox-build-hooks/ \
                    -type f \
                    -name "*.sh" | sort); do\
        sh "$script" || exit 1; \
    done

# =============
# Switch to non-root user for runtime
# -------------
USER $AI_USER

# =====================
# Environment variables
# ---------------------
ENV HOME=/home/aiuser \
    HOSTNAME=ai-sandbox \
    CONTAINER_ID=ai-sandbox \
    PATH=/usr/local/bin:/usr/bin:/bin \
    PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    GH_CONFIG_DIR=/home/aiuser/.config/gh \
    NPM_CONFIG_PREFIX=/home/aiuser/.npm-global \
    TERM=xterm-256color \
    AGENT="${AGENT}"

WORKDIR /home/$AI_USER

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
CMD ["bash", "--login"]
