aboutsummaryrefslogtreecommitdiff
path: root/bin/print
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2022-05-12 12:01:54 -0300
committerEuAndreh <eu@euandre.org>2022-05-12 12:01:54 -0300
commit90eaebabcaaea74237f34cf05709625345f276cc (patch)
tree349e7609d20ecfb6567652a7e28595cec9647eb0 /bin/print
parent.usr/etc/i3/config: WIP setup extra bindings (diff)
downloaddotfiles-90eaebabcaaea74237f34cf05709625345f276cc.tar.gz
dotfiles-90eaebabcaaea74237f34cf05709625345f276cc.tar.xz
Move Git repository into ~/.usr/.git/
Diffstat (limited to 'bin/print')
-rwxr-xr-xbin/print151
1 files changed, 151 insertions, 0 deletions
diff --git a/bin/print b/bin/print
new file mode 100755
index 0000000..735d8ba
--- /dev/null
+++ b/bin/print
@@ -0,0 +1,151 @@
+#!/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 -dq high *.pdf
+
+ Print the file from STDIN, double-sided:
+ $ print -d < f2.ps
+
+ Print multiple source code files:
+ $ print src/*.{c,h}
+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 "$FILE" | grep -q PostScript; then
+ ps2pdf "$FILE" "$NEWDIR"/in.pdf
+ elif file -b "$FILE" | grep -q PDF; then
+ cp "$FILE" "$NEWDIR"/in.pdf
+ else
+ enscript -o- "$FILE" | ps2pdf - "$NEWDIR"/in.pdf
+ fi
+ cd "$NEWDIR"
+
+ if [ -z "$DUPLEX" ]; then
+ lp in.pdf
+ cd - > /dev/null
+ 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
+
+ cd - > /dev/null
+}
+
+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
+ FILE="$NEWDIR"/STDIN
+ cat - > "$FILE"
+ main
+else
+ for f in "$@"; do
+ FILE="$f"
+ main
+ done
+fi