#!/bin/sh
#
# chkconfig: 234 92 34
# description: Starts and stops the Samba Winbindd daemon \
#	       used to provide seamless NT domain integration.

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

# Source networking configuration.
# Check that networking is up.
# Check that smb.conf exists.
SourceIfNotEmpty /etc/sysconfig/network && [ "$NETWORKING" != no ] && [ -s /etc/samba/smb.conf ] || exit

# msg_

export TMPDIR=/tmp
LOCKFILE=/var/lock/subsys/winbind
RETVAL=0

start()
{
	echo -n "Starting Winbind services: "
	start-stop-daemon --start --quiet --exec /usr/sbin/winbindd -- -B
	RETVAL=$?
	if [ $RETVAL -eq 0 ] ; then
		touch "$LOCKFILE"
		success "Winbind startup"
	else
		RETVAL=1
		failure "Winbind startup"
	fi
	echo
	return $RETVAL
}

stop()
{
	echo -n "Shutting down Winbind services: "
	smbcontrol winbindd shutdown 2>/dev/null >/dev/null
	RETVAL=$?
	if [ $RETVAL -eq 0 ] ; then
		success "Winbind shutdown"
		rm -f "$LOCKFILE"
	else
		RETVAL=1
		failure "Winbind shutdown"
	fi
	echo
	return $RETVAL
}	

restart()
{
	stop
	start
}	

reload()
{
        echo -n "Reloading smb.conf file: "
	smbcontrol winbindd reload-config  2>/dev/null >/dev/null
	RETVAL=$?
	[ $RETVAL -eq 0 ] && success "Winbind config reload"
	[ $RETVAL -eq 0 ] || failure "winbind config reload"
	echo
	return $RETVAL
}	

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

exit $RETVAL
