#!/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 powerful mailing list management system.

if [ -f /usr/local/sbin/functions ]; then
    # Source function library.
    . /usr/local/sbin/functions
    
    ## Set a flag 
    use_functions=1
fi

# Sympa parameters

# Sympa binaries directory
sympadir="/usr/local/sbin"
# Sympa config files directory
sympaconf="/var/sympa/etc/sympa.conf"

sympapiddir="/var/run/sympa"
sympalockdir="/var/spool/lock"



if [ -f /etc/sysconfig/network ]; then
    # Get config.
    . /etc/sysconfig/network
fi

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

# OSTYPE *is* defined on Solaris 10! (bug #3149)
case "$OSTYPE" in
        *solaris*)
        OSTYPE=SunOS
        ;;
esac

if [ -f /etc/SuSE-release ] ; then
    OSTYPE='Suse'
fi

if [ -f /etc/debian_version ] ; then
    OSTYPE='Debian'
fi

if [ -f /etc/slackware-version ] ; then
    OSTYPE='Slack'
fi


##'echo -n' not supported with SH on Solaris
if [ ${OSTYPE} = "SunOS" ]; then
  echo_opt=""
else
  echo_opt="-n"
fi

# End of parameters

# Current state of the module
sympa_status() {
    # Test syntax.
    if [ $# = 0 ] ; then
        echo "Usage: sympa_status {program}"
        return 1
    fi
 
    if [ ${use_functions} ]; then

      status $1.pl

    else
       # First try "/u1/home/sympa/*.pid" files
       if [ -f ${sympapiddir}/$1.pid ] ; then
             pid=`head -1 ${sympapiddir}/$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
    fi
}

# Health check
sympa_check() {
	echo $echo_opt "Checking environment: "
	if [ ${use_functions} ]; then
		$sympadir/sympa.pl --health_check && success || failure
		return $?
	else
		if $sympadir/sympa.pl --health_check; then
			echo "success"
			return 0
		else
			echo "failure"
			return 1
		fi
	fi
        echo
}

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

#	if [ $1 = "sympa" -a $lang != "" ]; then
#		startparam="-l $lang"
#	else
#		startparam=""
#	fi
 
	if [ ${use_functions} ]; then
		$sympadir/$1.pl $startparam && success || failure
	else
		$sympadir/$1.pl $startparam && echo "success" || echo "failure"
	fi
	echo
}

# 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 [ $1 = 'sympa-creation' ]; then
		process_name='sympa';
	else
		process_name=$1;
    fi
    comm=`echo $process_name.pl | cut -c-8`

    if [ -f ${sympapiddir}/$1.pid ]; then
	echo $echo_opt "Stopping module $process_name.pl: "
	pids=`head -1 ${sympapiddir}/$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
			if [ ${use_functions} ]; then
			    failure
			else
			    echo 'failure'
			fi
			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
	    if [ ${use_functions} ]; then
		success
	    else
		echo 'success'
	    fi
	else
	    echo 'died'
	fi
	echo
    else
	echo "Module $process_name.pl not running"
    fi
    return 0
}


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

# See how we were called.
case "$1" in
  start)
	if [ ! -f ${sympalockdir}/sympa ]; then
		echo "Starting Sympa subsystem: "
		sympa_check || exit 1
		sympa_start sympa_msg
		sympa_start bulk
		sympa_start archived
		sympa_start bounced
		sympa_start task_manager
		touch ${sympalockdir}/sympa
		echo
	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_msg
	if [ -f ${sympapiddir}/sympa.pid ]; then
		# For Sympa earlier than 6.2b.4
		sympa_stop sympa
	fi
	if [ -f ${sympapiddir}/sympa-distribute.pid ]; then
		# For Sympa earlier than 6.2b.1
		sympa_stop sympa-distribute
	fi
	if [ -f ${sympapiddir}/sympa-creation.pid ]; then
		# For Sympa earlier than 6.2b.4
		sympa_stop sympa-creation
	fi
	if [ -f ${sympapiddir}/sympa_automatic.pid ]; then
		sympa_stop sympa_automatic
	fi
	sympa_stop task_manager
	if [ -f ${sympalockdir}/sympa ]; then
		rm -f ${sympalockdir}/sympa
	fi
	;;
  status)
	echo "Status of Sympa subsystem: "
	if [ -f ${sympalockdir}/sympa ]; then
		echo "Status file for subsystem found."
	else
		echo "Status file for subsystem NOT found."
	fi
	failed=0
	sympa_status sympa_msg || failed=1
	sympa_status bulk || failed=1
	sympa_status archived || failed=1
	sympa_status bounced || failed=1
	sympa_status task_manager || failed=1
	if [ $failed == 1 ] ; then exit 1 ; fi
	;;
  restart)
	echo "Restarting Sympa subsystem: "
	$0 stop && $0 start
	echo
	;;
  *)
	echo "Usage: $0 {start|stop|status|restart}"
	exit 1
	;;
esac

exit 0

