2 # shellcheck disable=SC2250
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
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.
17 CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/pdfman"
19 while [[ $# -gt 0 ]]; do
22 # Enable verbose mode.
32 echo "Usage: pdfman [-v] [-T] [<section>] <topic> ..." >&2
33 echo " pdfman <man(1) options> <arguments> ..." >&2
34 echo " pdfman {--help|--version>}" >&2
40 echo "pdfman, version 1" >&2
45 # Unknown options passed; use man(1).
56 # Ghostscript ps2pdf(1)
62 pstopdf -i -o "$1" >/dev/null
65 # Manual page name(s) given?
66 if [[ -z "$1" ]]; then
67 # Give man's wtf error ("help message"):
72 # Make sure there is a terminal available ...
78 # Detect "opener" to use ...
79 if command -v xdg-open >/dev/null; then
81 elif command -v open >/dev/null; then
88 # Detect PS-to-PDF converter to use ...
89 if command -v ps2pdf >/dev/null; then
90 ps_to_pdf_function=run_ps2pdf
91 elif command -v pstopdf >/dev/null; then
92 ps_to_pdf_function=run_pstopdf
98 [[ -n "$VERBOSE" ]] && echo "Using ${ps_to_pdf_function#*_}(1) and ${open_command}(1) ..."
100 # Try to move old cache directory ...
101 if [[ -d "$HOME/.pdfman" && ! -d "$CACHE" ]]; then
102 echo "Moving cache folder to new location ..."
103 mv -v "$HOME/.pdfman" "$CACHE" || exit 1
106 # Make sure that cache directory exists and is writable
108 [[ -w "$CACHE" ]] || CACHE="/tmp"
110 # Handle arguments, manual page name(s) ...
111 # shellcheck disable=SC2312
112 man -w "$@" | while read -r MANFILE; do
113 MANPAGE=$(basename "$MANFILE" | sed -e 's/\.gz$//g')
115 SECTION=${MANPAGE##*.}
116 PDF=$CACHE/$SECTION/$NAME.$SECTION.pdf
117 [[ -n "$VERBOSE" ]] && echo "Manual page \"$NAME($SECTION)\":"
119 # Check if cached PDF is available and still up to date:
120 if [[ -e "$PDF" && "$PDF" -ot "$MANFILE" ]]; then
121 [[ -n "$VERBOSE" ]] && echo "Deleting outdated cached PDF file ..."
125 # if there's no cached PDF file, create one!
126 if [[ ! -r "$PDF" ]]; then
127 mkdir -p "$CACHE/$SECTION"
128 [[ -n "$VERBOSE" ]] && echo "Converting \"$MANFILE\" to \"$PDF\" ..."
129 if ! man -t "$MANFILE" | $ps_to_pdf_function "$PDF"; then
130 echo "Failed to convert manual page to PDF!" >&2
134 [[ -n "$VERBOSE" ]] && echo "Using cached PDF file ..."
137 [[ -n "$VERBOSE" ]] && echo "Opening \"$PDF\" ..."