#!/bin/bash
# Copyright (c) 2010 Holger Hetterich
# Debianized by Eida.cz 2015
#
# Based on the smb script by Lars Müller <lmuelle@suse.de>
#
# /etc/init.d/smbtad
#   and its symbolic link
# /usr/sbin/rcsmbtad
#
#	This program is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

#!/bin/bash
# /etc/init.d/smbtad
# version 2015-02-05

### BEGIN INIT INFO
# Provides:   smbtad
# Required-Start: $network $syslog
# Required-Stop:  $network $syslog
# Should-Start:   
# Should-Stop:    
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Short-Description:    SMB Traffic Analyzer Daemon
# Description:   SMB Traffic Analyzer Daemon
### END INIT INFO

# config
SMBTAD_BIN="$(which smbtad)"
SMBTAD_CONF="/etc/smbtad.conf"

# Check for missing binary
if [ ! -x ${SMBTAD_BIN} ]; then
	echo -n >&2 "SMB Traffic Analyzer daemon, ${SMBTAD_BIN} is not installed. "
	exit 5
fi

# be extra carefull cause connection fail if TMPDIR is not writeable
export TMPDIR="/var/tmp"



#Start-Stop 
case "$1" in
  start)
    echo -n "Starting SMB Traffic Analyzer daemon "
    
	if [ ! -f ${SMBTAD_CONF} ]; then
		echo -n >&2 "smbtad configuration file, ${SMBTAD_CONF} does not exist. "
		exit 6
	fi
	
	if [ "$(pidof process_name)" ] 
	then
  		echo -n "SMB Traffic Analyzer daemon is already running."
	else
  		$SMBTAD_BIN -c $SMBTAD_CONF
	fi
	exit 1
		
    ;;
  stop)
    
    if [ "$(pidof smbtad)" ] 
	then
  		killall smbtad
	else
  		echo -n "SMB Traffic Analyzer daemon is not running"
	fi

    exit 1
    ;;
  restart)
  	echo -n "Restarting SMB Traffic Analyzer daemon "
    killall smbtad
    $SMBTAD_BIN -c $SMBTAD_CONF
    ;;
  backup)
    mc_backup
    ;;
  status)
    if [ "$(pidof smbtad)" ] 
	then
  		echo -n "SMB Traffic Analyzer daemon is running"
	else
  		echo -n "SMB Traffic Analyzer daemon is not running"
	fi
    ;;

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

exit 0
