FROM python:3.11-slim-bookworm

ARG DEBIAN_FRONTEND=noninteractive

ENV VIRTUAL_ENV=/opt/beets-venv \
    PATH="/opt/beets-venv/bin:/music/beets/scripts:/music/beets/scripts/audit:/music/beets/scripts/convert:/music/beets/scripts/spotify_beets_reports:/music/beets/scripts/unknown:${PATH}" \
    HOME=/music/beets \
    BEETSDIR=/music/beets/config \
    XDG_CACHE_HOME=/music/beets/cache \
    LANG=C.UTF-8 \
    LC_ALL=C.UTF-8 \
    PYTHONUNBUFFERED=1 \
    EDITOR=vim \
    PAGER=less

COPY apt-packages.txt /tmp/apt-packages.txt

RUN apt-get update \
  && xargs -r apt-get install -y --no-install-recommends < /tmp/apt-packages.txt \
  && rm -rf /var/lib/apt/lists/* /tmp/apt-packages.txt

RUN python -m venv "$VIRTUAL_ENV" \
  && "$VIRTUAL_ENV/bin/pip" install --no-cache-dir --upgrade pip setuptools wheel

COPY requirements.txt /tmp/requirements.txt

RUN "$VIRTUAL_ENV/bin/pip" install --no-cache-dir -r /tmp/requirements.txt \
  && rm -f /tmp/requirements.txt \
  && printf '%s\n' 'export PATH="/opt/beets-venv/bin:$PATH"' > /etc/profile.d/beets-venv.sh

COPY docker-entrypoint.sh /usr/local/bin/beets-entrypoint
RUN chmod +x /usr/local/bin/beets-entrypoint

WORKDIR /music/beets

ENTRYPOINT ["tini", "--", "beets-entrypoint"]
CMD ["zsh"]
