aboutsummaryrefslogtreecommitdiff
path: root/src/bin/print
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/print')
-rwxr-xr-xsrc/bin/print146
1 files changed, 146 insertions, 0 deletions
diff --git a/src/bin/print b/src/bin/print
new file mode 100755
index 00000000..ce069417
--- /dev/null
+++ b/src/bin/print
@@ -0,0 +1,146 @@
+#!/bin/sh
+set -eu
+
+usage() {
+ cat <<-'EOF'
+ Usage:
+ print [-d] [-q QUALITY] [FILE...]
+ print -h
+EOF
+}
+
+help() {
+ cat <<-'EOF'
+
+ Options:
+ -d print duplex/double-sided
+ -q QUALITY choose the print quality, either:
+ low, medium (default) or high.
+ -h, --help show this message
+
+ Examples:
+
+ Print the given PostScript file with default quality:
+ print f1.ps
+
+ Print multiple PDF files with high quality:
+ print -q high *.pdf
+
+ Print the file from STDIN, double-sided:
+ print -d < f2.ps
+EOF
+}
+
+mkdtemp() {
+ name="$(echo 'mkstemp(template)' |
+ m4 -D template="${TMPDIR:-/tmp}/m4-tmpname.")"
+ rm -f "$name"
+ mkdir "$name"
+ echo "$name"
+}
+
+n_pages() {
+ pdftk "$1" dump_data | awk '/NumberOfPages/ { print $2 }'
+}
+
+main() {
+ if file -b in-tmp | grep -q PostScript; then
+ ps2pdf in-tmp in.pdf
+ elif file -b in-tmp | grep -q PDF; then
+ cp in-tmp in.pdf
+ else
+ enscript -o- in-tmp | ps2pdf - in.pdf
+ fi
+
+ if [ -z "$DUPLEX" ]; then
+ lp in.pdf
+ return
+ fi
+
+ if [ "$(n_pages in.pdf)" = '1' ]; then
+ lp in.pdf
+ return
+ fi
+
+ pdftk A=in.pdf cat Aodd output odd.pdf
+ pdftk A=in.pdf cat Aeven output even.pdf
+
+ NODD="$(n_pages odd.pdf)"
+ NEVEN="$(n_pages even.pdf)"
+
+ printf 'Printing odd pages...\n' >&2
+ lp odd.pdf
+ printf 'Has printing finished yet? Once it does, reload the pages and hit it enter to continue. '
+ read -r < /dev/tty
+ lp even.pdf
+
+ if [ "$NODD" != "$NEVEN" ]; then
+ printf '\n' | lp
+ fi
+}
+
+for flag in "$@"; do
+ case "$flag" in
+ --)
+ break
+ ;;
+ --help)
+ usage
+ help
+ exit
+ ;;
+ *)
+ ;;
+ esac
+done
+
+lpoptions -o PrintQuality=standard
+DUPLEX=
+while getopts 'dq:h' flag; do
+ case "$flag" in
+ d)
+ DUPLEX=1
+ ;;
+ q)
+ case "$OPTARG" in
+ low)
+ lpoptions -o PrintQuality=draft
+ ;;
+ medium)
+ lpoptions -o PrintQuality=standard
+ ;;
+ high)
+ lpoptions -o PrintQuality=high
+ ;;
+ *)
+ echo "Bad QUALITY option: \"$OPTARG\"" >&2
+ exit 2
+ ;;
+ esac
+ ;;
+ h)
+ usage
+ help
+ exit
+ ;;
+ *)
+ usage >&2
+ exit 2
+ ;;
+ esac
+done
+shift $((OPTIND - 1))
+
+NEWDIR="$(mkdtemp)"
+if [ -z "${1:-}" ]; then
+ cd "$NEWDIR"
+ cat - > in-tmp
+ main
+else
+ for f in "$@"; do
+ cp "$f" "$NEWDIR"/in-tmp
+ cd "$NEWDIR"
+ main
+ cd - > /dev/null
+ done
+fi