FROM vitasdk/vitasdk:latest

# The published image has changed base distro over time (Alpine -> Ubuntu), so
# probe for the package manager instead of assuming `apk`. Recent images
# already ship cmake/make/git, in which case this layer is a no-op.
RUN if command -v apk >/dev/null 2>&1; then \
        apk add --no-cache cmake make git ca-certificates; \
    elif command -v apt-get >/dev/null 2>&1; then \
        apt-get update && apt-get install -y --no-install-recommends \
            cmake make git ca-certificates && rm -rf /var/lib/apt/lists/*; \
    fi

# vdpm is pre-installed at /usr/local/vitasdk/bin/vdpm; -f = force install.
#
# NOTE 1: `openssl` (1.0.2) is deliberately NOT listed. The SDK's libcurl.a
# (curl 8.17) is prebuilt against OpenSSL 1.0.2, but the image ships
# openssl-1.1.1 as the installed default (a dependency of cpython3) and the two
# vdpm packages conflict, so `vdpm openssl` aborts and leaves libcurl
# referencing 1.0.2-only symbols (EVP_MD_CTX_create, SSLeay,
# SSLv23_client_method, ...) that 1.1.1 does not provide -> link failure.
# The RUN below overlays the matching 1.0.2 libs/headers from the tarball.
#
# NOTE 2: vita2d is NOT a vdpm package (it ships with the SDK core).
RUN yes | vdpm -f zlib libpng libjpeg-turbo freetype curl zstd

# Overlay the OpenSSL 1.0.2 build libcurl was compiled against (see above).
# This is the `openssl` vdpm package from the release snapshot the image's own
# package DB points at; the SHA256 is the one recorded in that DB.
ARG OPENSSL_URL=https://github.com/vitasdk/vitasdk-autobuild/releases/download/packages-2026.08-snapshot-20260925.50.1/openssl-1.0.2.r12304.gcd52124-1-vita.pkg.tar.xz
ADD ${OPENSSL_URL} /tmp/openssl102.tar.xz
RUN echo "1f97f3658fa6099354547202e2a9df6fa2919c60bcce314c83e99ef25de8998e  /tmp/openssl102.tar.xz" | sha256sum -c - && \
    mkdir -p /tmp/o102 && tar xJf /tmp/openssl102.tar.xz -C /tmp/o102 && \
    cp -a /tmp/o102/arm-vita-eabi/lib/libcrypto.a \
          /tmp/o102/arm-vita-eabi/lib/libssl.a \
          /usr/local/vitasdk/arm-vita-eabi/lib/ && \
    cp -a /tmp/o102/arm-vita-eabi/include/openssl/. \
          /usr/local/vitasdk/arm-vita-eabi/include/openssl/ && \
    rm -rf /tmp/o102 /tmp/openssl102.tar.xz

WORKDIR /src
COPY . .

RUN cmake -B /build -DCMAKE_BUILD_TYPE=Release . && cmake --build /build --parallel 4

# Mount a host directory to /output to retrieve the VPK
CMD ["sh", "-c", "cp /build/vitaImmich.vpk /output/vitaImmich.vpk && echo 'Done: /output/vitaImmich.vpk'"]