#!/bin/bash

###########################################################################
#
#	Shell program to Show man pages in html.
#
#	Copyright 2001, USM Bish, <bish@nde.vsnl.net.in>.
#
#	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.
#
#	Usage:
#
#		mman [ manpage | -i | --info ]
#
#	Options:
#
#		-i, --info	Display this help message and exit.
#
#
#	Revisions:
#
#	Oct/17/2001	File created               .... ver 0.1
#       Oct/19/2001     Menu for multiple mans     .... ver 0.2
#
###########################################################################


###########################################################################
#	Constants
###########################################################################

PROGNAME=$(basename $0)
VERSION="0.2"

###########################################################################
#       User configurable variables
###########################################################################

CBROWSER=links
XBROWSER=xelvis
RMAN=/usr/local/bin/rman

###########################################################################
#	Functions
###########################################################################

function do_it
{
        #####
        #       Function to display man page once it is found
        #####
        
        #####   If in .gz format then gunzip it
        if echo $MAN | grep -E -q "\.gz$" ; then
            gzip -dc $MAN | rman -b -f html > /tmp/$PREFIX.html 2>/dev/null
            cat /tmp/$PREFIX.html | grep -v "<!--" > /tmp/$PREFIX.html.$$
            cat /tmp/$PREFIX.html.$$ > /tmp/$PREFIX.html
            $MYBROWSER /tmp/$PREFIX.html 
            rm -f /tmp/$PREFIX*
        else
            rman -b -f html $MAN > /tmp/$PREFIX.html 2>/dev/null   
            cat /tmp/$PREFIX.html | grep -v "<!--" > /tmp/$PREFIX.html.$$
            cat /tmp/$PREFIX.html.$$ > /tmp/$PREFIX.html
            $MYBROWSER /tmp/$PREFIX.html 
            rm -f /tmp/$PREFIX*
         fi
}

function check_rman
{
	#####
	#	Function to check whether rman is installed
	#	Quit if not installed
	#####

        if ! [ -x $RMAN ]; then
             echo "Can't find "$RMAN
             term_exit
        fi
}

function check_term
{
	#####
	#	Function to check whether on console or X
	#	and initialise MYBROWSER variable
	#####

        if [ $TERM = "linux" ]; then
           MYBROWSER=$CBROWSER
        else
           MYBROWSER=$XBROWSER
        fi
}

function message
{

	#####	
	#	Function to print a message in case man
	#	name is not given
	#####

        cat << MSG_EOF

        $PROGNAME                                       Version : $VERSION
        
        Usage : $PROGNAME [manpage]
                $PROGNAME --info
                $PROGNAME -i
                        
        Search for man page along MANPATH (Ususally in /usr/man/, 
        /usr/X11R6/man and /usr/share/man),  then format  to html 
        and display with your selected html browser : $MYBROWSER

        Example: $PROGNAME foo
        
        This would look for foo.[1-9], foo.[1-9].gz, or foo.man
        in current or any of the above directories.
        
MSG_EOF

}

function clean_up
{
	rm -f ${TEMP_FILE}
}


function graceful_exit
{
	clean_up
	exit
}


function error_exit 
{
	echo "${PROGNAME}: ${1:-"Unknown Error"}" >&2
	clean_up
	exit 1
}


function term_exit
{
	#####
	#	Function to perform exit if termination signal is trapped
	#	No arguments
	#####

	echo "${PROGNAME}: Terminated"
	clean_up
	exit
}


function int_exit
{
	echo "${PROGNAME}: Aborted by user"
	clean_up
	exit
}


function usage
{
	echo "Usage: ${PROGNAME} [manpage | -i | --info]"
}


function helptext
{
	#####
	#	Function to display help message for program
	#####
	
        local tab=$(echo -en "\t\t")
		
        cat <<- -EOF-

        ${PROGNAME} ver. ${VERSION}	

        This is a program to Show man pages in html. This is
        dependent upon the excellent utility  "PolyglotMan".
        PolyglotMan takes man pages from most of the popular 
        flavors  of  UNIX and  transforms them into any of a 
        number of text source formats. PolyglotMan was form-
        erly known as RosettaMan.  The name of the binary is 
        still called "rman", for scripts like this one which
        depend on that name; mnemonically, just think "rman"
        to be "reverse man". URL for download: 
        
        ftp://ftp.cs.berkeley.edu/ucb/people/phelps/tcltk/rman.tar.Z
	
        $(usage)
        e.g. mman ls
	
        Options:
	
        -i, --info	Display this help message and exit.

-EOF-
}	


###########################################################################
#	Program starts here
###########################################################################

# Trap TERM, HUP, and INT signals and properly exit

trap term_exit TERM HUP
trap int_exit INT

