#!/bin/sh
# vim: ts=4:et:sw=4

set -e


CACHEDIR=/var/cache/man2html

[ -d "$CACHEDIR" ] && which index++ >/dev/null || exit 0

MANPATH=/usr/bin/manpath
MANPATH_CONFIG=/etc/manpath.config

# Get global man directories to index. See manpath(5) for more information.
if [ -x "$MANPATH" ] && [ -f "$MANPATH_CONFIG" ] ; then
     MANDIRS=$("$MANPATH" -C "$MANPATH_CONFIG" -g | tr : ' ')
else
     # Use some sensible defaults when manpath(1) is not available.
     MANDIRS="/usr/local/share/man /usr/share/man /opt/man"
fi



# Expand the man directories. Remove any non-existant directories. Use 
# device & inode numbers  to remove any symlinked directories from the list.
MANDIRS=$(find -L $MANDIRS -maxdepth 1 -type d -name 'man?' -printf "%D %i %p\n" 2>/dev/null | \
          sort -t' ' -k1,2 -u | \
          cut -d' ' -f 3)

### echo -e "Expanded MANDIRS are:\n$MANDIRS"

# Return when it expands to nothing.
[ -n "$MANDIRS" ] || exit 0


# See ionice(1)
[ -x /usr/bin/ionice ] &&  IONICE="/usr/bin/ionice -c3" || IONICE=

# Index the man pages
$IONICE index++ --config-file=/usr/share/man2html/swish++.conf \
                --index-file="$CACHEDIR"/man2html.swish++.index.tmp \
                $MANDIRS

mv -f "$CACHEDIR"/man2html.swish++.index.tmp \
      "$CACHEDIR"/man2html.swish++.index

chmod 644 "$CACHEDIR"/man2html.swish++.index

exit 0
