summaryrefslogtreecommitdiffstats
path: root/printq
diff options
context:
space:
mode:
Diffstat (limited to 'printq')
-rwxr-xr-xprintq89
1 files changed, 89 insertions, 0 deletions
diff --git a/printq b/printq
new file mode 100755
index 0000000..f6e2f23
--- /dev/null
+++ b/printq
@@ -0,0 +1,89 @@
+#!/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"
+
+ lp "${@}" "${queue}/${file}" || return 1
+ 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=
+
+ 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}" || return 1
+ done
+
+ # Shouldn't reach this point.
+ return 1
+}
+
+usage()
+{
+ printf 'Usage: %s <dir>\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 "${@}"