#!/bin/sh
#
# $Id$
#
# Sample init script for Debian GNU/Linux
#
#  Copyright (c) 2002 Georg C. F. Greve <greve@gnu.org>,
#   all rights maintained by FSF Europe e.V., 
#   Villa Vogelsang, Antonienallee 1, 45279 Essen, Germany
#
#   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
#
#   Contact:
#            Michael Brown <michaelb@opentext.com>

# This init script was modified on Thu, 30 Jan 2003 02:06:04 -0500 by
# Don Armstrong <don@donarmstrong.com> from contrib/spamass-milter to
# allow force-reload and options specified in
# /etc/default/spamass-milter necessary for inclusion in debian.

# It has been modified additionally to support LSB Boot options and
# status on Friday, July 6, 2007 14:02:44 PDT

# Functionality has been abstracted out to enable using a single
# codebase to call the systemd and init calls.


### BEGIN INIT INFO
# Provides:          spamass-milter
# Required-Start:    $syslog $local_fs $remote_fs
# Required-Stop:     $syslog $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: milter for spamassassin
# Description:       Calls spamassassin to allow filtering out
#                    spam from ham in libmilter compatible MTAs.
### END INIT INFO

. /usr/lib/spamass-milter/default_variables.sh

. /lib/lsb/init-functions

PATH=/sbin:/bin:/usr/sbin:/usr/bin
NAME=spamass-milter
DESC="Sendmail milter plugin for SpamAssassin"

RUNAS="spamass-milter"
CHUID=""

test -x $DAEMON || exit 0

if [ -n "$RUNAS" ]; then
    CHUID="--chuid $RUNAS";
fi;

set -e

start() {
    if [ -e $PIDFILE ] && kill -0 $(cat $PIDFILE); then
        echo "$NAME is already running";
        exit 1;
    fi;
    /usr/lib/spamass-milter/spamass-milter_execpre.sh
    if [ -n "$RUNAS" ] && [ -d $(dirname $PIDFILE) ] &&
	[ "$(stat -c '%U' $(dirname $PIDFILE))" != "$RUNAS" ]; then
	echo "WARNING: $NAME will run as user $RUNAS but $(dirname $PIDFILE) is not owned by $RUNAS";
	echo "Either delete this directory or chown it appropriately. Startup attempts may fail.";
    fi;
    if [ -n "$RUNAS" ] && [ $(dirname $SOCKET) != "." ] &&
	[ -d $(dirname $SOCKET) ] &&
	[ "$(stat -c '%U' $(dirname $SOCKET))" != "$RUNAS" ]; then
	echo "WARNING: $NAME will run as user $RUNAS but $(dirname $SOCKET) is not owned by $RUNAS";
	echo "Either delete this directory or chown it appropriately. Startup attempts may fail.";
    fi;
    if [ $(dirname $SOCKET) != "." ]; then 
	/bin/rm -f $SOCKET
    fi;
    start-stop-daemon --start -p $PIDFILE $CHUID --exec /usr/lib/spamass-milter/spamass-milter_wrapper
}

stop(){
    start-stop-daemon --oknodo --stop -p $PIDFILE --signal 3 --exec $DAEMON
    /bin/sleep 5
    if [ $(dirname $SOCKET) != "." ]; then 
	    /bin/rm -f $SOCKET
    fi;
    /bin/rm -f $PIDFILE
}

status(){
    if [ -e $PIDFILE ]; then 
	    if kill -0 $(cat $PIDFILE); then
	        echo "${NAME} running";
	        exit 0;
	    else
	        echo "${NAME} dead but $PIDFILE exists";
	        exit 1;
	    fi;
	fi;
    echo "${NAME} not running";
	exit 3;
}

case "$1" in
  start)
	echo -n "Starting $DESC: "
	start
	echo "${NAME}"
	;;
  stop)
	echo -n "Stopping $DESC: "
	stop
	echo "${NAME}"
	;;
  force-reload | restart)
	echo -n "Restarting $DESC: "

	stop
	start
	echo "${NAME}"
	;;
  status)
	status
	;;
  *)
	N=$0
	echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
	exit 1
	;;
esac

exit 0
