#!/bin/sh
#
# vwman	- print VxWorks manual entries
#
# modification history
# --------------------
# 01e,15sep88,ecs  added -s option to more.
# 01d,13sep88,gae  figures out vw directory correctly with sanity check;
#                  formats even if no write permission in man directory.
# 01c,10sep88,gae  documentation; added kernel directory.
# 01b,08sep88,gae  fixed roff macro for "a" and "0" sections; fixed .nr's.
# 01a,26aug88,gae  written
#
# SYNOPSIS
# vwman [section] [-l] entry
#
# DESCRIPTION
# Lookup entry in online VxWorks manual and print.
# Without any options, vwman will find and print a
# manual entry named `entry'.
#
# OPTIONS
#  -l     list all entries with *entry*
#
# EXAMPLE
#   % vwman -0 network    # print network chapter
#   % vwman -l log        # list everything on log's
#   % vwman mv133/target  # print mv133 target info
#
# FILES
#  vw/mansrc/man{0,1,2,3,4,a,t,k}  source manual entries
#  vw/man/man{0,1,2,3,4,a,t,k}     nroff'd manual entries
#  /usr/lib/tmac/tmac.wrs          troff macros (man0, mana)
#  /usr/lib/tmac/tmac.angen        troff macros (all others)
#
# INTERNAL
# The troff macros are distributed in vw/misc and must be placed
# in the appropriate tmac directory.
#*/

# directory containing this script
home=`expr $0 : '\(.*/\)[^/]' '|' ./`

pager="/usr/ucb/more -s"
roff="nroff -mangen"
vw=$home..
mansrc=$vw/mansrc
man=$vw/man
mann="0 1 2 3 4 a t k"
found=0
look=0
debug=0
tool=`basename $0`
tmpentry=/tmp/$tool.$$
usage="usage: $tool [$mann] [-l] entry"

trap "rm -f $tmpentry; exit 0" 0 1 2 3 15

if (test ! -d $mansrc) then
    echo "$tool: \"$vw\" does not contain a \"mansrc\" directory"
    exit 1
fi

## determine flags

readoptions=true
while ($readoptions) do
    case $1 in
    0)		mann="0" ;;
    1)		mann="1" ;;
    2)		mann="2" ;;
    3)		mann="3" ;;
    4)		mann="4" ;;
    a)		mann="a" ;;
    t)		mann="t" ;;
    k)		mann="k" ;;
    -l)		look=1;  ;; 
    -*)		echo "$tool: invalid option $1"; exit 1;;
    *)		readoptions=false; ;;
    esac

    if ($readoptions) then
	shift
    fi
done

if (test $# -ne 1) then
    echo $usage
    exit 1
fi

# do "lookup" option

if (test $look -eq 1) then
    for m in $mann
    do
	for e in $mansrc/man${m}/*$1*
	do
	    if (test -r $e) then
		entry="$entry `basename $e .nr`"
		found=1
	    fi
	done
    done
    if (test $found -eq 0) then
	echo "$tool: sorry, entry not found"
    else
	echo $entry
    fi
    exit 0
else
    entry=$1
fi

# try to find manual entry in already formatted directory

for m in $mann
do
    if (test -r $man/man${m}/$entry) then
	$pager $man/man${m}/$entry
	found=1
	break
    fi
done

# try to find manual entry in source directory

if (test $found -eq 0) then
    for m in $mann
    do
	if (test -r $mansrc/man${m}/$entry.nr) then

	    if (test X$m = X0 -o X$m = Xa) then
		roff="nroff -mwrs"
	    fi

	    echo "$tool: formatting $entry from man${m}"
	    if (test -w $man/man${m}/) then
		fmtentry=$man/man${m}/$entry
	    else
		fmtentry=$tmpentry
	    fi
	    $roff $mansrc/man${m}/$entry.nr | colrm 1 10 > $fmtentry
	    $pager $fmtentry
	    found=1
	    break
	fi
    done
fi

if (test $found -eq 0) then
    echo "$tool: sorry, entry not found"
fi

exit 0
