#! /bin/sh
#
# postinst	NIS suite postinst script.
#		configures the nis stuff.
#
# Version:	nis.postinst  3.9  28-Jun-2001  miquels
#

# Install the defaults file if needed, and update the NISSERVER
# setting if the preinst left a settings file behind.
install_defaults () {
	[ ! -s /etc/default/nis ] || return 0
	NISSERVER=false
	if [ -f /etc/default/nis.settings ]
	then
		. /etc/default/nis.settings
	fi
	sed -e 's/^\(NISSERVER\)=.*$/\1='"$NISSERVER/" \
		< /usr/share/nis/nis.default > /etc/default/nis
	rm -f /etc/default/nis.settings
}

set -e
umask 022

case "$1" in
	configure)
		;;
	abort-upgrade|abort-remove|abort-deconfigure)
		#
		#	Oops - restart NIS anyway.
		#
		if [ -x /etc/init.d/nis ] ; then
			if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
				invoke-rc.d nis start
			else
				/etc/init.d/nis start
			fi
		fi
		exit 0
		;;
	*)
		exit 0
		;;
esac

# Source debconf library.
. /usr/share/debconf/confmodule
db_version 2.0 || { echo "nis.config: need DebConf 2.0 or later"; exit 1; }

cd /etc/init.d
if [ -f nis ]; then
	chmod 755 nis
fi
install_defaults

. /etc/default/nis

# Retrieve the domain from Debconf and set it.
db_get nis/domain || true
if [ "$RET" != "" ]
then
	echo $RET > /etc/defaultdomain
fi

# Update the links for nis.
if [ -L /etc/rc2.d/S30nis ]
then
	rm -f /etc/rc?.d/[SK]30nis
fi
# The user may have decided to remove the init script...
if [ -x /etc/init.d/nis ]; then
	update-rc.d nis start 18 2 3 4 5 . stop 18 1 . >/dev/null 2>&1
fi

# Convert the old ypserv.conf to the new format.
if [ -f /etc/ypserv.conf ]
then
	/usr/lib/yp/convert-ypserv-conf /etc/ypserv.conf
fi

# And start the service.
if [ "$2" != "" -a "$2" != "<>" ] && dpkg --compare-versions "$2" le 3.9-1
then
	killall ypbind >/dev/null 2>&1 || true
fi

# Halt debconf so we don't leave the control fd open to the daemon
db_stop

# The user may have decided to remove the init script...
if [ -x /etc/init.d/nis ]; then
	invoke-rc.d nis restart

	# Remove shutdown and reboot links; this init script does not need them.
	if dpkg --compare-versions "$2" lt "3.17-18"; then
		rm -f /etc/rc0.d/K18nis /etc/rc6.d/K18nis
	fi
fi

# /sbin/unix_chkpwd is installed setgid shadow by default; for NIS we need
# setuid root. (See #155583)
if ! dpkg-statoverride --list /sbin/unix_chkpwd >/dev/null; then
    dpkg-statoverride --update --add root root 4755 /sbin/unix_chkpwd
fi

exit 0
