#!/bin/sh
#
# sympa			Mailing Lists Management System
#
# Written by Michel Bouissou  20/07/2000
#
# Modified by Olivier Salaun 27/07/2000
#    - translations
#    - lang parameter deleted (defined in sympa.conf)
#    - introduced --VARS-- parsed by Makefile
#    - no more sympauser since sympa sets its UID
# Modified by Michel Bouissou  27/07/2000
#
# chkconfig: 345 95 05
# description: sympa is a powerfull mailing lists management system.

# OSTYPE is not defined on Solaris
if [ ! "${OSTYPE}" ]; then
    OSTYPE=`uname -s`
fi

# Sympa parameters
# Sympa binaries directory
sympadir="/usr/local/sbin"

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

  echo_opt="-n"

# End of parameters

# Current state of the module
sympa_status() {
    # Test syntax.
    if [ $# = 0 ] ; then
        echo "Usage: sympa_status {program}"
        return 1
    fi
 
       # First try "/u1/home/sympa/*.pid" files
       if [ -f /var/run/sympa/$1.pid ] ; then
             pid=`head -1 /var/run/sympa/$1.pid | tr -s ' ' '|'`
             if [ "$pid" != "" ] ; then
                 running=`pgrep -f $1.pl`
                 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
}

# Start a module
sympa_module_start() {
    if [ $# = 0 ] ; then
        echo "Usage: sympa_module_start {program}"
        return 1
    fi

	$sympadir/$1.pl && echo "success" || echo "failure"
}

# Test state of module before startup
sympa_start() {
    if [ $# = 0 ] ; then
        echo "Usage: sympa_start {program}"
        return 1
    fi
 
	sympa_status $1 > /dev/null
	case "$?" in
		3)
			echo $echo_opt "Starting module $1.pl: "
			sympa_module_start $1
			;;
		1) 
			echo $echo_opt "Starting $1, overwriting old pid file."
			sympa_module_start $1
			;;
		0)
			echo "$1 seems active. No action will be taken."
			echo "Try \"sympa status\" or \"sympa restart"\".
			;;
	esac
}

# Stop a module
sympa_stop() {
    if [ $# = 0 ] ; then
        echo "Usage: sympa_stop {program}"
        return 1
    fi
 
    if [ -f /var/run/sympa/$1.pid ]; then
	echo $echo_opt "Stopping module $1.pl: "
	pids=`head -1 /var/run/sympa/$1.pid`
	runcount=0
	if [ "$pids" != "" ]; then
	    for pid in $pids; do
		killcount=0
		running=`pgrep -f $1.pl`
		while [ "$running" != "" ]; do
		    if [ $killcount -gt 10 ]; then
			    echo 'failure'
			return 3
		    fi

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


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

# See how we were called.
case "$1" in
  start)
	if [ ! -f /var/spool/lock/sympa ]; then
		echo "Starting Sympa subsystem: "
		sympa_start sympa
		sympa_start bulk
		sympa_start archived
		sympa_start bounced
		sympa_start task_manager
		touch /var/spool/lock/sympa
	else

		echo "Sympa seems active. No action will be taken."
		echo "Try \"sympa status\" or \"sympa restart"\".

	fi
	;;
  stop)
	echo "Stopping Sympa subsystem: "
	sympa_stop bounced
	sympa_stop archived
	sympa_stop bulk
	sympa_stop sympa
	#if [ -f /var/run/sympa/sympa-distribute.pid ]; then
	#	sympa_stop sympa-distribute
	#fi
	if [ -f /var/run/sympa/sympa-creation.pid ]; then
		sympa_stop sympa-creation
	fi
	sympa_stop task_manager
	if [ -f /var/spool/lock/sympa ]; then
		rm -f /var/spool/lock/sympa
	fi
	;;
  status)
	echo "Status of Sympa subsystem: "
	if [ -f /var/spool/lock/sympa ]; then
		echo "Status file for subsystem found."
	else
		echo "Status file for subsystem NOT found."
		exit 1
	fi
	sympa_status sympa
	[ $? = 0 ] || exit 1
	sympa_status bulk
	[ $? = 0 ] || exit 1
	sympa_status archived
	[ $? = 0 ] || exit 1
	sympa_status bounced
	[ $? = 0 ] || exit 1
	sympa_status task_manager
	[ $? = 0 ] || exit 1
	;;
  restart)
	echo "Restarting Sympa subsystem: "
	$0 stop && $0 start
	;;
  *)
	echo "Usage: $0 {start|stop|status|restart}"
	exit 1
	;;
esac

exit 0

