#!/bin/sh
#
# ms-check
# 
# checks to see if the mailscanner process is running. starts the process
# unless it has been manually stopped.
#
# author: 	Jerry Benton <mailscanner@mailborder.com>
#			28 APR 2016
#
#   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 2 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, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#      https://www.mailscanner.info
#

PATH=$PATH:/usr/sbin:/usr/bin:/bin:/sbin
export PATH
NAME=MailScanner
DAEMON=/usr/sbin/MailScanner
QUICKPEEK=/usr/sbin/ms-peek
run_mailscanner=0
ramdisk_sync=0
ms_conf=/etc/MailScanner/MailScanner.conf
ms_core=/usr/share/MailScanner
ms_lib=/usr/lib/MailScanner
ramdisk_store=/var/spool/MailScanner/ramdisk_store
stopped_lockfile=/var/lock/subsys/MailScanner.off
HOSTNAME=$(hostname)
export HOSTNAME

# enable logging of non-critical notices to the maillog: yes/no
VERBOSE=yes

# basic config file
if [ -f /etc/MailScanner/defaults ] ; then
	. /etc/MailScanner/defaults
else
	echo "Aborted: missing configuration file /etc/MailScanner/defaults"
    exit 1
fi

PIDFILE=`${QUICKPEEK} PIDFile ${ms_conf}`

if [ -z $PIDFILE ]; then
	PIDFILE=/var/run/MailScanner.pid
fi

# Exit if the MailScanner executable is not installed
[ -x $DAEMON ] || exit 0

# if /var/lock/subsys is missing
[ -d /var/lock/subsys ] || mkdir -p /var/lock/subsys

# Don't start if MailScanner is not configured
if [ $run_mailscanner = 0 ]; then
	exit 0
fi

# check if a PID file exists
if [ -f $PIDFILE ] ; then
	# get the PID
	PID=$(head -n 1 $PIDFILE)
	# check to see if running and belongs to mailscanner
	ps wwp $PID|grep -iq '[M]ailScanner' > /dev/null 2>&1

	# get the return
	RETVAL="$?"

else
	RETVAL=9
fi

# if 0 it is already running
if [ $RETVAL -eq 0 ]; then
	# already running
	exit 0
else

	# if the stopped lock file is present exit
	if [ -f $stopped_lockfile ] ; then
		[ "$VERBOSE" != no ] && logger -i -p mail.notice "ms-check: not starting - lock file found"
		exit 0
	fi
	
	# remove the PID file if preset
	if [ -f $PIDFILE ] ; then
		rm -f $PIDFILE
	fi
	
	# kill any rogue processes
	kill -15 $(ps axww | grep '[M]ailScanner' | awk '{print $1}') > /dev/null 2>&1
	# wait until they're gone.
	while (ps axww | grep -q '[M]ailScanner'); do
	    sleep 1
	done

	# log the start
	[ "$VERBOSE" != no ] && logger -i -p mail.notice "ms-check: starting mailscanner"
	
	# start mailscanner
	$DAEMON $ms_conf
	
	# set run file
	if [ ! -f /var/lock/subsys/MailScanner ] ; then
		touch /var/lock/subsys/MailScanner
	fi
	
	exit 0
	
fi
