#!/bin/sh # Startup script for the containerized CUPS print server. # # Starts the D-Bus system bus, Avahi (mDNS/DNS-SD) and CUPS, then registers # the Brother HL-4150CDN queue if it does not exist yet. The CUPS daemon is # kept in the foreground so the container lives as long as the print server. # # Configuration (all optional, via environment): # PRINTER_IP IP address / host name of the network printer. # PRINTER_URI Full device URI; overrides PRINTER_IP when set. # PRINTER_PROTOCOL lpd (default), socket or ipp - used with PRINTER_IP. # PRINTER_NAME CUPS queue name and advertised (DNS-SD) name. # PRINTER_DESCRIPTION CUPS Description (lpadmin -D) shown in the web UI. # PPD_FILE PPD used for the queue. set -eu PRINTER_NAME="${PRINTER_NAME:-HL4150CDN}" PRINTER_IP="${PRINTER_IP:-}" PRINTER_URI="${PRINTER_URI:-}" PRINTER_PROTOCOL="${PRINTER_PROTOCOL:-lpd}" PRINTER_DESCRIPTION="${PRINTER_DESCRIPTION:-}" PPD_FILE="${PPD_FILE:-/usr/share/cups/model/brhl4150cdn.ppd}" LOG_PREFIX="[entrypoint]" log() { echo "${LOG_PREFIX} $*"; } # Build a device URI from PRINTER_IP unless an explicit PRINTER_URI is given. if [ -z "${PRINTER_URI}" ] && [ -n "${PRINTER_IP}" ]; then case "${PRINTER_PROTOCOL}" in lpd) PRINTER_URI="lpd://${PRINTER_IP}/binary_p1" ;; socket) PRINTER_URI="socket://${PRINTER_IP}:9100" ;; ipp) PRINTER_URI="ipp://${PRINTER_IP}/ipp/print" ;; *) log "unknown PRINTER_PROTOCOL '${PRINTER_PROTOCOL}', defaulting to lpd" PRINTER_URI="lpd://${PRINTER_IP}/binary_p1" ;; esac fi # --- D-Bus system bus ------------------------------------------------------- mkdir -p /run/dbus /var/lib/dbus if [ ! -s /etc/machine-id ] && [ ! -s /var/lib/dbus/machine-id ]; then log "generating D-Bus machine id" dbus-uuidgen > /var/lib/dbus/machine-id fi if [ ! -S /run/dbus/system_bus_socket ]; then log "starting dbus-daemon (system bus)" dbus-daemon --system --fork fi # --- Avahi / mDNS ----------------------------------------------------------- mkdir -p /run/avahi-daemon if ! pgrep -x avahi-daemon >/dev/null 2>&1; then log "starting avahi-daemon" avahi-daemon --daemonize --no-chroot fi # --- CUPS ------------------------------------------------------------------- log "starting cupsd" /usr/sbin/cupsd -f & CUPSD_PID=$! # Wait until CUPS accepts requests. i=0 while [ "$i" -lt 30 ]; do if lpstat -r >/dev/null 2>&1; then break fi i=$((i + 1)) sleep 0.5 done # Share printers on the network and allow remote administration. cupsctl --share-printers --remote-admin >/dev/null 2>&1 || true # --- Brother HL-4150CDN queue ---------------------------------------------- if lpstat -p "${PRINTER_NAME}" >/dev/null 2>&1; then log "printer ${PRINTER_NAME} already exists" elif [ -n "${PRINTER_URI}" ]; then if [ -f "${PPD_FILE}" ]; then log "adding printer ${PRINTER_NAME} -> ${PRINTER_URI}" lpadmin -p "${PRINTER_NAME}" -E -v "${PRINTER_URI}" -P "${PPD_FILE}" \ || log "warning: lpadmin failed to add ${PRINTER_NAME}" else log "warning: PPD ${PPD_FILE} not found; skipping printer creation" fi else log "neither PRINTER_URI nor PRINTER_IP is set; skipping automatic printer creation" fi # Make sure the queue is shared, named for advertising and set as default. if lpstat -p "${PRINTER_NAME}" >/dev/null 2>&1; then lpadmin -p "${PRINTER_NAME}" -o printer-is-shared=true -E >/dev/null 2>&1 || true if [ -n "${PRINTER_DESCRIPTION}" ]; then lpadmin -p "${PRINTER_NAME}" -D "${PRINTER_DESCRIPTION}" >/dev/null 2>&1 || true fi lpadmin -d "${PRINTER_NAME}" >/dev/null 2>&1 || true log "queue ${PRINTER_NAME} is shared and set as default" fi log "ready: CUPS on 631/tcp, mDNS on 5353/udp" # Keep the container running by waiting on cupsd. wait "${CUPSD_PID}"