#!/bin/sh
#
# sympasoap                     Sympa SOAP Interface
#
# Written by IKEDA Soji  2011-10-20
# Deployed by alternc-sympa package
#
# chkconfig: 345 95 05
# description: sympa is a powerful mailing lists management system.

### BEGIN INIT INFO
# Provides:          sympasoap
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Should-Start:      mysql postgresql
# Should-Stop:       mysql postgresql
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: start and stop the SYMPA SOAP interface
# Description:       API for the SYMPA mailing list manager
### END INIT INFO

if [ -f /etc/rc.d/init.d/functions ]; then
    # Source function library.
    . /etc/rc.d/init.d/functions

    ## Set a flag
    use_functions=1
fi

# SympaSOAP parameters
# SympaSOAP binaries directory
sympafcgidir="/usr/lib/cgi-bin/sympa"

# Sympa config files directory
sympaconf="/etc/sympa/sympa/sympa.conf"

FCGI_CHILDREN=5
FCGI_USER=sympa
FCGI_GROUP=sympa
FCGI_PID_DIR=/var/run/sympa
FCGI_OPTS='-s /var/run/sympa/sympasoap.socket -M 0600 -U www-data'
if [ -e /etc/sysconfig/sympa ]; then
    . /etc/sysconfig/sympa
fi

# Current state of SympaSOAP
sympasoap_status() {
    if [ ${use_functions} ]; then
        status sympasoap
    else
        if [ -f ${FCGI_PID_DIR}/sympasoap.pid ]; then
            pid=`cat ${FCGI_PID_DIR}/sympasoap.pid | xargs | tr ' ' '|'`
            if [ "$pid" != "" ]; then
                running=`ps -A | egrep "$pid"`
                if [ "$running" != "" ]; then
                    echo "$1 (pid(s) $pid) is active..."
                    return 0
                else
                    echo "$1 died, pid file remains."
                    return 1
                fi
            fi
        fi
        echo "$1 is stopped."
        return 3
    fi
}

# Start SympaSOAP
sympasoap_start() {
    sympasoap_status > /dev/null
    rc=$?
    case "$rc" in
    3)
        echo -n "Starting sympasoap: "
        ;;
    1)
        echo -n "Starting sympasoap, overwriting old pid file."
        ;;
    0)
        echo "SympaSOAP seems active. No action will be taken."
        echo "Try \"sympasoap status\" or \"sympasoap restart"\".
        return
    esac

    /usr/bin/spawn-fcgi -F ${FCGI_CHILDREN} -P ${FCGI_PID_DIR}/sympasoap.pid \
    -u ${FCGI_USER} -g ${FCGI_GROUP} ${FCGI_OPTS} -- \
    ${sympafcgidir}/sympa_soap_server.fcgi
}

# Stop SympaSOAP
sympasoap_stop() {
    if [ -f ${FCGI_PID_DIR}/sympasoap.pid ]; then
        runcount=0
        pids=`cat ${FCGI_PID_DIR}/sympasoap.pid`
        if [ "$pids" != "" ]; then
            for pid in "$pids"; do
                killcount=0
                running=`ps -A | grep "$pid ..* sympasoap"`
                while [ "$running" != "" ]; do
                    if [ $killcount -gt 10 ]; then
                        if [ ${use_functions} ]; then
                            failure
                        else
                            echo 'failure'
                        fi
                        return 3
                    fi

                    kill -TERM $pid >/dev/null 2>&1
                    running=`ps -A | grep "$pid ..* $1\\.pl"`
                    if [ "$running" = "" ]; then
                        runcount=`expr $runcount + 1`
                        break
                    fi
                    sleep 2
                    running=`ps -A | grep "$pid ..* $1\\.pl"`
                    if [ "$running" = "" ]; then
                        runcount=`expr $runcount + 1`
                        break
                    fi
                    killcount=`expr $killcount + 1`
                done
            done
        fi
        if [ $runcount -gt 0 ]; then
            if [ ${use_functions} ]; then
                success
            else
                echo 'success'
            fi
        else
            echo 'died'
        fi
        echo
    else
        echo "Module $1.pl not running"
    fi
    return 0
}

# Check config files
[ -d $sympafcgidir ] || exit 0
[ -f $sympaconf ] || exit 0

# See how we were called.
case "$1" in
start)
    if [ ! -f /run/lock/sympasoap.lock ]; then
        echo "Starting SympaSOAP: "
        sympasoap_start
        touch /run/lock/sympasoap.lock
        echo
    else
        echo "SympaSOAP seems active. No action will be taken."
        echo "Try \"sympasoap status\" or \"sympasoap restart"\".
    fi
    ;;
stop)
    echo "Stopping SympaSOAP: "
    sympasoap_stop
    if [ -f /run/lock/sympasoap.lock ]; then
        rm -f /run/lock/sympasoap.lock
    fi
    ;;
status)
    echo "Status of SympaSOAP: "
    sympasoap_status
    ;;
restart)
    echo "Restarting SympaSOAP: "
    $0 stop && $0 start
    echo
    ;;
force-reload)
    echo "Restarting SympaSOAP: "
    $0 stop && $0 start
    echo
    ;;
*)
    echo "Usage: $0 {start|stop|status|restart}"
    exit 1
    ;;
esac

exit 0