# Process command line arguments

if [ "$1" = "" ]; then
   clear
   check_term 
   message
   exit 1
fi

if [ "$1" = "--info" ]; then
	helptext
	graceful_exit
fi

# Process arguments - if any

while getopts ":i" opt; do
    case $opt in

    i )	helptext
	graceful_exit 
	;;

    * )	usage
	exit 1
	;;
    esac
done

#

check_term
check_rman

PREFIX=$1

# Check for pwd
find $PWD -maxdepth 1 -name $1.man >/tmp/$1 2>/dev/null

if [ ! -s /tmp/$1 ]; then
   find $PWD -maxdepth 1 -name $1.[1-9]* >/tmp/$1 2>/dev/null
fi

# Check in /usr/man and /usr/X11R6/man
if [ ! -s /tmp/$1 ]; then
   find /usr/man/man[1-9] /usr/X11R6/man/man[1-9] -maxdepth 1 -name $1.[1-9]* >/tmp/$1 2>/dev/null
fi

# Check in /usr/local/man and /opt/local/man
if [ ! -s /tmp/$1 ]; then
   find /usr/local/man/man[1-9] /opt/local/man/man[1-9] -maxdepth 1 -name $1.[1-9]* >/tmp/$1 2>/dev/null
fi

# Check in /usr/share/man and /usr/man/preformat/
if [ ! -s /tmp/$1 ]; then
   find /usr/share/man/man[1-9] /usr/man/preformat/man[1-9] -maxdepth 1 -name $1.[1-9]* >/tmp/$1 2>/dev/null
fi

# Check in /usr/man/cat
if [ ! -s /tmp/$1 ]; then
   find /usr/man/cat[1-9] -maxdepth 1 -name $1.[1-9]* >/tmp/$1 2>/dev/null
fi


if [ -s /tmp/$1 ]; then

   echo
   echo $PROGNAME"          Version :"$VERSION
   echo
   echo 'man pages available for "'$PREFIX'"'
   echo
   CNT=0 
   for i in `cat /tmp/$PREFIX`; do
       CNT=$((CNT+1))
       echo -en $CNT" ] "$i
       echo
   done    
   echo -en "\n0 ]  Abort\n\n"

   echo -en "Enter option [ 0 - $CNT ] : " 
   read ANSWER
   
   if [ "$ANSWER" = "0" ]; then
       term_exit
   fi      

   CCNT=0

   for i in `cat /tmp/$PREFIX`; do
       CCNT=$((CCNT+1)) 
       if [ $CCNT = $ANSWER ]; then
          MAN=`echo $i`
       fi   
   done
   do_it
      
else

   rm -f /tmp/$1
   echo "Cannot find man page for : "$1

fi

graceful_exit




#!/bin/sh
#

MYBROWSER=links


MSG() {
cat <<EOF
===============================================================================
Search for man page in current directory, /usr/man/man[1-9], and 
/usr/X11R6/man/man[1-9], format to html and display with $MYBROWSER

For example, this would look for fooboo.[1-9], fooboo.[1-9].gz, or fooboo.man.

# man fooboo

Last, if the man page is found and formatted in /usr/man/cat[1-9], the pager 
less will be be used to display it, press q to quit less.
===============================================================================
EOF
}

if [ "$1" = "" ]; then
clear 
MSG
exit
fi

if [ -x /opt/local/bin/rman ]; then

find $PWD -maxdepth 1 -name $1.man >/tmp/$1 2>/dev/null

if [ ! -s /tmp/$1 ]; then
find $PWD -maxdepth 1 -name $1.[1-9]* >/tmp/$1 2>/dev/null
fi

if [ ! -s /tmp/$1 ]; then
find /usr/man/man[1-9] /usr/X11R6/man/man[1-9] -maxdepth 1 -name $1.[1-9]* >/tmp/$1 2>/dev/null
fi

if [ ! -s /tmp/$1 ]; then
find /usr/man/cat[1-9] -maxdepth 1 -name $1.[1-9]* >/tmp/$1 2>/dev/null
fi

if [ -s /tmp/$1 ]; then
man=`cat /tmp/$1`

if echo $man | grep -E -q "\.gz$" ; then
gzip -dc $man | rman -b -f html > /tmp/$1.html 2>/dev/null  
$MYBROWSER /tmp/$1.html ; rm -f /tmp/$1*
else
rman -b -f html $man > /tmp/$1.html 2>/dev/null   
$MYBROWSER /tmp/$1.html ; rm -f /tmp/$1*
fi
else
less $man ; rm -f /tmp/$1
fi
else
rm -f /tmp/$1
echo "Can't find $1 man page."
fi
else
echo "Can't find /opt/local/bin/rman program."
fi
