# docker build -t gerbera-opensuse . -f Dockerfile.opensuse
ARG BASE_IMAGE=opensuse/leap:15.6

FROM ${BASE_IMAGE} AS grb_base

RUN zypper addrepo -G http://ftp.gwdg.de/pub/linux/misc/packman/suse/openSUSE_Leap_15.6/ "Packman"
RUN zypper refresh
RUN zypper update --no-confirm

RUN zypper install --no-confirm \
    sudo \
    bash

FROM grb_base AS builder

RUN zypper install --no-confirm \
    cmake zlib-devel \
    libcurl-devel \
    ffmpeg-7-libavdevice-devel ffmpeg-7-libavutil-devel ffmpeg-7-libswscale-devel \
    ffmpeg-7-libavfilter-devel ffmpeg-7-libpostproc-devel \
    ffmpeg-7-libavcodec-devel ffmpeg-7-libavformat-devel ffmpeg-7-libswresample-devel \
    file-devel \
    gcc13-c++ \
    gcc13 \
    git \
    make \
    patch \
    tar \
    gzip \
    xz \
    lsb-release \
    libmariadb-devel \
    pkgconf \
    sqlite3-devel \
    libuuid-devel \
    # libexecinfo-devel \
    # libexecinfo \
    autoconf \
    automake \
    libtool \
    wget \
    file

ENV CXX=g++-13
ENV CC=gcc-13

