#!/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.6g)
#
# keep old PATH - 000323 - Bryan Henderson
# also look in /var/cache/man - 030801 - aeb

program=`basename $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: $program keyword ..."
    exit 1
fi

manpath=`man -w | tr : '\040'`

if [ "$manpath" = "" ]
then
    echo "$program: manpath is null"
    exit 1
fi

args=
for arg in $*; do
    case $arg in
        --version|-V|-v)
	    echo "$program from man-1.6g"
	    exit 0
	    ;;
	--help|-h)
            echo "usage: $program keyword ..."
	    exit 0
	    ;;
	-*)
	    echo "$program: $arg: unknown option"
	    exit 1
	    ;;
	*)
	    args="$args $arg"
    esac
done

# list of languages to look for
LANG_LIST=`echo $LANGUAGE:$LC_ALL:$LC_MESSAGES:$LANG | tr ':' ' '`
DIR_LIST=""
for d in /var/cache/man $manpath /usr/lib
do
    for l in $LANG_LIST
    do
        if [ -d $d/$l ]
        then
            # check that the path is not already in the list
            if ! echo "$DIR_LIST" | grep " $d/$l\b" > /dev/null
            then
                DIR_LIST="$DIR_LIST $d/$l"
            fi
        fi
    done
    DIR_LIST="$DIR_LIST $d"
    # check that the path is not already in the list
    if ! echo "$DIR_LIST" | grep " $d\b" > /dev/null
    then
        DIR_LIST="$DIR_LIST $d/$l"
    fi
done

while [ "$1" != "" ]
do
    found=0
    # in order not to display lines in more than one language for
    # a same man page; we check that a given man page name
    # hasn't already been displayed
    BAZ=""
    for d in $DIR_LIST
    do
        if [ -f $d/whatis ]
        then
            if FOO=`grep -"$grepopt1" "$grepopt2""$1" $d/whatis`
            then
                # the LC_ALL=C is needed in case the text is
                # in a different encoding than the locale
                BAR=`echo -e "$FOO" | LC_ALL=C sed 's/ - .*$//' | tr ' []' '_' | sort -u`
                for i in $BAR
                do
                    if ! echo "$BAZ" | grep "$i" > /dev/null
                    then
                        BAZ="$BAZ $i"
                        i="^`echo $i | sed 's:_\+:\\\(\[_ \]\\\|\\\[\\\|\\\]\\\)\\\+:g'`"
                        echo -e "$FOO" | grep "$i"
                        found=1
                    fi
                done
# Some people are satisfied with a single occurrence
# But it is better to give all
#               break
            fi
        fi
    done

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

    shift
done

exit
