FROM ubuntu:24.04 AS base

ARG DEBIAN_FRONTEND=noninteractive
ENV PATH=/dotnet:/dotnet/tools:$PATH
ENV DOTNET_ROOT=/dotnet

# Add intel hardware encoding support
ENV LIBVA_DRIVERS_PATH="/usr/lib/x86_64-linux-gnu/dri" \
    LD_LIBRARY_PATH="/usr/lib/x86_64-linux-gnu" \
    NVIDIA_DRIVER_CAPABILITIES="compute,video,utility" \
    NVIDIA_VISIBLE_DEVICES="all" \
    DOTNET_CLI_TELEMETRY_OPTOUT=true

RUN apt-get update && \
    apt-get install -y tzdata wget ca-certificates gnupg curl tar xz-utils libssl-dev apt-transport-https openssl locales libfontconfig1 libfreetype6 pciutils vainfo git pip
    
# MODS# Install mods and architecture-specific packages
RUN apt-get install -y comskip mkvtoolnix ccextractor aom-tools svt-av1 x265 x264 nano p7zip-full htop imagemagick vainfo libva-dev mesa-va-drivers && \
    if [ "$(dpkg --print-architecture)" = "amd64" ]; then \
        apt-get install -y rar unrar libmfx-dev libmfx-gen1.2 intel-media-va-driver-non-free i965-va-driver-shaders; \
    fi && \
    rm -rf /var/lib/apt/lists/*

# Install ffmpeg6 from jellyfin
RUN wget -O - https://repo.jellyfin.org/jellyfin_team.gpg.key | gpg --dearmor -o /usr/share/keyrings/jellyfin.gpg && \
    os_id=$(awk -F'=' '/^ID=/{ print $NF }' /etc/os-release) && \
    os_codename=$(awk -F'=' '/^VERSION_CODENAME=/{ print $NF }' /etc/os-release) && \
    echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/jellyfin.gpg] https://repo.jellyfin.org/$os_id $os_codename main" | tee /etc/apt/sources.list.d/jellyfin.list && \
    apt-get update && \
    apt-get install -y --no-install-recommends jellyfin-ffmpeg6 && \
    ln -s /usr/lib/jellyfin-ffmpeg/ffmpeg /usr/local/bin/ffmpeg && \
    ln -s /usr/lib/jellyfin-ffmpeg/ffmpeg /usr/local/bin/ffmpeg6 && \
    ln -s /usr/lib/jellyfin-ffmpeg/ffprobe /usr/local/bin/ffprobe && \
    rm -rf /var/lib/apt/lists/* && \
    ffmpeg --help


# Install dotnet SDK
RUN wget https://dot.net/v1/dotnet-install.sh  && \
    bash dotnet-install.sh -c 8.0 --install-dir /dotnet && \
    rm -f dotnet-install.sh && \
    chmod 775 /dotnet
    
##########################################
### actual FileFlows stuff happens now ###
##########################################

COPY /deploy /app

RUN chmod +x /app/docker-entrypoint.sh

RUN /dotnet/dotnet dev-certs https

# expose the ports we need
EXPOSE 5000

# set the working directory
WORKDIR /app

ENTRYPOINT ["/app/docker-entrypoint.sh"]