#!/bin/sh # # printq - Watches a directory of print queues and submits files for printing # # Copyright (C) 2021 P. J. McDermott # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. set -eu print_from_queue() { local dest="${1}" local queue="${2}" local file="${3}" shift 3 local opt= set -- -d "${dest}" while read opt; do set -- "${@}" -o "${opt}" done 0<"${queue}/.options" logger -t 'printq' "Printing ${queue}/${file}" if ! out="$(lp "${@}" "${queue}/${file}" 2>&1)"; then logger -t 'printq' "Failed to print: ${out}" return 1 fi rm -f "${queue}/${file}" || return 1 return 0 } watch_queues() { local dir="${1}" shift 1 local watched_filename= local event_names= local event_filename= local dest= logger -t 'printq' "Watching queues under ${dir}" inotifywait -m -r -q -e close_write "${dir}" | \ while read -r watched_filename event_names event_filename; do dest="${watched_filename#${dir}}" while ! [ x"${dest}" = x"${dest#/}" ]; do dest="${dest#/}" done dest="${dest%%/*}" print_from_queue "${dest}" "${watched_filename}" \ "${event_filename}" || : # Don't exit on error. done # Shouldn't reach this point. logger -t 'printq' 'Failed to watch queues' return 1 } usage() { printf 'Usage: %s \n' "${0}" return 0 } main() { local dir= if [ ${#} -ne 1 ]; then usage 1>&2 return 1 fi dir="${1}" shift 1 watch_queues "${dir}" || return 1 return 0 } main "${@}"