# syntax=docker/dockerfile:1
# Copyright 2025 Alibaba Group Holding Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


FROM ubuntu:24.04

ARG TARGETARCH

# Use bash for RUN commands to support source and arrays
SHELL ["/bin/bash", "-c"]

ENV DEBIAN_FRONTEND=noninteractive \
    LANG=C.UTF-8 \
    MAVEN_VERSION=3.9.2 \
    MAVEN_HOME=/opt/maven \
    UV_PYTHON_INSTALL_DIR=/opt/python/versions \
    NODE_ROOT=/opt/node \
    GO_ROOT=/opt/go \
    NODE_V18=18.20.3 \
    NODE_V20=20.14.0 \
    NODE_V22=22.2.0 \
    GO_V1_25=1.25.5 \
    GO_V1_24=1.24.11 \
    GO_V1_23=1.23.12

# 1. Install common tools
RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates curl wget git vim nano unzip zip tar build-essential \
    software-properties-common gnupg lsb-release \
    && rm -rf /var/lib/apt/lists/*

# 2. Install Java (8, 11, 17, 21)
RUN add-apt-repository universe && apt-get update && apt-get install -y --no-install-recommends \
    openjdk-8-jdk openjdk-11-jdk openjdk-17-jdk openjdk-21-jdk \
    && rm -rf /var/lib/apt/lists/*

# 3. Install Maven
RUN mkdir -p ${MAVEN_HOME} && \
    curl -fsSL https://archive.apache.org/dist/maven/maven-3/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz \
    | tar -xzC ${MAVEN_HOME} --strip-components=1 && \
    ln -s ${MAVEN_HOME}/bin/mvn /usr/local/bin/mvn

# 4. Install Python (3.10 - 3.14) using uv
RUN curl -LsSf https://astral.sh/uv/install.sh | sh && \
    mv /root/.local/bin/uv /usr/local/bin/uv && \
    mv /root/.local/bin/uvx /usr/local/bin/uvx && \
    mkdir -p /opt/python/versions && \
    uv python install 3.10 3.11 3.12 3.13 && \
    (uv python install 3.14 || echo "Python 3.14 skipped")

# 5. Install Node.js (18, 20, 22)
RUN mkdir -p ${NODE_ROOT} && \
    ARCH="" && \
    if [ -z "${TARGETARCH}" ]; then \
      case "$(uname -m)" in \
        x86_64) TARGETARCH="amd64" ;; \
        aarch64) TARGETARCH="arm64" ;; \
        *) echo "Unsupported architecture: $(uname -m)"; exit 1 ;; \
      esac; \
    fi && \
    case "${TARGETARCH}" in \
      "amd64") ARCH="x64" ;; \
      "arm64") ARCH="arm64" ;; \
      *) echo "Unsupported architecture: ${TARGETARCH}"; exit 1 ;; \
    esac && \
    cd ${NODE_ROOT} && \
    for v in ${NODE_V18} ${NODE_V20} ${NODE_V22}; do \
        curl -fsSL https://nodejs.org/dist/v${v}/node-v${v}-linux-${ARCH}.tar.xz | tar -xJ && \
        mv node-v${v}-linux-${ARCH} v${v}; \
    done

# 6. Install Go (latest three major versions)
RUN mkdir -p ${GO_ROOT} && \
    ARCH="" && \
    if [ -z "${TARGETARCH}" ]; then \
      case "$(uname -m)" in \
        x86_64) TARGETARCH="amd64" ;; \
        aarch64) TARGETARCH="arm64" ;; \
        *) echo "Unsupported architecture: $(uname -m)"; exit 1 ;; \
      esac; \
    fi && \
    case "${TARGETARCH}" in \
      "amd64") ARCH="amd64" ;; \
      "arm64") ARCH="arm64" ;; \
      *) echo "Unsupported architecture: ${TARGETARCH}"; exit 1 ;; \
    esac && \
    cd ${GO_ROOT} && \
    for v in ${GO_V1_25} ${GO_V1_24} ${GO_V1_23}; do \
        curl -fsSL https://go.dev/dl/go${v}.linux-${ARCH}.tar.gz | tar -xz && \
        mv go ${v}; \
    done

# 7. Configure defaults & env script
ENV GOROOT=${GO_ROOT}/${GO_V1_25} \
    PATH="${NODE_ROOT}/v${NODE_V22}/bin:${GO_ROOT}/${GO_V1_25}/bin:${PATH}"

RUN mkdir -p /opt/opensandbox
COPY scripts/code-interpreter-env.sh /opt/opensandbox/code-interpreter-env.sh
RUN chmod +x /opt/opensandbox/code-interpreter-env.sh

CMD ["/bin/bash"]
