# Jellyfin 10.11 for POWER8 (ppc64le)
# Based on jellyfin-packaging/docker/Dockerfile, adapted for Fedora/ppc64le
ARG DOTNET_VERSION=10.0
ARG NODEJS_VERSION=22

#
# Build the web artifacts
#
FROM fedora:43 AS web

RUN dnf install -y nodejs npm git autoconf automake gcc g++ make \
    libpng-devel nasm python3 && dnf clean all

WORKDIR /src
COPY jellyfin-web .

# sass-embedded has no ppc64le binary - force pure JS sass
RUN npm ci --no-audit --unsafe-perm && \
    rm -rf node_modules/sass-embedded node_modules/sass-embedded-* && \
    npm install sass@^1.77.0 --save-dev && \
    sed -i "s/'sass-loader'/{loader: 'sass-loader', options: {implementation: require('sass')}}/" webpack.common.js && \
    npm run build:production && \
    mv dist /web

#
# Build the server artifacts
#
FROM fedora:43 AS server

RUN dnf install -y dotnet-sdk-${DOTNET_VERSION:-10.0} && dnf clean all

WORKDIR /src
COPY jellyfin-server .

ENV DOTNET_CLI_TELEMETRY_OPTOUT=1

# Patch global.json to allow .NET 10 (ppc64le only has .NET 10 on Fedora)
RUN sed -i 's/"latestMinor"/"latestMajor"/' global.json

# Remove SkiaSharp dependency - no native lib for ppc64le
# Patch CoreAppHost to always use NullImageEncoder
RUN sed -i '/Jellyfin.Drawing.Skia/d' Jellyfin.Server/Jellyfin.Server.csproj && \
    sed -i '/using Jellyfin.Drawing.Skia/d' Jellyfin.Server/CoreAppHost.cs && \
    sed -i 's/bool useSkiaEncoder = SkiaEncoder.IsNativeLibAvailable();/bool useSkiaEncoder = false;/' Jellyfin.Server/CoreAppHost.cs && \
    sed -i 's/? typeof(SkiaEncoder)/? typeof(NullImageEncoder)/' Jellyfin.Server/CoreAppHost.cs

RUN dotnet publish Jellyfin.Server \
    --configuration Release \
    --output /server \
    --self-contained false \
    -p:DebugSymbols=false \
    -p:DebugType=none \
    -p:UseAppHost=false \
    -p:SkiaSharpSkipRuntimesPackage=true

#
# Final runtime image
#
FROM fedora:43

ENV HEALTHCHECK_URL=http://localhost:8096/health \
    LC_ALL="en_US.UTF-8" \
    LANG="en_US.UTF-8" \
    JELLYFIN_DATA_DIR="/config" \
    JELLYFIN_CACHE_DIR="/cache" \
    JELLYFIN_CONFIG_DIR="/config/config" \
    JELLYFIN_LOG_DIR="/config/log" \
    JELLYFIN_WEB_DIR="/jellyfin/jellyfin-web" \
    JELLYFIN_FFMPEG="/usr/bin/ffmpeg" \
    XDG_CACHE_HOME="/cache" \
    MALLOC_TRIM_THRESHOLD_=131072

RUN dnf install -y \
    aspnetcore-runtime-10.0 \
    dotnet-runtime-10.0 \
    ffmpeg-free \
    fontconfig freetype libpng libjpeg-turbo \
    libicu curl \
    langpacks-en glibc-langpack-en \
    && dnf clean all

RUN mkdir -p ${JELLYFIN_DATA_DIR} ${JELLYFIN_CACHE_DIR} \
    && chmod 777 ${JELLYFIN_DATA_DIR} ${JELLYFIN_CACHE_DIR}

COPY --from=server /server /jellyfin
COPY --from=web /web /jellyfin/jellyfin-web

LABEL "org.opencontainers.image.title"="Jellyfin" \
      "org.opencontainers.image.description"="Jellyfin Media Server for POWER8 (ppc64le)"

EXPOSE 8096
VOLUME ${JELLYFIN_DATA_DIR} ${JELLYFIN_CACHE_DIR}
ENTRYPOINT ["dotnet", "/jellyfin/jellyfin.dll"]

HEALTHCHECK --interval=30s --timeout=30s --start-period=10s --retries=3 \
    CMD curl --noproxy 'localhost' -Lk -fsS "${HEALTHCHECK_URL}" || exit 1
