]> arthur.ath.cx Git - pdfman.git/blob - pdfman
a11889c5e76fd5f33c72f2668ff9aeaf4cb8fc38
[pdfman.git] / pdfman
1 #!/bin/bash
2 # shellcheck disable=SC2250
3 #
4 # pdfman - View Manual Pages as PDF Files, using pstopdf(1) and open(1)
5 # Copyright (c)2010,2013,2016,2019 Barton IT-Consulting, Alexander Barton
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 # or FITNESS FOR A PARTICULAR PURPOSE.
14 # See the GNU General Public License for more details.
15 #
16
17 CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/pdfman"
18
19 while [[ $# -gt 0 ]]; do
20         case "$1" in
21                 "-v")
22                         # Enable verbose mode.
23                         VERBOSE="-p"
24                         ;;
25                 "-T")
26                         # Force using man(1)!
27                         shift
28                         man "$@"
29                         exit $?
30                         ;;
31                 "--help")
32                         echo "Usage: pdfman [-v] [-T] [<section>] <topic> ..." >&2
33                         echo "       pdfman <man(1) options> <arguments> ..." >&2
34                         echo "       pdfman {--help|--version>}" >&2
35                         echo >&2
36                         man --help
37                         exit 2
38                         ;;
39                 "--version")
40                         echo "pdfman, version 1" >&2
41                         man --version
42                         exit 2
43                         ;;
44                 -*)
45                         # Unknown options passed; use man(1).
46                         man "$@"
47                         exit $?
48                         ;;
49                 *)
50                         break
51         esac
52         shift
53 done
54
55 # Manual page name(s) given?
56 if [[ -z "$1" ]]; then
57         # Give man's wtf error ("help message"):
58         man
59         exit $?
60 fi
61
62 # Make sure there is a terminal available ...
63 if [[ ! -t 1 ]]; then
64         man "$@"
65         exit $?
66 fi
67 # Make sure required tools are available ...
68 if ! command -v open >/dev/null || ! command -v pstopdf >/dev/null; then
69         man "$@"
70         exit $?
71 fi
72
73 # Try to move old cache directory ...
74 if [[ -d "$HOME/.pdfman" && ! -d "$CACHE" ]]; then
75         echo "Moving cache folder to new location ..."
76         mv -v "$HOME/.pdfman" "$CACHE" || exit 1
77 fi
78
79 # Make sure that cache directory exists and is writable
80 mkdir -p "$CACHE"
81 [[ -w "$CACHE" ]] || CACHE="/tmp"
82
83 # Handle arguments, manual page name(s) ...
84 # shellcheck disable=SC2312
85 man -w "$@" | while read -r MANFILE; do
86         MANPAGE=$(basename "$MANFILE" | sed -e 's/\.gz\$//g')
87         NAME=${MANPAGE%.*}
88         SECTION=${MANPAGE##*.}
89         PDF=$CACHE/$SECTION/$NAME.$SECTION.pdf
90
91         # Check if cached PDF is available and still up to date:
92         if [[ -e "$PDF" && "$PDF" -ot "$MANFILE" ]]; then
93                 [[ -n "$VERBOSE" ]] && echo "Deleting outdated cached PDF file ..."
94                 rm "$PDF"
95         fi
96
97         # if there's no cached PDF file, create one!
98         if [[ ! -r "$PDF" ]]; then
99                 mkdir -p "$CACHE/$SECTION"
100                 [[ -n "$VERBOSE" ]] && echo "Converting manual page to PDF ..."
101                 # shellcheck disable=SC2312,SC2086
102                 if ! man -t "$MANFILE" | pstopdf $VERBOSE -i -o "$PDF" >/dev/null; then
103                         echo "Failed to convert manual page to PDF!" >&2
104                         exit 1
105                 fi
106         else
107                 [[ -n "$VERBOSE" ]] && echo "Using cached PDF file ..."
108         fi
109
110         [[ -n "$VERBOSE" ]] && echo "Opening \"$PDF\" ..."
111         open "$PDF"
112 done