#!/bin/sh
#
# Start/stops the Samba daemons (nmbd and smbd).
#

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DEBIAN_CONFIG=/etc/samba/debian_config

# Sanity check: see if Samba has been configured on this system.
if [ ! -f $DEBIAN_CONFIG ]; then
	echo "The file $DEBIAN_CONFIG does not exist! There is something wrong"
	echo "with the installation of Samba on this system. Please re-install"
	echo "Samba. I'll try to continue..."
fi

# If Samba is running from inetd then there is nothing to do
if grep -q '^netbios-ns' /etc/inetd.conf; then
	echo "Warning: Samba is not running as daemons. Daemons not restarted/stopped."
	echo "Daemons will start automatically by inetd. Use 'killall nmbd' and"
	echo "'killall smbd' to stop the Samba daemons (they'll start again)."
	exit 0
fi

# See if the daemons are there
test -x /usr/sbin/nmbd -a -x /usr/sbin/smbd || exit 0

case "$1" in
	start)
		echo -n "Starting Samba daemons:"

		echo -n " nmbd"
		start-stop-daemon --start --quiet --exec /usr/sbin/nmbd -- -D -a

		echo -n " smbd"
		start-stop-daemon --start --quiet --exec /usr/sbin/smbd -- -D

		echo "."
		;;
	stop)
		echo -n "Stopping Samba daemons:"

		echo -n " nmbd"
		start-stop-daemon --stop --quiet --exec /usr/sbin/nmbd -- -D -a

		echo -n " smbd"
		start-stop-daemon --stop --quiet --exec /usr/sbin/smbd -- -D

		echo "."
		;;
	restart|force-reload)
		echo -n "Restarting Samba daemons:"

		echo -n " nmbd"
		start-stop-daemon --stop --quiet --exec /usr/sbin/nmbd -- -D -a
		sleep 2
		start-stop-daemon --start --quiet --exec /usr/sbin/nmbd -- -D -a

		echo -n " smbd"
		start-stop-daemon --stop --quiet --exec /usr/sbin/smbd -- -D
		sleep 2
		start-stop-daemon --start --quiet --exec /usr/sbin/smbd -- -D

		echo "."
		;;
	*)
		echo "Usage: /etc/init.d/samba {start|stop|restart}"
		exit 1
		;;
esac

exit 0
