#!/bin/sh
#
# chkconfig: 234 91 35
# description: Starts and stops the Samba smbd and nmbd daemons \
#	       used to provide SMB network services.

# 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/smb
RETVAL=0

start()
{
	echo -n "Starting CIFS services: "
	echo -n " NetBIOS over TCP/IP server,"
	start-stop-daemon --start --quiet --exec /usr/sbin/nmbd -- -D
	RETVAL=$?

	echo -n " Samba server"
	start-stop-daemon --start --quiet --exec /usr/sbin/smbd -- -D
	RETVAL2=$?
	if [ $RETVAL -eq 0 -a $RETVAL2 -eq 0 ] ; then
		success "CIFS startup"
		touch "$LOCKFILE"
	else
		RETVAL=1
		failure "CIFS startup"
	fi
	echo
	return $RETVAL
}

stop()
{
	echo -n "Shutting down CIFS services: "
	echo -n " Samba server,"
	# before shutting down the server, force share closing.
	# This way the client will be notified about change of server state
	smbcontrol smbd shutdown 2>/dev/null >/dev/null
	RETVAL=$?

	echo -n " NetBIOS over TCP/IP server"
	smbcontrol nmbd shutdown 2>/dev/null >/dev/null
	RETVAL2=$?
	[ $RETVAL -eq 0 -a $RETVAL2 -eq 0 ] && success "CIFS shutdown"
	[ $RETVAL -eq 0 -a $RETVAL2 -eq 0 ] || failure "CIFS shutdown"
	[ $RETVAL -eq 0 -a $RETVAL2 -eq 0 ] && rm -f "$LOCKFILE"
	echo
	return $RETVAL
}	

restart()
{
	stop
	start
}	

reload()
{
        echo -n "Reloading smb.conf file: "
	echo -n " NetBIOS over TCP/IP server,"
	smbcontrol nmbd reload-config  2>/dev/null >/dev/null
	RETVAL2=$?
	echo -n " Samba server"
	smbcontrol smbd reload-config  2>/dev/null >/dev/null
	RETVAL=$?
	[ $RETVAL -eq 0 -a $RETVAL2 -eq 0 ] && success "CIFS shutdown"
	[ $RETVAL -eq 0 -a $RETVAL2 -eq 0 ] || failure "CIFS shutdown"
	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 smbd
		RETVAL=$?
		status nmbd
		RETVAL2=$?
		[ $RETVAL -eq 0 -a $RETVAL2 -eq 0 ] ||
		RETVAL=1
		;;
	*)
		echo "Usage: ${0##*/} {start|stop|reload|restart|condstop|condrestart|status}"
		RETVAL=1
esac

exit $RETVAL
