#!/bin/bash # vi:set ts=8 sw=4 et sta: # # Author : Clark J. Wang # License: GPLv2 # # $Date$ # $Author$ # $HeadURL$ # $Revision$ # #--------------------------------------------------------------------# { # global variables declare g_progname=$(basename $0) declare g_man g_ps2pdf g_groff declare g_secs g_all g_verbose g_showonly g_topics declare g_errno=0 declare ESC=$'\033' } #--------------------------------------------------------------------# debug() { if [ "$g_verbose" = yes ]; then echo "[debug] $1" fi } #--------------------------------------------------------------------# error() { echo "[$ESC[0;31merror$ESC[0m] $1" } #--------------------------------------------------------------------# usage() { cat << HELP Usage: $g_progname [-h] [-a | -s ] [-v] [-w] topic ... -a Search all manual sections. -h Help -s is a colon separated list of manual sections to search. -v Verbose -w Do not create PDF. Only show which file to use. Report bugs to $ESC[1;35mClark J. Wang$ESC[0m HELP exit $1 } #--------------------------------------------------------------------# getargs() { declare option OPTIND=1 OPTERR=0 while getopts ":ahs:vw" option "$@"; do case $option in a) g_all=yes ;; h|:|'?') [ $option = h ] && usage 0 || usage 1 ;; s) g_secs=$OPTARG ;; v) g_verbose=yes ;; w) g_showonly=yes ;; esac done shift $((OPTIND - 1)) if [ "$g_all" = yes -a "$g_secs" ]; then usage 1 fi g_topics=( "$@" ) if [ ${#g_topics[@]} = 0 ]; then usage 1 fi } #--------------------------------------------------------------------# findcmd() { declare cmd exe for cmd in "$@"; do exe=$(type -P "$cmd" 2> /dev/null) if [ "$exe" ]; then echo "$exe" return fi done } #--------------------------------------------------------------------# findcmds() { g_man=$(findcmd man) if [ "$g_man" ]; then debug "MAN: $g_man" else error "Man not found" exit 1 fi g_groff=$(findcmd groff) if [ "$g_groff" ]; then debug "GROFF: $g_groff" else error "Groff not found" exit 1 fi g_ps2pdf=$(findcmd ps2pdf14 ps2pdf13 ps2pdf12 ps2pdf pstopdf) if [ "$g_ps2pdf" ]; then debug "PS2PDF: $g_ps2pdf" else error "Ps2pdf not found" exit 1 fi } #--------------------------------------------------------------------# runcmd() { declare cmd=$1 debug "$cmd" if [ "$g_verbose" = yes ]; then eval "$cmd" else eval "$cmd" >& /dev/null fi g_errno=$? if [ $g_errno -gt 0 ]; then debug "Command exited: $g_errno" fi } #--------------------------------------------------------------------# man2pdf() { declare topic man_w tmpdir cmd manfile basename found=0 topic=$1 man_w="$g_man -w" tmpdir=/tmp if [ ! "$topic" ]; then return 1 fi if [ "$g_all" = yes ]; then man_w="$man_w -a" elif [ "$g_secs" ]; then man_w="$man_w -a -S$g_secs" fi # # On Mac OS X (Darwin), "man -w" may output like this: # # $ man -w ls # /usr/share/man/cat1/ls.1.gz (source: /usr/share/man/man1/ls.1) # # # On Gentoo, "man -w" may output like this: # # $ man -w emerge # /var/cache/man/cat1/emerge.1.bz2 (<-- /usr/share/man/man1/emerge.1.gz) # while read manfile; do found=1 if [ -f "$manfile" ]; then debug "File: $manfile" else error "File '$manfile' not found" continue fi if [ "$g_showonly" = yes ]; then echo "$topic: $manfile" continue fi # copy to $tmpdir basename=$(basename $manfile) if [[ $basename == *.gz ]]; then basename=${basename%.gz} cmd="gzip -dc $manfile > $tmpdir/$basename" elif [[ $basename == *.bz2 ]]; then basename=${basename%.bz2} cmd="bzip2 -dc $manfile > $tmpdir/$basename" else cmd="cp -f $manfile $tmpdir" fi runcmd "$cmd" if [ $g_errno -gt 0 ]; then continue fi # groff cmd="$g_groff -t -e -mandoc -Tps $tmpdir/$basename > $tmpdir/$basename.ps" runcmd "$cmd" if [ $g_errno -gt 0 ]; then continue fi # ps2pdf if [[ $g_ps2pdf == *pstopdf ]]; then # On Mac OS X, 'pstopdf' by default puts the new PDF file in # the same directory as the PS file. cmd="$g_ps2pdf -o ./$basename.pdf $tmpdir/$basename.ps" else cmd="$g_ps2pdf $tmpdir/$basename.ps" fi runcmd "$cmd" if [ $g_errno -gt 0 ]; then continue fi if [ -f $basename.pdf ]; then echo "Created: $basename.pdf" else error "Failed to create '$basename.pdf'" fi cmd="rm -f $tmpdir/$basename $tmpdir/$basename.ps" runcmd "$cmd" done < <( $man_w -- $topic 2> /dev/null \ | sed -e 's@^[[:blank:]]\+\|[[:blank:]]\+$@@g' \ -e 's@ (source:.*@@' \ -e 's@.*(<-- \(.*\))@\1@') if ((! found)); then error "Man page for '$topic' not found" return 1 fi } #--------------------------------------------------------------------# mans2pdfs() { declare topic for topic in "${g_topics[@]}"; do man2pdf "$topic" done } #--------------------------------------------------------------------# { getargs "$@" findcmds mans2pdfs }