#!/bin/sh -e 
#
# Copyright 2001,2002 by Stefan Hornburg (Racke) <racke@linuxia.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA  02111-1307  USA.

# create /var/lib/dhelp if necessary
if [ ! -d /var/lib/dhelp ]; then
    mkdir /var/lib/dhelp
fi

# remove /usr/doc/HTML2 if upgrading from old dhelp package
if [ "$1" = configure -a -d /usr/doc/HTML2 ]; then
    rm -rf /usr/doc/HTML2
fi

# remove old /var/lib/dhelp/swish++.index (versions older than 0.6)
rm -f /var/lib/dhelp/swish++.index

# hack to be able to tell if the package is configured, so 3rd party packages
# that call dhelp don't barf (from scrollkeeper package)
touch /var/lib/dhelp/configured

DHELP_INDEXER=/usr/share/dhelp/index-deferred
DHELP_APTHOOK=/etc/apt/apt.conf.d/35dhelp

# create and register with ucf our dpkg post-invoke hook

DHELP_POSTCMD="\"if test -x ${DHELP_INDEXER}; then ${DHELP_INDEXER}; fi\""
DHELP_TMP_APTHOOK=`mktemp 2>/dev/null`
if [ $? -ne 0 ]; then
    >&2 echo "Error: unable to create temporary file"
    exit 1
fi
if ! chmod 644 ${DHELP_TMP_APTHOOK}; then
    >&2 echo "Error: unable to change mode of temporary file"
    exit 2
fi
echo "Dpkg::Post-Invoke { ${DHELP_POSTCMD}; };" > ${DHELP_TMP_APTHOOK}

ucf --debconf-ok --three-way ${DHELP_TMP_APTHOOK} ${DHELP_APTHOOK}
rm -f ${DHELP_TMP_APTHOOK}
ucfr dhelp ${DHELP_APTHOOK}

# 'dhelp_parse -r', among other actions, produces the pending list.
echo -n "Building HTML tree..."
dhelp_parse -r
echo " done."

# Thus, if index file does not exist, it suffices to do incremental indexing.
if [ ! -f /var/lib/dhelp/documents.index ]; then
    echo "Reindexing documentation in the background"
    if [ -x ${DHELP_INDEXER} ]; then
        ${DHELP_INDEXER}
    else
        >&2 echo "Error: ${DHELP_INDEXER} does not exist or is not executable"
        exit 3
    fi
fi

unset DHELP_INDEXER DHELP_POSTCMD DHELP_APTHOOK DHELP_TMP_APTHOOK || true

#DEBHELPER#
