#!/bin/sh
# $Id: postinst 95 2007-11-27 23:11:50Z 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.7
# upgrades of the following version requires complete re-registration
bad_ver=0.7.13
VERBOSE=

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

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

remove_obsolete() {
	# remove old dhelp files (from versions <= 0.8.4)
	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 newer dhelp files
	find $infodir -maxdepth 1 -type f -name "*.status" | \
	xargs sed -ne 's/^Dhelp-file: *"*\(.*\.dhelp\)"* *$/\1/p'  |  \
	while read f; do
		[ -e "$f" ] || continue;
		rm -f "$f"
	done


	# remove status/list files
	find $infodir -maxdepth 1 -type f \( -name "*.status" -o -name "*.list" \) | \
	xargs rm -f
	
	# remove generated documents
	find $docsdir -maxdepth 1 -type f  | 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.7; then
    	remove_obsolete
    fi		


    if dpkg --compare-versions "$2" lt-nl "$compat_ver" ||
      [ -z "`/bin/ls $infodir`" ]; 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
