#!/bin/sh
#
# nscd:		Starts the Name Switch Cache Daemon
#
# chkconfig: - 30 74
# description:  This is a daemon which handles passwd and group lookups \
#		for running programs and cache the results for the next \
#		query.  You should start this daemon if you use \
#		slow naming services like NIS, NIS+, LDAP, or hesiod.
# processname: /usr/sbin/nscd
# config: /etc/nscd.conf
#

DAEMON=/usr/sbin/nscd

# Sanity checks.
[ -x "$DAEMON" -a -s /etc/nscd.conf ] || exit

# Source function library.
. /etc/init.d/functions

# nscd does not run on any kernel lower than 2.2.0 because of threading
# problems, so we require that in first place.
case $(uname -r) in
	2.[2-9].*)
		# this is okay
		;;
	[3-9]*)
		# these are of course also okay
		;;
	*)
		# this is not
		exit 0
		;;
esac

PROG=nscd
LOCKFILE=/var/lock/subsys/nscd
RETVAL=0

start ()
{
	secure=
#	for table in passwd group; do
#		if egrep -qs '^'$table':.*nisplus' /etc/nsswitch.conf; then
#			/usr/lib/nscd_nischeck $table ||
#				secure="$secure -S $table,yes"
#		fi
#	done
	echo -n "Starting $PROG: "
	daemon "$DAEMON" $secure
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch "$LOCKFILE"
	return $RETVAL
}

stop ()
{
	echo -n "Stopping $PROG: "
	"$DAEMON" -K
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
		rm -f "$LOCKFILE"
		# nscd won't be able to remove these if it is running as
		# a non-privileged user
		rm -f /var/run/{nscd.pid,.nscd_socket}
		success "$PROG shutdown"
	else
		failure "$PROG shutdown"
	fi
	echo
	return $RETVAL
}

reload()
{
	echo -n "Reloading $PROG: "
	killproc -HUP "$DAEMON"
	RETVAL=$?
	echo
	return $RETVAL
}

restart()
{
	stop
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status nscd
		RETVAL=$?
		;;
	reload)
		reload
		;;
	restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	*)
		echo "Usage: ${0##*/} {start|stop|status|restart|reload|condstop|condrestart}"
		RETVAL=1
		;;
esac

exit $RETVAL
