From: Alexander Barton Date: Fri, 30 Jun 2023 12:12:52 +0000 (+0200) Subject: Initial import of the existing "pdfman" script X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7383bd27ae25c618d7206a4d90f233fff802197e;p=pdfman.git Initial import of the existing "pdfman" script --- 7383bd27ae25c618d7206a4d90f233fff802197e diff --git a/pdfman b/pdfman new file mode 100755 index 0000000..bb53411 --- /dev/null +++ b/pdfman @@ -0,0 +1,109 @@ +#!/bin/sh +# +# pdfman - View Manual Pages as PDF Files, using pstopdf(1) and open(1) +# Copyright (c)2010,2013,2016,2019 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 + +# Manual page name(s) given? +if [ ! "$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 +# Make sure required tools are available ... +if ! command -v open >/dev/null || ! command -v pstopdf >/dev/null; then + man "$@" + exit $? +fi + +# 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) ... +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 + + # 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 manual page to PDF ..." + if ! man -t "$MANFILE" | pstopdf $VERBOSE -i -o "$PDF" >/dev/null; 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 "$PDF" +done