aboutsummaryrefslogtreecommitdiff
path: root/bin/print
blob: 5aef10281eb0b9e2043446e93b552b21afb9acf1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/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

		  FILE          path to file to be printed


		Print FILE using CUPS using lp(1).


		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
}

n_pages() {
	qpdf "$1" --show-npages
}

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

	qpdf in.pdf --pages . 1-z:odd  -- odd.pdf
	qpdf in.pdf --pages . 1-z:even -- 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

QUALITY_SET=false
DUPLEX=
while getopts 'dq:h' flag; do
	case "$flag" in
		(d)
			DUPLEX=1
			;;
		(q)
			QUALITY_SET=true
			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))

if [ "$QUALITY_SET" = false ]; then
	lpoptions -o PrintQuality=standard
fi

NEWDIR="$(mkdtemp)"
trap 'rm -rf "$NEWDIR"' EXIT
if [ -z "${1:-}" ]; then
	FILE="$NEWDIR"/STDIN
	cat - > "$FILE"
	main
else
	for f in "$@"; do
		FILE="$f"
		main
	done
fi