#        -*- Dockerfile -*-
#
# Recipe to build a container image for dash-mpd-cli + helper applications, for Linux/ppc64le.
#
# This Containerfile contains the recipe needed to generate a docker/podman/OCI container image
# including the dash-mpd-cli binary alongside the external helper applications that it uses for
# muxing media streams, for extracting/converting subtitle streams, and for decrypting content
# infected with DRM. These are packaged with a minimal Alpine Linux installation.
#
# To build the container locally (not needed for an end user)
#
#   podman manifest create dash-mpd-cli
#   podman build -f etc/Containerfile_linux_ppc64le --platform linux/ppc64le --tag dash-mpd-cli-linux-ppc64le --manifest dash-mpd-cli .


# We make a static build of MP4Box from GPAC, which is not packaged for Alpine Linux.
# https://github.com/gpac/gpac/wiki/GPAC-Build-Guide-for-Linux
#
# We build shaka-packager from source, because it's not packed for Alpine Linux and not distributed
# in binary form for ppc64le.
FROM --platform=linux/ppc64le docker.io/alpine:latest AS builder
WORKDIR /src
COPY ./ ./
RUN apk update && \
    apk upgrade && \
    apk add --no-cache bash curl bsd-compat-headers linux-headers build-base file musl-dev pkgconfig git g++ binutils make cmake zlib-dev zlib-static ninja python3 cargo rust && \
    cd /src && git clone --recurse-submodules https://github.com/shaka-project/shaka-packager.git && \
    # We see C compiler errors when building with a build type of Release. The official Dockerfile is 
    # building with BUILD_TYPE of Debug...
    cd shaka-packager && env CFLAGS=-Wno-error cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug && \
    env CFLAGS=-Wno-error cmake --build build/ && \
    ls -l build/packager && \
    file build/packager/shaka-packager
    cargo update && \
    cargo build --release && \
    git clone https://github.com/gpac/gpac.git && \
    cd gpac && ./configure --static-bin && \
    make -j 4 && \
    ls -l bin/gcc/ && \
    file bin/gcc/MP4Box &&


# Now build the final image
FROM --platform=linux/ppc64le docker.io/alpine:latest
LABEL org.opencontainers.image.description="Download media content from a DASH-MPEG or DASH-WebM MPD manifest." \
    org.opencontainers.image.title="dash-mpd-cli" \
    org.opencontainers.image.url="https://github.com/emarsden/dash-mpd-cli" \
    org.opencontainers.image.source="https://github.com/emarsden/dash-mpd-cli" \
    org.opencontainers.image.version="0.2.11" \
    org.opencontainers.image.authors="eric.marsden@risk-engineering.org" \
    org.opencontainers.image.licenses="MIT,GPL-2.0-or-later"

# Install our external dependencies. Licences for the external dependencies:
#   - ffmpeg: GNU GPL v2
#   - mkvmerge (from mkvtoolnix): GNU GPL v2
#   - vlc: GNU GPL v2, not installed because it inflates image size considerably
#   - mp4decrypt (from bento4): GNU GPL v2
#   - xsltproc (from libxslt): MIT
#   - Shaka packager: MIT
RUN apk update && \
    apk upgrade && \
    apk add --no-cache ca-certificates libc6-compat wget ffmpeg mkvtoolnix bento4 libxslt && \
    update-ca-certificates && \
    mkdir /content && \
    chown root.root /content && \
    chmod a=rwx,o+t /content

COPY --from=builder --chown=root:root --chmod=755 \
    /src/target/aarch64-unknown-linux-musl/release/dash-mpd-cli /usr/local/bin
COPY --from=builder --chown=root:root --chmod=755 \
    /src/gpac/bin/gcc/MP4Box /usr/local/bin
COPY --from=builder --chown=root:root --chmod=755 \
    /src/shaka-packager/build/packager/shaka-packager /usr/local/bin

WORKDIR /content
ENTRYPOINT ["/usr/local/bin/dash-mpd-cli"]

# Size of our container image:
#   with vlc:     331 MB
#   without vlc:  203 MB  (aarch64)
