#!/bin/sh
# $Id: postinst 74 2007-05-06 10:56:47Z robert $
# 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.4
# upgrades of the following version requires complete re-registration
bad_ver=0.7.13
VERBOSE=

ctrldir="/usr/share/$package"
infodir="/var/lib/$package/info"

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

remove_obsolete() {
	# remove old dhelp files
	find $infodir -maxdepth 1 -type f -name "*.list" | xargs grep -h '^/.*/.dhelp' |  \
	while read f; do
		[ -e "$f" ] || continue;
		[ -x /usr/sbin/dhelp_parse ] && /usr/sbin/dhelp_parse -d "`dirname "$f"`" || true
		rm -f "$f"
	done

	# remove old scrollkeeper files
	find $infodir -maxdepth 1 -type f -name "*.status" | \
	xargs sed -ne 's/^Scrollkeeper-omf-file: *\(.*-C.omf\) *$/\1/p'  |  \
	while read f; do
		[ -e "$f" ] || continue;
		rm -f "$f"
		rmdir --ignore-fail-on-non-empty "`dirname "$f"`" || true
	done

	# remove status/list files
	find $infodir -maxdepth 1 -type f \( -name "*.status" -o -name "*.list" \) | \
	xargs rm -f
	
}


reinstall_docs () {
    install-docs ${VERBOSE} --install-all
}

register_our_docs () {
    install-docs ${VERBOSE} --install $ctrldir/$package	\
		  		      $ctrldir/install-docs-man
}

case "$1" in
  configure)


    if dpkg --compare-versions "$2" lt-nl 0.8.4; then
    	remove_obsolete
    fi		


    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
	register_our_docs
    fi
    ;;
esac

#DEBHELPER#

exit 0
