FROM debian:bookworm-slim AS build

# https://raw.githubusercontent.com/MusicPlayerDaemon/MPD/v0.24.1/NEWS
# https://mpd.readthedocs.io/en/latest/user.html#compiling-from-source

# install build dependencies and the desired library headers
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
      curl \
      ca-certificates \
      xz-utils \
      meson g++ \
      libfmt-dev \
      libpcre2-dev \
      #libmad0-dev libmpg123-dev \
      libid3tag0-dev \
      libflac-dev libopus-dev libogg-dev \
      #libvorbis-dev \
      #libadplug-dev libaudiofile-dev libsndfile1-dev libfaad-dev \
      #libfluidsynth-dev libgme-dev libmikmod-dev libmodplug-dev \
      #libmpcdec-dev libwavpack-dev libwildmidi-dev \
      #libsidplay2-dev libsidutils-dev libresid-builder-dev \
      libavcodec-dev libavformat-dev libavfilter-dev \
      libmp3lame-dev \
      #libtwolame-dev libshine-dev \
      libsamplerate0-dev libsoxr-dev \
      #libbz2-dev libcdio-paranoia-dev libiso9660-dev libmms-dev \
      #libzzip-dev \
      libcurl4-openssl-dev libyajl-dev libexpat-dev \
      #libasound2-dev libao-dev libjack-jackd2-dev libopenal-dev \
      #libpulse-dev libshout3-dev \
      #libsndio-dev \
      #libmpdclient-dev \
      #libnfs-dev \
      #libupnp-dev \
      #libavahi-client-dev \
      libsqlite3-dev \
      #libsystemd-dev \
      #libgtest-dev \
      libboost-dev \
      libicu-dev && \
      #ylibchromaprint-dev \
      #libgcrypt20-dev \
    rm -rf /var/lib/apt/lists/* && \
    find /var/log -type f -name '*.log*' -delete

# get release tarball
ENV MPD_VERSION=0.24.1
ENV MPD_VERSION_MAJOR=0.24
RUN curl -fsSL -o mpd-${MPD_VERSION}.tar.xz https://www.musicpd.org/download/mpd/${MPD_VERSION_MAJOR}/mpd-${MPD_VERSION}.tar.xz && \
    tar xf mpd-${MPD_VERSION}.tar.xz && \
    ln -s /mpd-${MPD_VERSION} /build

# alternatively, cherry-pick a certain commit
#ENV MPD_COMMIT=b6e187efd8520ca9e3541e630559246c893cc304
#RUN curl -fsSL -o mpd-${MPD_COMMIT}.tar.gz https://github.com/MusicPlayerDaemon/MPD/archive/${MPD_COMMIT}.tar.gz && \
#    tar xzf mpd-${MPD_COMMIT}.tar.gz && \
#    ln -s /MPD-${MPD_COMMIT} /build

# or, copy a local checkout
#COPY MPD/ /build/

# configure features and build
WORKDIR /build
RUN meson . output/release \
      --buildtype=release --auto-features=disabled -Db_ndebug=if-release \
      --prefix=/usr --sysconfdir=/etc --mandir=/usr/share/man --localstatedir=/var \
      -Dcue=false \
      -Ddsd=false \
      -Dfifo=false \
      -Dhtml_manual=false \
      -Dmanpages=false \
      -Dneighbor=false \
      -Dpipe=false \
      -Drecorder=false \
      -Dsnapcast=false \
      -Dwave_encoder=false \
      -Dcurl=enabled \
      -Dffmpeg=enabled \
      -Diconv=enabled \
      -Dicu=enabled \
      -Did3tag=enabled \
      -Dflac=enabled \
      -Dopus=enabled \
      -Dlame=enabled \
      -Dlibsamplerate=enabled \
      -Dpcre=enabled \
      -Dsoxr=enabled \
      -Dsqlite=enabled && \
    meson configure output/release && \
    ninja -j 2 -C output/release
RUN ls -l output/release && \
    ldd output/release/mpd | sort && \
    output/release/mpd -V

# start from scratch
FROM debian:bookworm-slim

# install the desired runtime libraries
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
      ca-certificates \
      libmp3lame0 libflac12 libopus0 libopusenc0 libogg0 \
      libavcodec59 libavformat59 libswresample4 libavfilter-extra8 \
      libsamplerate0 libsoxr0 libid3tag0 \
      libsqlite3-0 libpcre3 libcurl4 libfmt9 libicu72 && \
    rm -rf /var/lib/apt/lists/* && \
    find /var/log -type f -name '*.log*' -delete

# get the main binary from previous build stage
COPY --from=build /build/output/release/mpd /usr/bin/

# check all dependencies are available
RUN ldd /usr/bin/mpd | sort && /usr/bin/mpd -V

# done
ENTRYPOINT ["/usr/bin/mpd", "--no-daemon"]
# EXPOSE 6600
# VOLUME /etc/mpd.conf