#!/bin/sh -e

[ "$1" = configure ] || exit 0

oldcatdir=/var/catman
catdir=/var/cache/man
maybesetuid='man mandb'
conffile=/etc/manpath.config


. /usr/share/debconf/confmodule
db_version 2.0
db_get man-db/install-setuid

# Sorry about this, but #98224 is right - statoverrides don't work as
# cleanly as I'd hoped here. I'm going to have to carry around some cruft
# for a while.
for x in $maybesetuid; do
    if dpkg --compare-versions "$2" eq 2.3.18-4 && \
	    [ "`dpkg-statoverride --list /usr/lib/man-db/$x`" = \
	      "man root 4755 /usr/lib/man-db/$x" ]; then
	dpkg-statoverride --remove /usr/lib/man-db/$x
    fi
done

if [ "$RET" = true ]; then
    # man and mandb are to be installed setuid man.
    owner=man:root
    mode=4755
else
    # man and mandb are not to be installed setuid.
    owner=root:root
    mode=0755
fi

for x in $maybesetuid; do
    # No statoverrides available or none exist for us ...
    if [ ! -x /usr/sbin/dpkg-statoverride ] || \
	    ! dpkg-statoverride --list /usr/lib/man-db/$x >/dev/null; then
	chown $owner /usr/lib/man-db/$x || true
	chmod $mode  /usr/lib/man-db/$x
    fi
    ln -sf ../lib/man-db/$x /usr/bin/$x
done

if [ -e /etc/cron.daily/man.moved-by-preinst ]; then
    rm /etc/cron.daily/man.moved-by-preinst
fi
if [ -e /etc/cron.weekly/catman.moved-by-preinst ]; then
    rm /etc/cron.weekly/catman.moved-by-preinst
fi

if dpkg --compare-versions "$2" lt 2.3.18; then
    # /usr/local/man now mapped to /var/cache/man/oldlocal
    if [ -d $catdir/local ] && [ ! -d $catdir/oldlocal ]; then
	mv -f $catdir/local $catdir/oldlocal
    fi
fi

if [ -d $catdir ]; then
    # Catdirs sometimes used to be created with the wrong permissions.
    if dpkg --compare-versions "$2" lt 2.3.20-4; then
	chown -R man /var/cache/man || true
    fi
else
    # Old packages removed catpages on upgrade. The preinst hack should have
    # avoided this, but let's be sure.
    install -d -o man -g root -m 02755 $catdir
fi

build_db=0

if dpkg --compare-versions "$2" lt 2.3.16 || \
   ([ ! -f $catdir/index.db ] && [ ! -f $catdir/index.bt ]); then
    # If the build-database question was never asked, this is probably a
    # fresh install, or maybe we're reconfiguring. The default is to build
    # the database.
    db_fget man-db/build-database seen
    if [ "$RET" = false ]; then
	build_db=1
    else
	# This should probably only fire when upgrading from less than
	# 2.3.16, but it doesn't really matter.
	db_get man-db/build-database
	if [ "$RET" = true ]; then
	    build_db=1
	fi
    fi
elif dpkg --compare-versions "$2" lt 2.4.2-1; then
    # Clean up old btree databases from before 2.4.2-1. They're useless now.
    find /var/cache/man -name index.bt -print0 | xargs -0r rm -f

    db_get man-db/rebuild-database
    if [ "$RET" = true ]; then
	build_db=1
    fi
fi

if [ $build_db -eq 1 ]; then
    frontend=`echo "$DEBIAN_FRONTEND" | tr '[:upper:]' '[:lower:]'`
    if [ "$frontend" = noninteractive ]; then
	# Run in the foreground. In this case, chances are we're being run
	# from debootstrap, which will have problems if mandb runs
	# backgrounded for too long (bug #100616).
	# start-stop-daemon isn't available when running from debootstrap.
	echo "Building database of manual pages ..." >&2
	perl -e '@pwd = getpwnam("man"); $( = $) = $pwd[3]; $< = $> = $pwd[2];
	         exec "/usr/bin/mandb", "-cq"' || true
    else
	echo "Building database of manual pages in the background." >&2
	# --pidfile /dev/null so it always starts; mandb isn't really a
	# daemon, but we want to start it like one.
	start-stop-daemon --start --pidfile /dev/null --background \
			  --startas /usr/bin/mandb --oknodo --chuid man \
			  -- --create --quiet
    fi
fi

#DEBHELPER#

# The upgrade is complete, so no more concern about this question being
# asked twice. Clean up the flag.
db_fset man-db/rebuild-database seen_in_2.4.2-1_upgrade false

db_stop


exit 0
