#!/bin/bash

BULLETIN_DIR="/opt/rtsda/church-api/uploads/bulletins"
PRINTER="Brother_MFC_L8900CDW"

# Find the most recent PDF file by modification time
LATEST_BULLETIN=$(ls -t "$BULLETIN_DIR"/*.pdf 2>/dev/null | head -n1)

if [ -n "$LATEST_BULLETIN" ]; then
    echo "Printing: $LATEST_BULLETIN"
    lp -d "$PRINTER" \
       -o 20 \
       -o PageSize=Letter \
       -o Duplex=DuplexTumble \
       -o ColorModel=RGB \
       "$LATEST_BULLETIN"
    echo "Print job sent successfully"
else
    echo "No bulletin found in $BULLETIN_DIR"
fi