#!/bin/sh
#
# Startup script for the Cloudmark CMAE server
#
# processname: cmae_server
#
# If you want to relocate this start/stop script, you'll need to
# set CMAE_HOME to the absolute path of the directory that contains 
# bin/cmae_server.
#
CMAE_HOME="/etc/spamassassin/cmae/cloudmark/"

if [ "x$CMAE_HOME" = "x" ]; then
    #
    # If the cmaed script has not be relocated, then automatically
    # determine CMAE_HOME.
    #
    rel_dir=`dirname $0`
    CMAE_HOME=`(cd ${rel_dir}/..; pwd)`
fi

if [ ! -x $CMAE_HOME/bin/cmae_server ]; then
    echo 
    echo "Error: The variable CMAE_HOME is not properly set.  Make sure"
    echo "       it is set to the directory that contains bin/cmae_server"
    echo
    exit 1
fi

prog=cmae_server
cmae_server=$CMAE_HOME/$prog
config=$CMAE_HOME/etc/cmae_client_server.cfg
exit_wait=15

PATH=/bin:/usr/bin

#
# Functions to handle process names and PIDs
#

# show me if the given pid is actively running 
# PRINTS: the pid if it is running, empty otherwise
listpid() {
    to_return=
    for pid in $*; do
        kill -0 $pid > /dev/null 2>&1
        if [ "$?" = "0" ]; then
            if [ "x$to_return" = "x" ]; then
                to_return=$pid
            else 
                to_return="$to_return $pid"
            fi
        fi
    done
    echo $to_return
}

# List all the PIDs that match the name of the process
# PRINTS: list of matching pids, empty otherwise
listproc() {
    pgrep "^$1\$"
}

killproc() {            # kill the named process(es)
    pids=`listproc $1`

    if [ "x$pids" != "x" ]; then
        for pid in $pids
        do 
            p=`listpid $pid`
            if [ "x$p" != "x" ]; then
                echo "Stopping $prog, PID: $pid"
                kill -TERM $pid > /dev/null 2>&1
                x=0
                while [ $x -lt $exit_wait ]
                do
                    x=`expr $x + 1`

                    # check if process has gone away
                    p=`listpid $pid`
                    if [ "x$p" = "x" ]; then break; fi

                    sleep 1
                done

                p=`listpid $pid`
                # check if process has gone away
                if [ "x$p" != "x" ]; then kill -KILL $pid; echo " .... killed forcefully"; fi
            fi
        done
    fi
}


stop() {
    killproc cmae_server
}


start() {
    pids=`listproc $prog`
    if [ "x$pids" != "x" ]; then
        echo "It appears that $prog is already running with PID: $pids"
        echo "Please verify and try again"
        exit 1
    fi 

    if [ ! -d $CMAE_HOME ]; then
        echo "CMAE_HOME=$CMAE_HOME does not exist"
        exit 1
    fi
    cd $CMAE_HOME
    if [ "x$?" != "x0" ]; then
        echo "Couldn't cd into CMAE_HOME=$CMAE_HOME"
        exit 1
    fi
    LD_LIBRARY_PATH=$CMAE_HOME:$CMAE_HOME/lib:$CMAE_HOME/syslib ./bin/$prog -c $config
    if [ "x$?" != "x0" ]; then
        echo "Couldn't start $prog, please consult log file"
        exit 1
    fi
    sleep 1
    pids=`listproc $prog`
    if [ "x$pids" = "x" ]; then
        # let's make sure the process stayed up for at least one second
        echo "Couldn't start $prog, please consult log file"
        exit 1
    fi
    echo "Started $prog, PID: " `echo "$pids" | head -1`
}


#
# What do we need to do?
#

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	stop
	start
	;;
  *)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
esac

exit 0
