#!/bin/sh
#
# man postinst
#
# this script is executed after all the files in the package are
# installed: it builds the catman hierarchy and then the database.

set -e

package="man-db"
oldcatdir="/var/catman"
catdir="/var/cache/man"
conffile="/etc/manpath.config"
chconfig="/usr/lib/man-db/chconfig"

test -s $conffile || cp /usr/share/doc/man-db/examples/manpath.config $conffile

# Permission processing inserted by hand, don't rely on debstd :-(
# moved on top because later progs will be running.
  # adding NOSETGID alone on a line in the conffile, avoid setgid binaries
if	! `egrep -q '^[      ]*NOSETGID[     ]*$' $conffile `
then	if [ -x "$(which suidregister)" -a -e /etc/suid.conf ]
	then
		suidregister -s man-db	/usr/lib/man-db/man	man root 4755
		suidregister -s man-db	/usr/lib/man-db/mandb	man root 4755
	else
		chown man.root	/usr/lib/man-db/man /usr/lib/man-db/mandb
		chmod	4755	/usr/lib/man-db/man /usr/lib/man-db/mandb
	fi
fi

  # substitute the empty files with hardlinks
if	[ -x /usr/bin/wrapper ]
then	rm -f /usr/bin/man || true
	ln /usr/bin/wrapper /usr/bin/man
	rm -f /usr/bin/mandb || true
	ln /usr/bin/wrapper /usr/bin/mandb
fi

if	[ ! -e /usr/bin/pager ]
then	if	[ -x /bin/more ]
	then	ln -sf /bin/more /usr/sbin/man-pager
		update-alternatives --install /usr/bin/pager pager /usr/sbin/man-pager 1
	else	echo "  ============================"
		echo "  /usr/bin/pager not found ..."
		echo "  please upgrade your preferred pager"
		echo "  or util-linux at least."
		echo "  ============================"
	fi
fi

# removing all catpages hierarchy
if	[ -d $oldcatdir ]
then	echo "  Removing catpages as well as $oldcatdir hierarchy."
	rm -rf $oldcatdir 2>/dev/null || true
fi
if	[ -d $catdir ]
then	echo "  Removing catpages as well as $catdir hierarchy."
	rm -rf $catdir 2>/dev/null || true
fi

  # before running mandb -c  must build correct conffile
md5=`sed 's/\#.*$//' $conffile | tr -s '\t ' '  ' | sort -u | md5sum`
if `grep -q $md5 /usr/lib/man-db/config_md5 `
then cp /usr/share/doc/man-db/examples/manpath.config /etc
else
	if	[ -x "$(which perl)" -a -x "$chconfig" ]
	then	$chconfig $conffile
	else
		echo "  Cannot update $conffile."
		echo "  It is suggested to run command $chconfig ."
	fi
fi

install -d -g root -m 02755 -o man $catdir

if	[ -x "$(which mandb)" ]
then	echo "  Building manual page index in background."
	nice su man -c "mandb -c 2>/dev/null > /dev/null " &
fi

test "$1" = configure -a -x "$(which install-docs)" &&
	install-docs -i /usr/share/doc-base/${package} || 
	true

if	[ "$1" = "configure" ]
then	if	[ -d /usr/doc -a ! -e /usr/doc/${package} -a -d /usr/share/doc/${package} ]
	then	ln -sf ../share/doc/${package} /usr/doc/${package}
	fi
fi

