#!/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

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

start()
{
	echo -n "Starting SMB services: "
	daemon smbd -D 	
	RETVAL=$?
	echo

	echo -n "Starting NMB services: "
	daemon nmbd -D
	RETVAL2=$?
	echo
	[ $RETVAL -eq 0 -a $RETVAL2 -eq 0 ] &&
		touch "$LOCKFILE" ||
		RETVAL=1
	return $RETVAL
}

stop()
{
	echo -n "Shutting down SMB services: "
	smbcontrol smbd shutdown
	RETVAL=$?
	[ $RETVAL -eq 0 ] && success "SMB shutdown"
	[ $RETVAL -eq 0 ] || failure "SMB shutdown"
	echo
	echo -n "Shutting down NMB services: "
	smbcontrol nmbd shutdown
	RETVAL2=$?
	[ $RETVAL2 -eq 0 ] && success "NMB shutdown"
	[ $RETVAL2 -eq 0 ] || failure "NMB shutdown"
	[ $RETVAL -eq 0 -a $RETVAL2 -eq 0 ] && rm -f "$LOCKFILE"
	echo
	return $RETVAL
}	

restart()
{
	stop
	start
}	

reload()
{
        echo -n "Reloading smb.conf file: "
	killproc smbd -HUP
	RETVAL=$?
	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
