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