# syntax=docker/dockerfile:1.7
FROM docker/sandbox-templates:shell-nightly

USER root
RUN apt-get update && apt-get install -y --no-install-recommends \
      build-essential pkg-config libssl-dev curl ca-certificates git jq \
      ripgrep fd-find fzf sqlite3 less procps \
    && rm -rf /var/lib/apt/lists/* \
    && ln -sf /usr/bin/fdfind /usr/local/bin/fd

USER agent

# OMP's runtime/dependencies.
RUN mkdir -p /home/agent/.local/bin /home/agent/.omp /home/agent/workspace \
    && curl -fsSL https://bun.sh/install | bash
ENV BUN_INSTALL=/home/agent/.bun
ENV PATH="${BUN_INSTALL}/bin:${PATH}"

ARG OMP_VERSION=latest
RUN set -eux; \
    arch="$(uname -m)"; \
    case "$arch" in \
      x86_64|amd64) omp_arch=x64 ;; \
      aarch64|arm64) omp_arch=arm64 ;; \
      *) echo "unsupported architecture: $arch" >&2; exit 1 ;; \
    esac; \
    if [ "$OMP_VERSION" = "latest" ]; then \
      OMP_VERSION="$(curl -fsSL https://api.github.com/repos/can1357/oh-my-pi/releases/latest \
        | sed -n 's/.*"tag_name":[[:space:]]*"v\([^"]*\)".*/\1/p' | head -1)"; \
    fi; \
    test -n "$OMP_VERSION"; \
    curl -fsSL \
      "https://github.com/can1357/oh-my-pi/releases/download/v${OMP_VERSION}/omp-linux-${omp_arch}" \
      -o /home/agent/.local/bin/omp; \
    chmod +x /home/agent/.local/bin/omp; \
    omp --version

COPY sbx-kit/omp-init.sh /usr/local/bin/omp-init.sh
USER root
RUN chmod +x /usr/local/bin/omp-init.sh
USER agent

ENV HOME=/home/agent \
    PI_CONFIG_DIR=.omp

WORKDIR /home/agent/workspace

LABEL com.docker.sandboxes.flavor="omp"
