#!/bin/sh

where=n
roff=nroff
post="col -b"

if [ x$MANPATH = x ]
then
	MANPATH=/usr/man:/usr/share/man:/usr/local/man
fi

for arg in $*
do
	case $arg in
	0)
		echo usage: man [-tnw] name... 1>&2
		exit 1
		;;
	-n)			# the default
		roff=nroff
		;;
	-t)
		roff="troff -Tps"
		post=cat
		;;
	-w)
		where=y
		;;
	*)
		names="$names $arg"
		;;
	esac
done

for name in $names
do
	for dir in `echo $MANPATH | sed -e 's/^:/. /' -e 's/:$/ ./' -e 's/:/ /g'`
	do
		for sect in `seq 0 9`
		do
			if [ -f $dir/man$sect/$name.$sect ]
			then
				if [ $where = y ]
				then
					echo $dir/man$sect/$name.$sect
				else
					$roff -man $dir/man$sect/$name.$sect | eval $post 
				fi
			elif [ -f $dir/man$sect/$name.$sect.gz ]
			then
				if [ $where = y ]
				then
					echo $dir/man$sect/$name.$sect.gz
				else
					gunzip - < $dir/man$sect/$name.$sect.gz | $roff -man | eval $post
				fi
			fi
		done
	done
done

