#!/bin/sh
# The Document Viewer (Papers) with its own print dialog, not the desktop portal's. The portal's dialog is a separate
# GTK3 program that keeps the print list it read when it started, so it could still pick "Print to LPR" on a computer
# with no printer, and Print then closed with no message and printed nothing (QA 20261106-0602 001). It also hands the
# app its internal temporary file (/tmp/gtkprintXXXXXX) as the Print to File target, which Papers then offered next
# time and asked to overwrite, instead of the document's name in Documents (002). Papers' own dialog reads
# /etc/xdg/gtk-4.0/settings.ini (printers and Print to File only) when Papers starts, and names the file after the
# document. The desktop entry runs "papers", found here before /usr/bin.
# GTK uses FileChooser portals by default. Remove the earlier global opt-out; disable Print alone.
export GDK_DEBUG="$(printf '%s' "${GDK_DEBUG:-}" | sed 's/no-portals//g;s/portals//g;s/^,//;s/,$//')"
if [ -r /usr/lib/flea/papers-print.so ]; then
  export LD_PRELOAD="/usr/lib/flea/papers-print.so${LD_PRELOAD:+:$LD_PRELOAD}"
fi
# With no printer set up, that dialog offers only Print to File and says nothing: a watcher for this viewer ($$ stays
# its process number after exec) says so when the dialog opens and offers Settings > Printers (QA 20261106-1742 001).
if command -v starchaser >/dev/null 2>&1; then
  starchaser printers watch-print "$$" </dev/null >/dev/null 2>&1 &
fi
exec /usr/bin/papers "$@"
