]> arthur.barton.de Git - pdfman.git/blob - pdfman
README.md: Add a prerequisites and a hints section
[pdfman.git] / pdfman
1 #!/bin/bash
2 # shellcheck disable=SC2250
3 #
4 # pdfman - View UNIX manual pages as Portable Document Format (PDF) files
5 # Copyright (c)2010,2013,2016,2019,2023 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 run_ps2pdf() {
56         # Ghostscript ps2pdf(1)
57         ps2pdf - "$1"
58 }
59
60 run_pstopdf() {
61         # Apple pstopdf(1)
62         pstopdf -i -o "$1" >/dev/null
63 }
64
65 # Manual page name(s) given?
66 if [[ -z "$1" ]]; then
67         # Give man's wtf error ("help message"):
68         man
69         exit $?
70 fi
71
72 # Make sure there is a terminal available ...
73 if [[ ! -t 1 ]]; then
74         man "$@"
75         exit $?
76 fi
77 # Make sure required tools are available ...
78 if ! command -v open >/dev/null; then
79         man "$@"
80         exit $?
81 fi
82
83 # Detect PS-to-PDF converter to use ...
84 if command -v ps2pdf >/dev/null; then
85         ps_to_pdf_function=run_ps2pdf
86 elif command -v pstopdf >/dev/null; then
87         ps_to_pdf_function=run_pstopdf
88 else
89         man "$@"
90         exit $?
91 fi
92 [[ -n "$VERBOSE" ]] && echo "Using ${ps_to_pdf_function#*_}(1) ..."
93
94 # Try to move old cache directory ...
95 if [[ -d "$HOME/.pdfman" && ! -d "$CACHE" ]]; then
96         echo "Moving cache folder to new location ..."
97         mv -v "$HOME/.pdfman" "$CACHE" || exit 1
98 fi
99
100 # Make sure that cache directory exists and is writable
101 mkdir -p "$CACHE"
102 [[ -w "$CACHE" ]] || CACHE="/tmp"
103
104 # Handle arguments, manual page name(s) ...
105 # shellcheck disable=SC2312
106 man -w "$@" | while read -r MANFILE; do
107         MANPAGE=$(basename "$MANFILE" | sed -e 's/\.gz$//g')
108         NAME=${MANPAGE%.*}
109         SECTION=${MANPAGE##*.}
110         PDF=$CACHE/$SECTION/$NAME.$SECTION.pdf
111         [[ -n "$VERBOSE" ]] && echo "Manual page \"$NAME($SECTION)\":"
112
113         # Check if cached PDF is available and still up to date:
114         if [[ -e "$PDF" && "$PDF" -ot "$MANFILE" ]]; then
115                 [[ -n "$VERBOSE" ]] && echo "Deleting outdated cached PDF file ..."
116                 rm "$PDF"
117         fi
118
119         # if there's no cached PDF file, create one!
120         if [[ ! -r "$PDF" ]]; then
121                 mkdir -p "$CACHE/$SECTION"
122                 [[ -n "$VERBOSE" ]] && echo "Converting \"$MANFILE\" to \"$PDF\" ..."
123                 if ! man -t "$MANFILE" | $ps_to_pdf_function "$PDF"; then
124                         echo "Failed to convert manual page to PDF!" >&2
125                         exit 1
126                 fi
127         else
128                 [[ -n "$VERBOSE" ]] && echo "Using cached PDF file ..."
129         fi
130
131         [[ -n "$VERBOSE" ]] && echo "Opening \"$PDF\" ..."
132         open "$PDF"
133 done