#!/bin/bash
#
# -*- this is a BASH script -*-
#
# makewhatis  for SuSE Linux
#
#   Copyright (c) 1998 SuSE GmbH Fuerth, Germany. 
#   Copyright (c) 1999 SuSE GmbH Nuernberg, Germany. 
#   please send bugfixes or comments to feedback@suse.de.
#
# * Thu Mar 12 13:59:22 MET 1998 - Werner Fink, <werner@suse.de>
#   This makewhatis is for system which uses a modern man utility,
#   which uses a hash table for indexing man pages. Such a system
#   is e.g. SuSE Linux.
#
# * Wed Dec  1 17:53:44 CET 1999 - Werner Fink, <werner@suse.de>
#   make this script able to use the manpath program and
#   try to get /usr/ within ro state after first run.
#
# * Tue Aug  6 08:52:14 CEST 2013 - Werner Fink, <werner@suse.de>
#   Redirect stderr of whatis(1) to catch standard warning message
#   of whatis(1) (bnc#832678)
#
# For debugging
#
debug=0

#
PATH=/usr/sbin:/usr/bin:/sbin:/bin
export PATH

#
test $debug -eq 0 -a "$UID" != "0" && \
    { echo "$0: Only root should call ${0##/*/}" 1>&2 ; exit 1; }

#
# Some useful settings, e.g. MANPATH
#
: ${CATMANPATH=/var/cache/man}
test -f ${CATMANPATH}/index.bt -o -f ${CATMANPATH}/index.db -o \
     -f ${CATMANPATH}/index.dir || \
    { echo "makewhatis: This version of makewhatis only supports man utilities" 1>&2 ;
      echo "            with indexing database system. " 1>&2 ; exit 1; }

#
OIFS="$IFS"
IFS=":$IFS"
manpath="$(unset MANPATH; manpath -qg 2> /dev/null)"
manpath="$(echo $manpath)"
IFS="$OIFS"

#
# Control given options
#
 section="$SECTION"
  update=0
 verbose=0
while getopts ":m:uv" y ; do
    case $y in
	m) manpath="$OPTARG" ;;
	u) update=1   ;;
	v) verbose=1  ;;
	*) echo "Usage: makewhatis [-u] [-v] [-m manpath]" 1>&1
	   echo "   This will build the old style whatis database" 1>&1
	   echo "   for the man pages found in manpath by using the" 1>&1
	   echo "   new index database scheme (see mandb(8))." 1>&1
	   echo "   Available options are:" 1>&1
	   echo "       -u          update whatis database with new pages" 1>&1
	   echo "       -v          give some infomation about progress" 1>&1
	   echo "       -m manpath  Override system default" 1>&1
	   echo "   $MANPATH" 1>&1
	   exit 1  ;;
    esac
done
test $debug -eq 1 && verbose=1
unset y

#
# Update if requested
#
mandb="mandb 1>&2"
test $verbose -eq 0 && mandb="mandb -q > /dev/null 2>&1"
test $update  -eq 1 && eval $mandb

#
# Main loop
#
top=$(pwd)
#
for loc in $manpath ; do
    if test -d $loc ; then
	cd $loc; test $verbose -eq 1 && echo "$0: processing $loc" 1>&2

	if test $debug -eq 0 ; then

	    output="$(MANPATH=$loc manpath -cq)/whatis"
	    link="${loc}/whatis"

	    if test $link != $output -a ! -L $link ; then
		rm  -f $link
		ln -sf $output $link
		test $? -gt 0 && continue
	    fi

	    trap "> $output; exit 1" HUP INT PIPE TRAP TERM
	else
	    output="/dev/null"
	fi
	whatis -M$loc -w '*' 2>/dev/null > >(exec sed '
		/^\*: nothing appropriate.$/D;	# Remove whatis message about *
		s/^\"//g;			# Remove leading double quotes
		s/[ 	]\+$//g; 		# Remove trailing spaces
		s/^[ 	]\+//g;			# Remove leading spaces
		s/-*$//g;			# Remove leading lines
		s/\( .\)\+$//g;			# Remove leading quotes and spaces
	' > $output )
	touch $output
	test $debug -eq 0 && chmod 644 $output
    fi
done
cd $top
#
# Bye
#
exit 0
