#!/bin/bash
#
## ::maintainer::
## ::copy::
##
## /etc/init.d/enable-nss-ldap
##
## This script is launched at system startup after
## the network startup stage.
##
## The purpouse is to restore the ldap-enabled
## /etc/nsswitch.conf if disabled by the companion
## script disable-nss-ldap in the previous
## startup stages. The file is restored only if
## the ldap server is online and reachable.
##

case "$1" in
   start) ;;	# start, ok
   *) exit 0 ;;	# ignore all other options
esac

tag="[ku]"

. /lib/lsb/init-functions

nssfile=/etc/nsswitch.conf

[ -f ${nssfile}-ldap ]		|| exit 0
[ -f ${nssfile}-no-ldap ]	|| exit 0

[ -f /etc/ldap.conf ]	|| exit 0
host=`grep "^host[ ,	]" /etc/ldap.conf | sed -e 's/host[ ,	]*//'`
[ X"$host" = X ]	&& exit 0

log_action_begin_msg "$tag checking LDAP server '$host' ..."

if ping -c 1 -w 3 $host 2>/dev/null >/dev/null 
then
	log_end_msg 0
	cp -a ${nssfile}-ldap $nssfile
	[ -f /etc/init.d/nscd ] && /etc/init.d/nscd restart
else
	log_end_msg 1
	log_warning_msg "$tag LDAP server unreachable (LDAP disabled)"
	cp -a ${nssfile}-no-ldap $nssfile
	[ -f /etc/init.d/nscd ] && /etc/init.d/nscd stop
fi

# upstart?
#
[ -d /etc/init ] && {
	initctl emit --no-wait "ldap-checked"
}

# cron must be restarted, some user can that was not available at boot
# time can be available 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
