#!/bin/sh
#
# chkconfig:	345 91 35
# description:	Starts and stops the Samba smbd and nmbd daemons \
#		used to provide SMB network services.
#
# config:	/etc/samba/smb.conf
# config:	/etc/samba/lmhosts
# processname:	nmbd
# processname:	smbd

export PATH=/bin:/sbin:/usr/bin:/usr/sbin

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

# Source networking configuration.
. /etc/sysconfig/network

# Demon specified configuration.
. /etc/sysconfig/samba

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down smb
		exit 1
	fi
else
	exit 0
fi

TMPDIR="/tmp"; export TMPDIR
unset TMP || :

# Check that smb.conf exists.
[ -f /etc/samba/smb.conf ] || exit 0

start() {
	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/smb ]; then
		msg_starting smbd
		daemon /usr/sbin/smbd -D
		RETVAL=$?
		msg_starting nmbd
		daemon /usr/sbin/nmbd -D
		[ $RETVAL -eq 0 ] && RETVAL=$?
		if [ $RETVAL -eq 0 ]; then
			touch /var/lock/subsys/smb
		fi
	else
		msg_already_running smb
	fi
}

stop() {
	# Stop daemons.
	if [ -f /var/lock/subsys/smb ]; then
		msg_stopping smbd
		killproc smbd
		msg_stopping nmbd
		killproc nmbd
		rm -f /var/lock/subsys/smb >/dev/null 2>&1
	else
		msg_not_running smb
	fi
}

condrestart() {
	if [ -f /var/lock/subsys/smb ]; then
		stop
		start
	else
		msg_not_running smb
		RETVAL=$1
	fi
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	stop
	start
	;;
  try-restart)
	condrestart 0
	;;
  reload|force-reload)
	if [ -f /var/lock/subsys/smb ]; then
		msg_reloading smb
		killproc smbd -HUP
		RETVAL=$?
	else
		msg_not_running smb
		exit 7
	fi
	;;
  status)
	status smbd
	RETVAL=$?
	status nmbd
	RET=$?
	[ $RETVAL -eq 0 ] && RETVAL=$RET
	smbstatus
	;;
  *)
	msg_usage "$0 {start|stop|restart|try-restart|reload|force-reload|status}"
	exit 3
esac

exit $RETVAL
