#!/bin/bash
#
# ::maintainer::
# ::copy::
#
### BEGIN INIT INFO
# Provides:          ku-disable-nss-ldap
# Required-Start:    mountall-bootclean
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start:     S
# Default-Stop:      0 6
# Short-Description: disable ldap in nsswitch.conf
# Description:       disable ldap in nsswitch.conf
### END INIT INFO
#
# This script is launched at basic system startup stage (/etc/rcS.d) just
# after the r/w remount of the root filesystem (assuming that /etc is
# not in a separate filesystem).
#
# The purpouse is to rename the file nsswitch.conf if it contains "ldap"
# instructions, to avoid systems hangs in the very basic system startup
# stages if the network is down or the ldap server is unreachable.
#
# The file nsswitch.conf will be restored by the companion script
# ku-enable-nss-ldap in the subsequent startup stages.
#
tag="[ku]"
nssfile=/etc/nsswitch.conf

. /lib/lsb/init-functions

[ -f ${nssfile}-no-ldap ] || exit 0	# noting to do

case "$1" in
   start)
	[ -f $nssfile ] && {
		grep -q "group.*ldap" $nssfile && {
			log_warning_msg "$tag disabling LDAP nsswitch.conf"
			cp -a ${nssfile}-no-ldap $nssfile || exit $?
			[ -f /etc/init.d/nscd ] && /etc/init.d/nscd stop
			[ -f /etc/init.d/nslcd ] && /etc/init.d/nslcd stop
		}
	}
	;;
esac

# cron must be restarted, some user can that was available with LDAP
# time can be unavailable now
#
[ "$(ps x | grep ' /usr/sbin/cron$' | fgrep -v grep)" != "" ] && {
	if [ -f /etc/init/cron.conf ]
	then
		# upstart
		restart cron
	elif [ -f /etc/init.d/cron ]
	then
		# old sysv init
		/etc/init.d/cron restart
	fi
}

exit 0
