### BEGIN INIT INFO
# Provides:          samba
# Required-Start:    $network $local_fs $remote_fs
# Required-Stop:     $network $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Should-Start:      slapd
# Should-Stop:       slapd
# Short-Description: start Samba daemons (nmbd and smbd)
### END INIT INFO

# Description of this script:
#                    
# This script comes initially from a Debian Squeeze machine on
# which samba 3.x was installed with "apt-get install samba". The script
# was modified/adjusted so it points to the correct paths of a default
# samba4 installation (/usr/local/samba).
#
# Installation instructions:
# (1) copy the content of this script into your clipboard or download it
# (2) save the content into /etc/init.d/samba of your samba4 host.
# (3) execute "chmod +x /etc/init.d/samba" to have the script executable
# (4) execute "update-rc.d samba defaults" to install auto-start function.
#     smbd+nmbd will automatically being started after earch system start/reboot
# 
# Modified by local@#samba~irc.freenode.net at 06th March 2013
# The script was successfully tested on Debian GNU/Linux Squeeze+Wheezy
#
#
#

NMBD=/usr/sbin/nmbd
SMBD=/usr/sbin/smbd

PID_DIR=/var/run
PID_NMBD=$PID_DIR/nmbd.pid
PID_SMBD=$PID_DIR/smbd.pid

. /lib/lsb/init-functions

#create needed directory
mkdir -p /var/run/samba/ncalrpc

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

# See if the daemons are there
test -x $NMBD -a -x $SMBD || exit 0

case "$1" in
  start)
    log_info_msg "Starting samba nmbd daemon..."
    start_daemon $NMBD 
    evaluate_retval
    log_info_msg "Starting samba smdb daemon..."
    start_daemon $SMBD 
    evaluate_retval
    ;;

  stop)
    log_info_msg "Stopping samba smdb daemon..."
    killproc -p "$PID_SMBD " $SMBD
    evaluate_retval
    log_info_msg "Stopping samba nmdb daemon..."
    killproc -p "$PID_NMBD " $NMBD
    evaluate_retval
    ;;

  reload)
    log_info_msg "Reloading smbd..."
    killproc -p "$PID_SMBD " $SMBD -HUP
    evaluate_retval
    log_info_msg "Reloading nmbd..."
    killproc -p "$PID_NMBD " $NMBD -HUP
    evaluate_retval
    ;;

  restart)
    $0 stop
    sleep 1
    $0 start
    ;;

  condrestart)
    pid=`cat "$PID_SMBD" 2>/dev/null`
    if [ -n "${pid}"  -a -d /proc/${pid} ]; then
      $0 restart
      fi
    ;;

  status)
    statusproc $NMBD
    statusproc $SMBD
    ;;

  *)
    echo "Usage: $0 {start|condrestart|stop|reload|restart|status}"
    exit 1
    ;;

esac
