#! /bin/sh

#
# Samba server control
#

stop_samba()
{
    if [ -r /usr/samba/var/locks/smbd.pid ]; then
	smbd_group=`cat /usr/samba/var/locks/smbd.pid`
	ps -p $smbd_group | grep smbd > /dev/null
	if [ $? -eq 0 ]; then
	    /usr/bin/kill -15 -$smbd_group
	fi
    fi
    if [ -r /usr/samba/var/locks/nmbd.pid ]; then
	nmbd_group=`cat /usr/samba/var/locks/nmbd.pid`
	ps -p $nmbd_group | grep nmbd > /dev/null
	if [ $? -eq 0 ]; then
	    /usr/bin/kill -15 -$nmbd_group
	fi
    fi
}

IS_ON=/etc/chkconfig

SAMBAD=/usr/samba/bin/smbd
#SAMBA_OPTS=-d2
NMBD=/usr/samba/bin/nmbd
#NMBD_OPTS=-d1

if test ! -x $IS_ON ; then
    IS_ON=true
fi

if $IS_ON verbose ; then
    ECHO=echo
else		# For a quiet startup and shutdown
    ECHO=:
fi

case $1 in
'profile')
	if $IS_ON samba && test -x $SAMBAD; then
	    stop_samba;
	    $ECHO "Samba:\c"
	    $SAMBAD.profile $SAMBA_OPTS; $ECHO " smbd.profile\c"
	    $NMBD.profile $NMBD_OPTS; $ECHO " nmbd.profile\c"
	    $ECHO "."
	fi
	;;
'start')
	if $IS_ON samba && test -x $SAMBAD; then
	    stop_samba;
	    $ECHO "Samba:\c"
	    $SAMBAD $SAMBA_OPTS; $ECHO " smbd\c"
	    $NMBD $NMBD_OPTS; $ECHO " nmbd\c"
	    $ECHO "."
	fi
	;;
'stop')
	$ECHO "Stopping Samba Servers."
	stop_samba;
	exit 0
	;;
*)
	echo "usage: /etc/init.d/samba {start|stop|profile}"
	;;
esac