# Build googletest
WORKDIR /gtest_build
COPY scripts/install-googletest.sh scripts/versions.sh ./
COPY scripts/opensuse/deps/*.sh ./opensuse/deps/
COPY scripts/opensuse/releases/*.sh ./opensuse/releases/
RUN ./install-googletest.sh

# Build fmt
WORKDIR /fmt_build
COPY scripts/install-fmt.sh scripts/versions.sh ./
COPY scripts/opensuse/deps/*.sh ./opensuse/deps/
COPY scripts/opensuse/releases/*.sh ./opensuse/releases/
RUN ./install-fmt.sh

# Build spdlog
WORKDIR /spdlog_build
COPY scripts/install-spdlog.sh scripts/versions.sh ./
COPY scripts/opensuse/deps/*.sh ./opensuse/deps/
COPY scripts/opensuse/releases/*.sh ./opensuse/releases/
RUN ./install-spdlog.sh

# Build pugixml
WORKDIR /pugixml_build
COPY scripts/install-pugixml.sh scripts/versions.sh ./
COPY scripts/opensuse/deps/*.sh ./opensuse/deps/
COPY scripts/opensuse/releases/*.sh ./opensuse/releases/
RUN ./install-pugixml.sh

# Build taglib
WORKDIR /taglib_build
COPY scripts/install-taglib.sh scripts/versions.sh ./
COPY scripts/opensuse/deps/*.sh ./opensuse/deps/
COPY scripts/opensuse/releases/*.sh ./opensuse/releases/
RUN ./install-taglib.sh

# Build wavpack
WORKDIR /wavpack_build
COPY scripts/install-wavpack.sh scripts/versions.sh ./
COPY scripts/opensuse/deps/*.sh ./opensuse/deps/
COPY scripts/opensuse/releases/*.sh ./opensuse/releases/
RUN ./install-wavpack.sh

# Build ebml
WORKDIR /ebml_build
COPY scripts/install-ebml.sh scripts/versions.sh ./
COPY scripts/opensuse/deps/*.sh ./opensuse/deps/
COPY scripts/opensuse/releases/*.sh ./opensuse/releases/
RUN ./install-ebml.sh

# Build matroska
WORKDIR /mastroska_build
COPY scripts/install-matroska.sh scripts/versions.sh ./
COPY scripts/opensuse/deps/*.sh ./opensuse/deps/
COPY scripts/opensuse/releases/*.sh ./opensuse/releases/
RUN ./install-matroska.sh

# Build duktape
WORKDIR /duktape_build
COPY scripts/install-duktape.sh scripts/versions.sh ./
COPY scripts/opensuse/deps/*.sh ./opensuse/deps/
COPY scripts/opensuse/releases/*.sh ./opensuse/releases/
RUN ./install-duktape.sh

# Build ffmpegthumbnailer
WORKDIR /ffmpegthumbnailer_build
COPY scripts/install-ffmpegthumbnailer.sh scripts/versions.sh ./
COPY scripts/opensuse/deps/*.sh ./opensuse/deps/
COPY scripts/opensuse/releases/*.sh ./opensuse/releases/
RUN ./install-ffmpegthumbnailer.sh

# Build libupnp
WORKDIR /libupnp_build
COPY scripts/install-pupnp.sh scripts/versions.sh ./
COPY scripts/opensuse/deps/*.sh ./opensuse/deps/
COPY scripts/opensuse/releases/*.sh ./opensuse/releases/
RUN ./install-pupnp.sh

# Build libexiv2
WORKDIR /libexiv2_build
COPY scripts/install-libexiv2.sh scripts/versions.sh ./
COPY scripts/opensuse/deps/*.sh ./opensuse/deps/
COPY scripts/opensuse/releases/*.sh ./opensuse/releases/
RUN ./install-libexiv2.sh

# Build libexif
WORKDIR /libexif_build
COPY scripts/install-libexif.sh scripts/versions.sh ./
COPY scripts/opensuse/deps/*.sh ./opensuse/deps/
COPY scripts/opensuse/releases/*.sh ./opensuse/releases/
RUN ./install-libexif.sh

# Build Gerbera
WORKDIR /gerbera_build
COPY . .
RUN cmake -S . -B build --preset=develop \
        -DWITH_SYSTEMD=NO \
        -DBUILD_DOC=NO \
        -DCMAKE_FIND_PACKAGE_PREFER_CONFIG=NO \
    && \
    cmake --build build -v -j$(nproc) && \
    cmake --build build -v -t test ARGS="--output-on-failure"

FROM grb_base AS gerbera

RUN zypper install --no-confirm \
    shadow \
    curl \
    libatomic1 \
    libjpeg8 \
    file \
    expat brotli libinih0 \
    libmariadb3 \
    sqlite3 \
    timezone \
    # libexecinfo \
    # Transcoding \
    ffmpeg-7 \
    libheif1 \
    vlc \
    # Debugging
    gdb

# Copy libupnp
COPY --from=builder /usr/local/lib/libixml.so* /usr/local/lib/libupnp.so* /usr/lib64/
COPY --from=builder /usr/local/lib64/libixml.so* /usr/local/lib64/libupnp.so* /usr/lib64/
# Copy libexiv2
COPY --from=builder /usr/local/lib/libexiv2.so* /usr/lib/
COPY --from=builder /usr/local/lib64/libexiv2.so* /usr/lib64/
# Copy libexif
COPY --from=builder /usr/local/lib/libexif.so* /usr/lib64/
COPY --from=builder /usr/local/lib64/libexif.so* /usr/lib64/
# Copy ffmpegthumbnailer
COPY --from=builder /usr/local/lib/libffmpegthumbnailer.so* /usr/lib/
COPY --from=builder /usr/local/lib64/libffmpegthumbnailer.so* /usr/lib64/

# Copy ebml
# COPY --from=builder /usr/local/lib/libebml.so* /usr/lib/
COPY --from=builder /usr/local/lib64/libebml.so* /usr/lib64/
# Copy mastroska
# COPY --from=builder /usr/local/lib/libmatroska.so* /usr/lib/
COPY --from=builder /usr/local/lib64/libmatroska.so* /usr/lib64/
# Copy wavpack
COPY --from=builder /usr/local/lib/libwavpack.so* /usr/lib64/
COPY --from=builder /usr/local/lib64/libwavpack.so* /usr/lib64/
# Copy fmt
# COPY --from=builder /usr/local/lib/libfmt.so* /usr/lib/
COPY --from=builder /usr/local/lib64/libfmt.so* /usr/lib64/
# Copy spdlog
# COPY --from=builder /usr/local/lib/libspdlog.so* /usr/lib/
COPY --from=builder /usr/local/lib64/libspdlog.so* /usr/lib64/
# Copy duktape
COPY --from=builder /usr/local/lib/libduktape.so* /usr/lib64/
COPY --from=builder /usr/local/lib64/libduktape.so* /usr/lib64/

# Copy Gerbera
COPY --from=builder /gerbera_build/build/gerbera /bin/gerbera
COPY --from=builder /gerbera_build/scripts/js /usr/local/share/gerbera/js
COPY --from=builder /gerbera_build/web /usr/local/share/gerbera/web
COPY --from=builder /gerbera_build/src/database/*/*.sql /gerbera_build/src/database/*/*.xml /usr/local/share/gerbera/
COPY --from=builder /gerbera_build/scripts/docker/docker-entrypoint.sh /usr/local/bin

ARG IMAGE_USER=gerbera
ARG IMAGE_GROUP=gerbera
ARG IMAGE_UID=1042
ARG IMAGE_GID=1042
ARG IMAGE_PORT=49494

RUN groupadd --system ${IMAGE_GROUP} --gid=${IMAGE_GID} 2>/dev/null \
    && useradd --system -d /var/run/gerbera -s /bin/bash -G "${IMAGE_GROUP},video" -g ${IMAGE_GROUP} --uid=${IMAGE_UID} ${IMAGE_USER} 2>/dev/null \
    && mkdir /var/run/gerbera/ \
    && chmod 2775 /var/run/gerbera/ \
    && mkdir /content \
    && chmod 777 /content \
    && ln -s /content /mnt/content \
    && mkdir -p /mnt/customization/js \
    && mkdir -p /mnt/customization/shell \
    && chmod -R 777 /mnt/customization

# Update entrypoint
RUN chmod 0755 /usr/local/bin/docker-entrypoint.sh \
    && sed "s/\$IMAGE_UID/$IMAGE_UID/g" -i /usr/local/bin/docker-entrypoint.sh \
    && sed "s/\$IMAGE_GID/$IMAGE_GID/g" -i /usr/local/bin/docker-entrypoint.sh \
    && sed "s/\$IMAGE_USER/$IMAGE_USER/g" -i /usr/local/bin/docker-entrypoint.sh \
    && sed "s/\$IMAGE_GROUP/$IMAGE_GROUP/g" -i /usr/local/bin/docker-entrypoint.sh

EXPOSE ${IMAGE_PORT}
EXPOSE 1900/udp
ENV IMAGE_PORT=${IMAGE_PORT:-49494}

ENTRYPOINT ["/bin/bash", "--", "docker-entrypoint.sh"]
CMD ["/bin/sh", "-c", "gerbera --port ${IMAGE_PORT} --config /var/run/gerbera/config.xml"]

FROM gerbera AS default
