#!/bin/sh
#
# apropos -- search the whatis database for keywords.
# whatis  -- idem, but match only commands (as whole words).
#
# Copyright (c) 1990, 1991, John W. Eaton.
# Copyright (c) 1994-1999, Andries E. Brouwer.
#
# You may distribute under the terms of the GNU General Public
# License as specified in the README file that comes with the man
# distribution.  
#
# apropos/whatis-1.5m aeb 2003-08-01 (from man-1.5m)
#
# keep old PATH - 000323 - Bryan Henderson
# also look in /var/cache/man - 030801 - aeb
# Modified for ALT Linux by Dmitry V. Levin <ldv@altlinux.org>

PATH=/bin:/usr/bin
export PATH

PROG="${0##*/}"

# When man pages in your favorite locale look to grep like binary files
# (and you use GNU grep) you may want to add the 'a' option to *grepopt1.
aproposgrepopt1='ia'
aproposgrepopt2=''
whatisgrepopt1='iwa'
whatisgrepopt2='^'
grepopt1=$aproposgrepopt1
grepopt2=$aproposgrepopt2

if [ $# = 0 ]
then
    echo "usage: $PROG keyword ..."
    exit 1
fi

manpath=`man --path | tr : \ `

if [ -z "$manpath" ]
then
    echo "$PROG: manpath is null" >&2
    exit 1
fi

if [ -z "$PAGER" ]
then
    PAGER="/usr/bin/less -isR"
fi

ICONV=/usr/bin/iconv
if [ ! -x "$ICONV" ]
then
    ICONV=
fi

if [ -n "$ICONV" ]
then
    ICONV="$ICONV -f utf-8 -r?"
else
    ICONV=cat
fi

args=
for arg in $*; do
    case $arg in
        --version|-V|-v)
	    echo "$PROG from man-1.5m"
	    exit 0
	    ;;
	--help|-h)
            echo "usage: $PROG keyword ..."
	    exit 0
	    ;;
	-*)
	    echo "$PROG: $arg: unknown option" >&2
	    exit 1
	    ;;
	*)
	    args="$args $arg"
    esac
done

# avoid using a pager if only output is "nothing appropriate"
nothing=
found=0
while [ $found = 0 -a -n "$1" ]
do
    for d in $manpath
    do
	if [ -z "${d##/usr/share/man*}" ]; then
		d="${d/\/usr\/share\/man//var/cache/man}"
	elif [ "$d" == /usr/X11R6/man ]; then
		d=/var/cache/man/X11R6
	elif [ "$d" == /usr/lib/perl5/man ]; then
		d=/var/cache/man/perl
	elif [ "$d" == /usr/local/man ]; then
		d=/var/cache/man/local
	else
		continue
	fi
        if [ -f "$d/whatis" ]
        then
            if grep -qs"$grepopt1" "$grepopt2""$1" "$d/whatis"
            then
                found=1
            fi
        fi
    done
    if [ $found = 0 ]
    then
	nothing="$nothing $1"
	shift
    fi
done

if [ $found = 0 ]
then
    for i in $nothing
    do
	echo "$i: nothing appropriate"
    done
    exit
fi

while [ "$1" != "" ]
do
    for i in $nothing
    do
	echo "$i: nothing appropriate"
    done
    nothing=
    found=0
    for d in $manpath
    do
	if [ -z "${d##/usr/share/man*}" ]; then
		d="${d/\/usr\/share\/man//var/cache/man}"
	elif [ "$d" == /usr/X11R6/man ]; then
		d=/var/cache/man/X11R6
	elif [ "$d" == /usr/lib/perl5/man ]; then
		d=/var/cache/man/perl
	elif [ "$d" == /usr/local/man ]; then
		d=/var/cache/man/local
	else
		continue
	fi
        if [ -f "$d/whatis" ]
        then
            if grep -"$grepopt1" "$grepopt2""$1" "$d/whatis"
            then
                found=1
            fi
        fi
    done

    if [ $found = 0 ]
    then
        echo "$1: nothing appropriate"
    fi

    shift
done | $ICONV | $PAGER
