# Containerfile for a CUPS print server sharing a Brother HL-4150CDN over
# IPP/AirPrint. Build with Podman:
#
#   podman build -t printserver:latest .
#
# Run with host networking so mDNS (Avahi) can advertise the printer. The
# printer is described by environment variables:
#
#   podman run --rm --network host \
#     -e PRINTER_IP=192.168.1.50 \
#     -e PRINTER_NAME=HL-4150CDN \
#     --name printserver printserver:latest
#
# PRINTER_URI can be used instead of PRINTER_IP for full control:
#
#   -e PRINTER_URI=lpd://192.168.1.50/binary_p1
#
FROM debian:bookworm-slim

ENV DEBIAN_FRONTEND=noninteractive

# --- Task 2.1 / 2.2: base system, CUPS, Avahi, DBus and i386 runtime -------
# The Brother filter binaries are 32-bit i386 ELF executables, so the i386
# architecture has to be enabled and the matching runtime libraries installed.
RUN dpkg --add-architecture i386 \
    && apt-get update \
    && apt-get install -y --no-install-recommends \
        cups \
        cups-client \
        cups-filters \
        cups-ppdc \
        avahi-daemon \
        avahi-utils \
        libnss-mdns \
        dbus \
        procps \
        curl \
        ghostscript \
        ca-certificates \
        poppler-utils \
        libc6:i386 \
        libstdc++6:i386 \
        libgcc-s1:i386 \
    && rm -rf /var/lib/apt/lists/*

# Make sure the 32-bit dynamic loader the Brother binaries ask for exists,
# even if the i386 libc places it only in the multiarch directory.
RUN if [ ! -e /lib/ld-linux.so.2 ] && [ -e /lib/i386-linux-gnu/ld-linux.so.2 ]; then \
        ln -s /lib/i386-linux-gnu/ld-linux.so.2 /lib/ld-linux.so.2; \
    fi \
    && test -e /lib/ld-linux.so.2

# --- Task 2.3: install the Brother HL-4150CDN driver packages --------------
# Copy the local driver packages from the repository into the build context and
# install them. The LPR package must be installed before the CUPS wrapper
# package, which depends on it. `--force-all` is used because the packages are
# built for i386 and have no dependency information Debian can satisfy here.
# The whole assets tree is copied because the directory name contains a space,
# which some COPY implementations cannot quote.
COPY assets /tmp/assets

RUN mkdir -p /var/spool/lpd \
    && dpkg -i --force-all "/tmp/assets/Brother HL-4150CDN/lpr-driver/hl4150cdnlpr-1.1.1-5.i386.deb" \
    && dpkg -i --force-all "/tmp/assets/Brother HL-4150CDN/cupswrapper/hl4150cdncupswrapper-1.1.1-5.i386.deb" \
    && dpkg -s hl4150cdnlpr:i386 hl4150cdncupswrapper:i386 >/dev/null \
    && rm -rf /tmp/assets

# --- Task 2.4: configure CUPS and verify the driver files are in place -----
# The CUPS wrapper postinst copies the PPD into the CUPS model directory and
# creates the brlpdwrapper filter. Fail the build if anything is missing.
RUN test -f /usr/share/cups/model/brhl4150cdn.ppd \
    && test -x /usr/lib/cups/filter/brlpdwrapperhl4150cdn \
    && test -x /usr/local/Brother/Printer/hl4150cdn/lpd/brhl4150cdnfilter \
    && mkdir -p /usr/share/ppd \
    && ln -sf /usr/share/cups/model/brhl4150cdn.ppd /usr/share/ppd/brhl4150cdn.ppd \
    && rm -f /etc/cups/printers.conf /etc/cups/printers.conf.O

COPY cupsd.conf /etc/cups/cupsd.conf

# CUPS needs the runtime directories to exist in the image.
RUN mkdir -p /run/cups /run/dbus /run/avahi-daemon /var/spool/cups /var/log/cups

# --- Task 2.5: startup script -----------------------------------------------
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod 0755 /usr/local/bin/entrypoint.sh

# --- Task 2.6: exposed ports ------------------------------------------------
# 631/tcp  CUPS administration and IPP
# 5353/udp mDNS / DNS-SD for AirPrint discovery
EXPOSE 631/tcp
EXPOSE 5353/udp

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
