FROM debian:bullseye AS ffmpeg
ARG DEBIAN_FRONTEND=noninteractive
WORKDIR /static
ARG TARGETPLATFORM
RUN echo ${TARGETPLATFORM}
RUN apt update && \
    apt install -y --no-install-recommends wget unzip tar ca-certificates xz-utils

RUN if [ "${TARGETPLATFORM}" = "linux/amd64" ]; then \
    wget https://github.com/Dusk-Labs/ffmpeg-static/releases/download/ffmpeg-all-0.0.1/ffmpeg && \
    wget https://github.com/Dusk-Labs/ffmpeg-static/releases/download/ffmpeg-all-0.0.1/ffprobe && \
    ls -la . && \
    pwd \
    ; fi
    
RUN if [ "${TARGETPLATFORM}" = "linux/arm64" ]; then \
    wget https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-arm64-static.tar.xz && \
    tar --strip-components 1 -xf ffmpeg-release-arm64-static.tar.xz \
    ; fi
    
RUN if [ "${TARGETPLATFORM}" = "linux/arm/v7" ]; then \
    wget https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-armhf-static.tar.xz && \
    tar --strip-components 1 -xf ffmpeg-release-armhf-static.tar.xz \
    ; fi
    
RUN chmod +x /static/ffmpeg && chmod +x /static/ffprobe
# Smoke Test
#RUN /static/ffmpeg -version
#RUN /static/ffprobe -version

FROM debian:bullseye AS dim
ARG DEBIAN_FRONTEND=noninteractive
ARG TARGETPLATFORM
WORKDIR /dim
COPY bin/ bin/
RUN ls -al bin/
RUN mkdir -p target/

RUN if [ "${TARGETPLATFORM}" = "linux/amd64" ]; then \
    mv bin/amd64-bin/dim target/dim && \
    chmod +x target/dim && \
    ls -la target/ . && \
    pwd \
    ; fi
    
RUN if [ "${TARGETPLATFORM}" = "linux/arm64" ]; then \
    mv bin/aarch64-bin/dim target/dim && \
    chmod +x target/dim && \
    ls -la target/ . && \
    pwd \
    ; fi
    
RUN if [ "${TARGETPLATFORM}" = "linux/arm/v7" ]; then \
    mv bin/armhf-bin/dim target/dim && \
    chmod +x target/dim && \
    ls -la target/ . && \
    pwd \
    ; fi


FROM debian:bullseye
ENV RUST_BACKTRACE=full
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y \
    ca-certificates \
    libfontconfig \
    libfribidi0 \
    libharfbuzz0b \
    libtheora0 \
    libva-drm2 \
    libva2 \
    libvorbis0a \
    libvorbisenc2 \
    && rm -rf /var/lib/apt/lists/*
COPY --from=ffmpeg /static/ffmpeg /opt/dim/utils/ffmpeg
COPY --from=ffmpeg /static/ffprobe /opt/dim/utils/ffprobe
COPY --from=dim /dim/target/dim /opt/dim/dim

EXPOSE 8000
VOLUME ["/opt/dim/config"]

ENV RUST_LOG=info
WORKDIR /opt/dim
CMD ["./dim"]
