#!/bin/sh
# postinst for doc-base

# Abort if any command returns an error value
set -e

package=doc-base
# upgrades prior to this version require complete re-registration
compat_ver=0.8.1
# upgrades of the following version requires complete re-registration
bad_ver=0.7.13
VERBOSE=

if [ "$DEBUG" ]; then
    echo entering $package postinst
    set -x
fi

reinstall_docs ( ) {
    
    # install all the existing doc-base control files
    num=`find /usr/share/$package/ -maxdepth 1 -type f | wc -l`
    echo "(re)registering $num documents from /usr/share/$package ..." >&2

    for file in /var/lib/doc-base/info/*.status; do
    	[ -f "$file" ] && install-docs ${VERBOSE} --no-update-menus -r "`basename "$file" .status`" || true
    done

    for file in /usr/share/$package/*; do
        [ -f "$file" ] && install-docs ${VERBOSE} --no-update-menus -i "$file" || true  # don't abort on failure
    done

    if [ -x /usr/bin/update-menus ]; then
        /usr/bin/update-menus
    fi
    
    if [ -x /usr/bin/scrollkeeper-update ]; then
        /usr/bin/scrollkeeper-update
    fi
}

case "$1" in
  configure)
    if [ "${2+set}" != set ] || [ -z "$2" ] || [ "$2" = "<unknown>" ] || [ ! -d /var/lib/doc-base/info ]; then
        # first install, or upgrade from ancient version, or install from removed-but-not-purged state
        reinstall_docs
    elif dpkg --compare-versions $2 lt $compat_ver; then
	# version is less than last compatable version, we need to
	# re-register all the docs
        reinstall_docs
    elif dpkg --compare-versions $2 eq $bad_ver; then
        # bad version, ew, re-register
        reinstall_docs
    else
        # only register our stuff
        install-docs ${VERBOSE} --no-update-menus -i /usr/share/$package/$package
        install-docs ${VERBOSE} -i /usr/share/$package/install-docs-man
    fi
    ;;
esac

#DEBHELPER#

exit 0
