#!/bin/sh
#
# chkconfig: - 91 35
# description: Provides SMB networking, often used \
#	       to share files and printers with Windows. \
# \
#	       Enable this service to allow you to \
#              provide shares to Windows machines, use WINS network names, \
#	       or authenticate to Windows machines. \
# \
#              You may also enable this service if you wish to share printers \
#              with Windows using Windows protocols, rather than using Windows \
#              'Internet Printing' IPP support with CUPS.
#
# pidfile: /var/run/samba/smbd.pid
# pidfile: /var/run/samba/nmbd.pid
# config:  /etc/samba/smb.conf


# Source function library.
if [ -f /etc/init.d/functions ] ; then
  . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
  . /etc/rc.d/init.d/functions
else
  exit 0
fi

# Avoid using root's TMPDIR
unset TMPDIR

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

if [ -f /etc/sysconfig/samba ]; then
   . /etc/sysconfig/samba
fi

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

RETVAL=0


rootok() {
	if [ ! -w /etc/samba/smb.conf ]; then
		echo 'Config file not writable.  Got root?'
		exit 1
	fi
}

start() {
	rootok
        KIND="SMB"
	echo -n $"Starting $KIND services: "
	daemon smbd $SMBDOPTIONS
	RETVAL=$?
	echo
        KIND="NMB"
	echo -n $"Starting $KIND services: "
	daemon nmbd $NMBDOPTIONS
	RETVAL2=$?
	echo
	[ $RETVAL -eq 0 -a $RETVAL2 -eq 0 ] && touch /var/lock/subsys/smb || \
	   RETVAL=1
	return $RETVAL
}

stop() {
	rootok
        KIND="SMB"
	echo -n $"Shutting down $KIND services: "
	killproc smbd
	RETVAL=$?
	echo
	KIND="NMB"
	echo -n $"Shutting down $KIND services: "
	killproc nmbd
	RETVAL2=$?
	[ $RETVAL -eq 0 -a $RETVAL2 -eq 0 ] && rm -f /var/lock/subsys/smb
	echo ""
	return $RETVAL
}

restart() {
	stop
	start
}

reload() {
	rootok
        echo -n $"Reloading smb.conf file: "
	killproc smbd -HUP
	RETVAL=$?
	echo
	return $RETVAL
}

rhstatus() {
	status smbd
	RETVAL=$?
	status nmbd
	if [ $? -ne 0 -o $RETVAL -ne 0 ] ; then
		return 1
	fi
}
configtest() {
	# Check that smb.conf exists.
	if [ ! -r /etc/samba/smb.conf ]; then
		echo 'Config file (/etc/samba/smb.conf) missing !'
		exit 1
	fi

	# First run testparm and check the exit status.
	# If it ain't 0, there was a problem, show them the testparm output.
	# Most of the time, though, testparm gives 0 - obvious problems
	# (like a missing config file) that testparm normally tells us about
	# have been checked for earlier in this script.
	/usr/bin/testparm -s &> /dev/null
        RETVAL=$?
	if [ $RETVAL -ne 0 ]; then
		/usr/bin/testparm -s
		exit $RETVAL
	fi

	# Note: testparm returns 0 even if it has unknown parameters or errors.
	# Check for the appropriate error text, and print the relevant section
	# if it appears. Return '3' because testparm doesn't usually
	# use that.
	# Don't just blindly search for "unknown", because we don't want to
	# false positive on "comment = Tomb of the unknown soldier", etc.
	/usr/bin/testparm -s 2>&1 | grep '^Ignoring unknown parameter\|^ERROR: '  &> /dev/null
	if [ $? -eq 0 ]; then
		RETVAL=3
		/usr/bin/testparm -s 2>&1 | grep '^Ignoring unknown parameter\|^ERROR: '
		exit $RETVAL
	fi

	# If testparm didn't fail and there weren't any unknowns, exit.
	echo Syntax OK
        return $RETVAL
}


# Allow status as non-root.
if [ "$1" = status ]; then
       rhstatus
       exit $?
fi

case "$1" in
  start)
	configtest
  	start
	;;
  stop)
  	stop
	;;
  restart)
  	restart
	;;
  reload)
  	reload
	;;
  status)
  	rhstatus
	;;
  configtest)
	configtest
	;;
  condrestart)
	rootok
  	[ -f /var/lock/subsys/smb ] && restart || :
	;;
  *)
	echo $"Usage: $0 {start|stop|restart|reload|status|condrestart|configtest}"
	exit 1
esac

exit $?
