~skiqqy/bin

bin/pdf -rwxr-xr-x 6.1 KiB
c0954778Stephen Cochrane Prog on deploy 2 days ago
                                                                                
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/bin/bash
# Author: Skiqqy
# Basically solves any problem that has to do with pdfs
# Usage: pdf -h

args=("$@")
SCRIPT_PATH=$(dirname "$0")
com="mupdf -C FFDED7 -I"
# shellcheck source=./import/alert.sh
. "$SCRIPT_PATH/import/alert.sh" > /dev/null 2>&1
. "$SCRIPT_PATH/import/stdlib.sh" > /dev/null 2>&1

[ ! "$(command -v error)" ] && echo "[WARNING] Missing 'error.sh' import."
[ ! "$(command -v mupdf)" ] && warning "Missing 'mupdf' dependency." 1

# Keep track of what needs to be done.
TODO ()
{
cat << EOF
pdf ~ TODO Tracker
------------------

TODO: Fix argument passing to only set vars, not run functions, as this is bad style.
	- Reason: Program can 'break' if arguments are passed in a wierd order.

TODO: During refactor some of the optorgs broke, this is due to not doing the above TODO, fix
EOF
}

usage ()
{
cat << EOF
pdf ~ An easy way to deal with pdfs
-----------------------------------

Usage: pdf [options] [command] file

Options
-------
-h			Shows this message.
-o			Specify output file when compiling a pdf.
-p			Use dmenu to enter a password for the pdf.
-v			Specify which pdf viewer you want to use.
-e			Use gimp to edit a pdf.
-d			Use mutool (from mupdf) to decompile a pdf.
-c			Compile a pdf and then open it, Supported file type(s) include:
			 - .md
			 - .tex
			 - .1 (man pages)
-t			Generate a .tex skeleton document.
--TODO			Lists current TODO's.
--output=<FILENAME>	Specify outputfile when compiling a pdf.

Commands
--------
exec        Attempt to compile a file, or open a pdf.
watch       Watch a pdf, and refresh mupdf if a change is detected.

Example(s)
----------
\$ pdf https://skiqqy.xyz/assets/CurriculumVitae.pdf # Download and open pdf
\$ pdf some.pdf # open pdf
\$ pdf document.tex # Compile a tex file to 'document.pdf'
\$ pdf document.md  # Compile a md file to 'document.pdf'
\$ pdf -c document.md  # Compile a md file to 'document.pdf' and then open it

Author: Skiqqy
EOF
	exit "$1"
}

