--- a/debian/control
+++ b/debian/control
@@ -7,7 +7,7 @@ Standards-Version: 3.8.4
 Build-Depends: debhelper (>= 5), cdbs (>= 0.4.23-1.1), libgettext-ruby-util
 
 Package: dhelp
-Depends: perl-modules, libtemplate-perl, libhtml-parser-perl, liburi-perl, ruby1.8, libdb-ruby1.8, libcommandline-ruby1.8, libgettext-ruby1.8, doc-base, swish++, liblocale-gettext-perl, libdata-page-perl, pstotext, poppler-utils, ${misc:Depends}
+Depends: perl-modules, libtemplate-perl, libhtml-parser-perl, liburi-perl, ruby1.8, libdb-ruby1.8, libcommandline-ruby1.8, libgettext-ruby1.8, doc-base, swish++, liblocale-gettext-perl, libdata-page-perl, pstotext, poppler-utils, ucf (>= 0.8), ${misc:Depends}
 Recommends: iceweasel | firefox | www-browser
 Suggests: httpd, info2www, man2html, lynx | links | w3m | html2text, catdvi
 Architecture: all
new file mode 100755
--- /dev/null
+++ b/debian/cron.monthly
@@ -0,0 +1,28 @@
+#!/bin/sh
+# Remove all but the newest five *.index-log.* files, to keep them
+# from accumulating inside /var/lib/dhelp/tmp.
+#
+# 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.
+
+if [ -d /var/lib/dhelp/tmp ]; then
+  (
+    cd /var/lib/dhelp/tmp || exit
+    ls *.index-log.* \
+    | sort --numeric-sort --field-separator='-' --key=1,4 --key=6,7 --key=9,10 \
+    | head --lines=-5 \
+    | xargs --no-run-if-empty --max-args=1 --max-lines=1 rm -f
+  )
+fi
--- a/debian/dirs
+++ b/debian/dirs
@@ -2,3 +2,4 @@
 var/lib/dhelp
 var/lib/dhelp/tmp
 etc/apache2/conf.d
+etc/apt/apt.conf.d
--- a/debian/postinst
+++ b/debian/postinst
@@ -34,13 +34,43 @@
 # 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"
-    /etc/cron.weekly/dhelp 2>/dev/null >/dev/null &
+    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#
--- a/debian/postrm
+++ b/debian/postrm
@@ -17,9 +17,20 @@
 # Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 # MA  02111-1307  USA.
 
-if [ "$1" = purge ]; then
-    # remove files generated by postinst
-	rm -rf /etc/dhelp
+# remove and unregister with ucf our dpkg post-invoke hook
+
+DHELP_APTHOOK=/etc/apt/apt.conf.d/35dhelp
+
+for ext in '' '~' '%' .bak .ucf-new .ucf-old .ucf-dist; do
+    rm -f ${DHELP_APTHOOK}${ext}
+done
+if which ucf >/dev/null; then
+    ucf --purge ${DHELP_APTHOOK}
 fi
+if which ucfr >/dev/null; then
+    ucfr --purge dhelp ${DHELP_APTHOOK}
+fi
+
+unset DHELP_APTHOOK || true
 
 #DEBHELPER#
