# Minimal Android Cast CI image (glibc-based; NDK prebuilts require linux-x86_64 glibc).
#
# Build:
#   docker build --build-arg ANDROIDCAST_CI_VERSION=00.01.00.1000 -t androidcast-ci:00.01.00.1000 -f Dockerfile .
#
# Run CI build (mount repo):
#   docker run --rm -v "$(pwd)":/workspace -w /workspace androidcast-ci:latest
#
# CI image version (AA.BB.CC.DDDD — AA always 00)
ARG ANDROIDCAST_CI_MAJOR=00
ARG ANDROIDCAST_CI_MINOR=01
ARG ANDROIDCAST_CI_PATCH=00
ARG ANDROIDCAST_CI_BUILD=1000
ARG ANDROIDCAST_CI_VERSION=00.01.00.1000

FROM debian:bookworm-slim

ARG ANDROIDCAST_CI_VERSION
LABEL org.androidcast.ci.version="${ANDROIDCAST_CI_VERSION}"
LABEL org.opencontainers.image.title="androidcast-ci"
LABEL org.opencontainers.image.version="${ANDROIDCAST_CI_VERSION}"

ARG DEBIAN_FRONTEND=noninteractive
ARG ANDROID_SDK_ROOT=/opt/android-sdk
ARG ANDROID_NDK_VERSION=27.0.12077973
ARG ANDROID_CMAKE_VERSION=3.22.1
ARG CMDLINE_TOOLS_REV=11076708

ENV ANDROID_SDK_ROOT="${ANDROID_SDK_ROOT}" \
    ANDROID_HOME="${ANDROID_SDK_ROOT}" \
    ANDROID_NDK_HOME="${ANDROID_SDK_ROOT}/ndk/${ANDROID_NDK_VERSION}" \
    ANDROID_NDK_ROOT="${ANDROID_SDK_ROOT}/ndk/${ANDROID_NDK_VERSION}" \
    PATH="${ANDROID_SDK_ROOT}/cmdline-tools/latest/bin:${PATH}" \
    ANDROIDCAST_CCACHE=1 \
    ANDROIDCAST_USE_DISTCC=0 \
    ANDROIDCAST_CI_VERSION="${ANDROIDCAST_CI_VERSION}" \
    GRADLE_USER_HOME=/root/.gradle \
    JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64

RUN apt-get update && apt-get install -y --no-install-recommends \
        bash ca-certificates curl git openjdk-17-jdk-headless unzip wget \
        make ninja-build cmake ccache \
    && rm -rf /var/lib/apt/lists/*

RUN apt-get update && apt-get install -y --no-install-recommends distcc \
    && rm -rf /var/lib/apt/lists/* || true

RUN mkdir -p "${ANDROID_SDK_ROOT}/cmdline-tools" \
    && curl -fsSL "https://dl.google.com/android/repository/commandlinetools-linux-${CMDLINE_TOOLS_REV}_latest.zip" \
        -o /tmp/cmdline-tools.zip \
    && unzip -q /tmp/cmdline-tools.zip -d "${ANDROID_SDK_ROOT}/cmdline-tools" \
    && mv "${ANDROID_SDK_ROOT}/cmdline-tools/cmdline-tools" "${ANDROID_SDK_ROOT}/cmdline-tools/latest" \
    && rm -f /tmp/cmdline-tools.zip

RUN yes | sdkmanager --licenses >/dev/null \
    && sdkmanager \
        "platform-tools" \
        "platforms;android-34" \
        "build-tools;34.0.0" \
        "ndk;${ANDROID_NDK_VERSION}" \
        "cmake;${ANDROID_CMAKE_VERSION}"

WORKDIR /workspace

RUN mkdir -p /workspace/build/.ccache || true

COPY scripts/ci-build.sh scripts/ci-build-and-publish-ota.sh scripts/generate-ota-v0.sh \
    scripts/android-ndk.sh scripts/native-build-cache.sh scripts/build-native-codecs.sh \
    /workspace/scripts/

RUN chmod +x /workspace/scripts/ci-build.sh /workspace/scripts/ci-build-and-publish-ota.sh \
    /workspace/scripts/generate-ota-v0.sh /workspace/scripts/build-native-codecs.sh

# Full tree is bind-mounted in CI; default command runs the pipeline.
# OTA staging publish: ./scripts/ci-build-and-publish-ota.sh (see docs/BUILD_DEPLOY.md).
CMD ["./scripts/ci-build.sh"]