fetch_pdf ()
{
	index=$((${#args[@]} - 1))
	url="${args[$index]}"

	# Check to see if we need to download the pdf
	if [[ "$url" =~ ^http(s)?:// ]]
	then
		file=$(basename "$url")
	else
		file="$url"
		url=
	fi
	extension=$(echo "$file" | cut -d "." -f 2)

	if [ "$extension" == "pdf" ]
	then
		if [ -z $url ]
		then
			$com "$file"
		else
			curl -s "$url" > /tmp/"$file"
			$com /tmp/"$file"
		fi
	else
		error "Invalid file type."
	fi
}

# Usage: build_tex "file_name"
# Note: will, if we call with "file_name", then it will also use a "file_name.bib" when building, if it exists
build_tex ()
{
	[ ! "$(command -v pdflatex)" ] && error "Missing 'pdflatex', cannot build from .tex"
	pdflatex "$1".tex && biber "$1"; pdflatex "$1".tex && pdflatex "$1".tex
	[ ! -f "$1.bib" ] && warning "\'$1.bib\' not found, skipped."
	rm -f "$1."{'aux','bbl','bcf','blg','log','xml','nav','out','snm','toc'} && success "Cleaned build files."

	if [ -n "$out" ]
	then
		[ "$(echo "$out" | cut -d\. -f 2)" = pdf ] && out="$(echo "$out" | cut -d\. -f 1)"
		mv "$1.pdf" "$out.pdf"
	fi
}

# Usage: build_md "file_name"
# Compiles file_name.md to file_name.pdf
build_md ()
{
	[ ! "$(command -v pandoc)" ] && error "Missing 'pandoc', cannot build from .md"
	if [ -z "$out" ]
	then
		out="$1"
	else
		[ "$(echo "$out" | cut -d\. -f 2)" = pdf ] && out="$(echo "$out" | cut -d\. -f 1)"
	fi

	# Compile
	pandoc "$1.md" -o "$out.pdf" && success "Compiled \'$1.md\'."
}

# Usage: build_man_page "file_name"
# Compiles file_name.1 to file_name.pdf
build_man_page ()
{
	[ ! "$(command -v ps2pdf)" ] && error "Missing 'ps2pdf', cannot build from .1"
	if [ -z "$out" ]
	then
		out="$1"
	else
		[ "$(echo "$out" | cut -d\. -f 2)" = pdf ] && out="$(echo "$out" | cut -d\. -f 1)"
	fi

	# Compile
	man -t <(cat "$1.1") | ps2pdf - "$out.pdf"
}

parse_build ()
{
	[ -z "$file" ] && [ -z "$ftype" ] && warning "Missing file." && usage 1
	[ ! -f "$file.$ftype" ] && error "\'$file.$ftype\' not found."
	case $ftype in
		tex)
			build_tex "$file"
			;;
		md)
			build_md "$file"
			;;
		1)
			build_man_page "$file"
			;;
		*)
			error "Unknown file extension."
			;;
	esac
}

gen_tex_skelly ()
{
cat << EOF
\documentclass{article}

\\title{TITLE}
\\author{$USER}
\date{\\today}

\\begin{document}
\maketitle
\\tableofcontents

\\section{Introduction}
\\end{document}
EOF
}

main ()
{
	while getopts "hpcv:o:edt-:" opt
	do
		case "$opt" in
			-)
				case "$OPTARG" in
					TODO)
						TODO
						return 0
						;;
					output=*)
						out=${OPTARG#*=}
						;;
					*)
						error "Invalid arg, use -h for help." 1
						;;
				esac
				;;
			h)
				usage 0
				;;
			p)
				[ ! "$(command -v dmenu)" ] && error "Missing 'dmenu' dependency." 1
				pass=$(prompt "PDF Password:")
				com="$com -p $pass"
				;;
			c)
				parse_build
				index=$((${#args[@]} - 1))
				url="${args[$index]}"
				if [ -z "$out" ]
				then
					args["$index"]="$(echo "$url" | cut -d\. -f 1).pdf"
				else
					[ "$(echo "$out" | cut -d\. -f 2)" = pdf ] && out="$(echo "$out" | cut -d\. -f 1)"
					args["$index"]="$out.pdf"
				fi
				fetch_pdf
				return 0
				;;
			v)
				com="$OPTARG"
				;;
			e)
				[ ! "$ftype" = "pdf" ] && error "Not a pdf!"
				gimp "${args[$index]}"
				return 0
				;;
			d)
				[ ! "$ftype" = "pdf" ] && error "Not a pdf!"
				file="${args[$index]}"
				mutool clean -d "$file" "decompiled-$file"
				return 0
				;;
			t)
				gen_tex_skelly
				return 0
				;;
			o)
				out="$OPTARG"
				;;
			*)
				error "Invalid arg, use -h for help." 1
				;;
		esac
	done
	shift $((OPTIND-1)) # Get rid of parsed optargs

	if [[ $1 =~ \. ]]
	then
		command=exec
	else
		command="$1"
		shift 1
	fi

	[ ! "$command" = help ] && [ -z "$1" ] && \
		warning 'Missing file argument\n' && usage 1
	case "$command" in
		exec)
			base=$(basename "$1" 2>/dev/null)
			ftype=$(echo "$base" | cut -d\. -f 2)
			file=$(echo "$base" | cut -d\. -f 1)

			# Detect what we must do, based on file extension.
			case $ftype in
				pdf)
					fetch_pdf # We open if the extension is .pdf
					;;
				*)
					parse_build # If the extionsion is not .pdf, we attempt to build it
					;;
			esac
			;;
		watch)
			[ ! -f "$1" ] && error "'$1' DNE!"
			while inotifywait -e close_write "$1"
			do
				pkill -HUP mupdf
			done
			;;
		help)
			usage 0
			;;
		*)
			usage 1
			;;
	esac
}

main "$@"