#!/bin/sh
set -e

catdir=/var/cache/man

ensure_catdir () {
    if [ ! -d $catdir ]; then
	# Make sure the cat directory exists.
	install -d -o man -g man -m 0755 $catdir
    fi
}

run_mandb () {
    if [ ! -e /var/lib/man-db/auto-update ]; then
	echo "Not building database; man-db/auto-update is not 'true'." >&2
	return 0
    fi
    # shellcheck disable=SC3043
    local message="$1"
    shift
    [ "$message" ] && echo "$message" >&2
    # start-stop-daemon isn't available when running from debootstrap.
    if command -v setpriv >/dev/null; then
	setpriv --reuid man --regid man --init-groups -- /usr/bin/mandb "$@" || true
    else
	# runuser isn't ideal here, because it starts a PAM session.
	# However, setpriv is only available on Linux.
	runuser -u man -- /usr/bin/mandb "$@" || true
    fi
}

if [ "$1" = triggered ]; then
    ensure_catdir
    # We don't print a status message here, as dpkg already said
    # "Processing triggers for man-db ...".
    run_mandb "" -pq
    exit 0
fi

# shellcheck disable=SC1091
. /usr/share/debconf/confmodule
db_version 2.0

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

maybesetuid='man mandb'

db_get man-db/install-setuid

if [ "$RET" = true ]; then
    # man and mandb are to be installed setuid man.
    owner=man:man
    mode=6755
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 ! dpkg-statoverride --list "/usr/bin/$x" >/dev/null; then
	chown $owner "/usr/bin/$x" || true
	chmod $mode  "/usr/bin/$x"
    fi
done

ensure_catdir

# debconf forms the interface used to suppress man-db updates during
# automatic package builds (see https://bugs.debian.org/554914), but we
# cache that value in the file system to avoid having to use debconf when
# processing triggers.
db_get man-db/auto-update
if [ "$RET" = true ]; then
    touch /var/lib/man-db/auto-update
else
    rm -f /var/lib/man-db/auto-update
fi

build_db=0

if [ ! -f $catdir/index.db ]; then
    # Build the database from scratch on fresh installs.
    build_db=1
fi

if [ $build_db -eq 1 ]; then
    # Chances are we're being run from debootstrap, which will have problems
    # if mandb runs backgrounded for too long (bug #100616).
    run_mandb "Building database of manual pages ..." -cq
else
    # Otherwise, just update the database in the foreground. It's unlikely
    # to take very long, and configuration needs to cover everything that
    # happens when we're triggered.
    run_mandb "Updating database of manual pages ..." -pq
fi

# Automatically added by dh_apparmor/3.0.8-3
if [ "$1" = "configure" ]; then
    APP_PROFILE="/etc/apparmor.d/usr.bin.man"
    if [ -f "$APP_PROFILE" ]; then
        # Add the local/ include
        LOCAL_APP_PROFILE="/etc/apparmor.d/local/usr.bin.man"

        test -e "$LOCAL_APP_PROFILE" || {
            mkdir -p `dirname "$LOCAL_APP_PROFILE"`
            install --mode 644 /dev/null "$LOCAL_APP_PROFILE"
        }

        # Reload the profile, including any abstraction updates
        if aa-enabled --quiet 2>/dev/null; then
            apparmor_parser -r -T -W "$APP_PROFILE" || true
        fi
    fi
fi
# End automatically added section
# Automatically added by dh_installtmpfiles/13.11.4
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
	if [ -x "$(command -v systemd-tmpfiles)" ]; then
		systemd-tmpfiles ${DPKG_ROOT:+--root="$DPKG_ROOT"} --create man-db.conf >/dev/null || true
	fi
fi
# End automatically added section
# Automatically added by dh_installsystemd/13.11.4
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
	# The following line should be removed in trixie or trixie+1
	deb-systemd-helper unmask 'man-db.timer' >/dev/null || true

	# was-enabled defaults to true, so new installations run enable.
	if deb-systemd-helper --quiet was-enabled 'man-db.timer'; then
		# Enables the unit on first installation, creates new
		# symlinks on upgrades if the unit file has changed.
		deb-systemd-helper enable 'man-db.timer' >/dev/null || true
	else
		# Update the statefile to add new symlinks (if any), which need to be
		# cleaned up on purge. Also remove old symlinks.
		deb-systemd-helper update-state 'man-db.timer' >/dev/null || true
	fi
fi
# End automatically added section
# Automatically added by dh_installsystemd/13.11.4
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
	if [ -d /run/systemd/system ]; then
		systemctl --system daemon-reload >/dev/null || true
		if [ -n "$2" ]; then
			_dh_action=restart
		else
			_dh_action=start
		fi
		deb-systemd-invoke $_dh_action 'man-db.service' 'man-db.timer' >/dev/null || true
	fi
fi
# End automatically added section


exit 0
