#!/bin/bash # shellcheck disable=SC2250 # # pdfman - View UNIX manual pages as Portable Document Format (PDF) files # Copyright (c)2010,2013,2016,2019,2023 Barton IT-Consulting, Alexander Barton # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY # or FITNESS FOR A PARTICULAR PURPOSE. # See the GNU General Public License for more details. # CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/pdfman" while [[ $# -gt 0 ]]; do case "$1" in "-v") # Enable verbose mode. VERBOSE="-p" ;; "-T") # Force using man(1)! shift man "$@" exit $? ;; "--help") echo "Usage: pdfman [-v] [-T] [
] ..." >&2 echo " pdfman ..." >&2 echo " pdfman {--help|--version>}" >&2 echo >&2 man --help exit 2 ;; "--version") echo "pdfman, version 1" >&2 man --version exit 2 ;; -*) # Unknown options passed; use man(1). man "$@" exit $? ;; *) break esac shift done run_ps2pdf() { # Ghostscript ps2pdf(1) ps2pdf - "$1" } run_pstopdf() { # Apple pstopdf(1) pstopdf -i -o "$1" >/dev/null } # Manual page name(s) given? if [[ -z "$1" ]]; then # Give man's wtf error ("help message"): man exit $? fi # Make sure there is a terminal available ... if [[ ! -t 1 ]]; then man "$@" exit $? fi # Detect "opener" to use ... if command -v xdg-open >/dev/null; then open_command=xdg-open elif command -v open >/dev/null; then open_command=open else man "$@" exit $? fi # Detect PS-to-PDF converter to use ... if command -v ps2pdf >/dev/null; then ps_to_pdf_function=run_ps2pdf elif command -v pstopdf >/dev/null; then ps_to_pdf_function=run_pstopdf else man "$@" exit $? fi [[ -n "$VERBOSE" ]] && echo "Using ${ps_to_pdf_function#*_}(1) and ${open_command}(1) ..." # Try to move old cache directory ... if [[ -d "$HOME/.pdfman" && ! -d "$CACHE" ]]; then echo "Moving cache folder to new location ..." mv -v "$HOME/.pdfman" "$CACHE" || exit 1 fi # Make sure that cache directory exists and is writable mkdir -p "$CACHE" [[ -w "$CACHE" ]] || CACHE="/tmp" # Handle arguments, manual page name(s) ... # shellcheck disable=SC2312 man -w "$@" | while read -r MANFILE; do MANPAGE=$(basename "$MANFILE" | sed -e 's/\.gz$//g') NAME=${MANPAGE%.*} SECTION=${MANPAGE##*.} PDF=$CACHE/$SECTION/$NAME.$SECTION.pdf [[ -n "$VERBOSE" ]] && echo "Manual page \"$NAME($SECTION)\":" # Check if cached PDF is available and still up to date: if [[ -e "$PDF" && "$PDF" -ot "$MANFILE" ]]; then [[ -n "$VERBOSE" ]] && echo "Deleting outdated cached PDF file ..." rm "$PDF" fi # if there's no cached PDF file, create one! if [[ ! -r "$PDF" ]]; then mkdir -p "$CACHE/$SECTION" [[ -n "$VERBOSE" ]] && echo "Converting \"$MANFILE\" to \"$PDF\" ..." if ! man -t "$MANFILE" | $ps_to_pdf_function "$PDF"; then echo "Failed to convert manual page to PDF!" >&2 exit 1 fi else [[ -n "$VERBOSE" ]] && echo "Using cached PDF file ..." fi [[ -n "$VERBOSE" ]] && echo "Opening \"$PDF\" ..." $open_command "$PDF" done