#!/bin/sh
#
# Name: samba  
# Date: 2006-07-24 12:40
# Author: MontaVista Software, Inc. <source@mvista.com>
# Copyright: Copyright 1999-2003 MontaVista Software, Inc.
# License: 2003 (c) MontaVista Software, Inc. This file is licensed
#          under the terms of the GNU General Public License version 2.
#          This program is licensed "as is" without any warranty of any
#          kind, whether express or implied.
  #
# Copyright 2002, 2003, 2004 Sony Corporation
# Copyright 2002, 2003, 2004 Matsushita Electric Industrial Co., Ltd.
#
### BEGIN INIT INFO
# Required-Start: 
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start/stop samba daemon
# Description: start and stop the Samba daemon (nmbd and smbd).
### END INIT INFO 
#
# chkconfig: 2345 45 45 
#

# Defaults
RUN_MODE="daemons"

# Reads config file (will override defaults above)
[ -r /etc/default/samba ] && . /etc/default/samba

# jsn
#[ ! -e /var/run/samba ] mkdir /var/run/samba

#NMBDPID=/var/run/samba/nmbd.pid
#SMBDPID=/var/run/samba/smbd.pid
SMBDPID=/var/run/smbd.pid

# clear conflicting settings from the environment
unset TMPDIR

# 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

		if [ "$RUN_MODE" != "inetd" ]; then
			echo -n " smbd"
			start-stop-daemon --start --quiet --exec /usr/sbin/smbd -- -D
		fi

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

		start-stop-daemon --stop --quiet --pidfile $NMBDPID
		# Wait a little and remove stale PID file
		sleep 1
		if [ -f $NMBDPID ] && ! ps h `cat $NMBDPID` > /dev/null
		then
			# Stale PID file (nmbd was succesfully stopped),
			# remove it (should be removed by nmbd itself IMHO.)
			rm -f $NMBDPID
		fi
		echo -n "nmbd"

		if [ "$RUN_MODE" != "inetd" ]; then
			start-stop-daemon --stop --quiet --pidfile $SMBDPID
			# Wait a little and remove stale PID file
			sleep 1
			if [ -f $SMBDPID ] && ! ps h `cat $SMBDPID` > /dev/null
			then
				# Stale PID file (nmbd was succesfully stopped),
				# remove it (should be removed by smbd itself IMHO.)
				rm -f $SMBDPID
			fi
			echo -n " smbd"
		fi

		echo "."

		;;
	reload)
		echo -n "Reloading /etc/samba/smb.conf (smbd only)"
		start-stop-daemon --stop --signal HUP --pidfile $SMBDPID

		echo "."
		;;
	restart|force-reload)
		$0 stop
		sleep 1
		$0 start
		;;
	*)
		echo "Usage: /etc/init.d/samba {start|stop|reload|restart|force-reload}"
		exit 1
		;;
esac

exit 0
