#!/bin/sh
# CUPS filter for LabelInn virtual printers (macOS and Linux).
#
# A LabelInn queue's backend is a raw socket to the app on 127.0.0.1, which
# carries only the document. CUPS gives a filter the job's options instead:
#   argv: job-id user title copies options [file]
# This filter writes them as a short header and then the document unchanged,
# so the app sees what the user picked in the print dialog (darkness, speed,
# sensor, after-print, copies). The app strips the header again
# (lib/services/virtual_printer/cups_job_options.dart).
#
# Installed root-owned (CUPS refuses filters others can write):
#   Linux  /usr/lib/cups/filter/labelinn-cups-options (the .deb)
#   macOS  /Library/Printers/LabelInn/Filter/labelinn-cups-options
oneline() { printf '%s' "$1" | tr '\r\n' '  '; }
printf '%%LabelInn-Job-Options: %s\n' "$(oneline "$5")"
printf '%%LabelInn-Job-Copies: %s\n' "$(oneline "$4")"
printf '%%LabelInn-Job-Title: %s\n' "$(oneline "$3")"
printf '%%LabelInn-Job-End\n'
if [ -n "$6" ]; then
  exec cat "$6"
fi
exec cat
