#! /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  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.

if [ ${OSTYPE} != "FreeBSD" -a ${OSTYPE} != "solaris" ]; then
    # Source function library.
    . /etc/rc.d/init.d/functions

    # Get config.
    . /etc/sysconfig/network
fi

# Sympa parameters
# Sympa binaries directory
sympadir="/usr/lib/sympa/bin"

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

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

# End of parameters

# Current state of the module
sympa_status() {
    # Test syntax.
    if [ $# = 0 ] ; then
        gprintf "Usage: sympa_status {program}\n"
        return 1
    fi
 
    if [ ${OSTYPE} != "FreeBSD" -a ${OSTYPE} != "solaris" ]; then
      status $1.pl

    else
       # First try "/u1/home/sympa/*.pid" files
       if [ -f /var/run/sympa/$1.pid ] ; then
             pid=`head -1 /var/run/sympa/$1.pid`
             if [ "$pid" != "" ] ; then
                 running=`ps -A | grep "$pid"`
                 if [ "$running" != "" ]; then
                     gprintf "%s (pid %s) is active...\n" "$1" "$pid"
                     return 0
                 else
                     gprintf "%s died, pid file remains.\n" "$1"
                     return 1
                 fi
             fi
        fi
        gprintf "%s is stopped.\n" "$1"
        return 3
    fi
}

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

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

# Test state of module before startup
sympa_start() {
    if [ $# = 0 ] ; then
        gprintf "Usage: sympa_start {program}\n"
        return 1
    fi
 
	sympa_status $1 > /dev/null
	case "$?" in
		3)
			gprintf "Starting module %s.pl: \n" "$1"
			sympa_module_start $1
			if [ ${OSTYPE} = "solaris" ]; then
				if [ $? -eq 0 ]; then
        	                       	gprintf "%s.pl started: success\n" "$1"
	                       	else
                	               	gprintf "%s.pl not started: failure\n" "$1"
                       		fi
			fi
			;;
		1) 
			gprintf "Starting %s, overwritting old pid file.\n" "$1"
			sympa_module_start $1
			if [ ${OSTYPE} = "solaris" ]; then
				if [ $? -eq 0 ]; then
        	                       	gprintf "%s.pl started: success\n" "$1"
	                       	else
                	               	gprintf "%s.pl not started: failure\n" "$1"
                       		fi
			fi
			;;
		0)
			gprintf "%s seems active. No action will be taken.\n" "$1"
			gprintf "Try sympa status or sympa restart.\n"
			;;
	esac
}

# Stop a module
sympa_stop() {
    if [ $# = 0 ] ; then
        gprintf "Usage: sympa_stop {program}\n"
        return 1
    fi
 
	if [ -f /var/run/sympa/$1.pid ]; then
		gprintf "Stopping module %s.pl: \n" "$1"
		pid=`head -1 /var/run/sympa/$1.pid`
		running=`ps -A | grep "$pid"`
		if [ "$running" != "" ]; then
		    if [ ${OSTYPE} = "IRIX" ]; then
			kill -TERM $pid && gprintf "success\n" || gprintf "failure\n"
		    elif [ ${OSTYPE} = "solaris" ]; then
			gprintf "Stopping module %s.pl: terminated\n" "$1"
			kill -TERM $pid
		    else
			kill -TERM $pid && success || failure
		    fi		    
		else
		    gprintf "died\n"
		fi
		echo
	else
	        gprintf "Module %s.pl not running\n" "$1"
	fi
}


# Check that networking is up.
if [ ${OSTYPE} != "FreeBSD" -a ${OSTYPE} != "solaris" ]; then
    if [ ${NETWORKING} = "no" ]
    then
	    exit 0
    fi
fi

# 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/lock/subsys/sympa ]; then
		gprintf "Starting Sympa subsystem: \n"
		sympa_start sympa
		sympa_start archived
		sympa_start bounced
		sympa_start task_manager
		touch /var/lock/subsys/sympa
		echo
	else

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

	fi
	;;
  stop)
	gprintf "Stopping Sympa subsystem: \n"
	sympa_stop bounced
	sympa_stop archived
	sympa_stop sympa
	sympa_stop task_manager
	if [ -f /var/lock/subsys/sympa ]; then
		rm -f /var/lock/subsys/sympa
	fi
	;;
  status)
	gprintf "Status of Sympa subsystem: \n"
	if [ -f /var/lock/subsys/sympa ]; then
		gprintf "Status file for subsystem found.\n"
	else
		gprintf "Status file for subsystem NOT found.\n"
	fi
	sympa_status sympa
	sympa_status archived
	sympa_status bounced
	sympa_status task_manager
	;;
  restart)
	gprintf "Restarting Sympa subsystem: \n"
	$0 stop
	$0 start
	echo
	;;
  *)
	gprintf "Usage: %s {start|stop|status|restart}\n" "$0"
	exit 1
	;;
esac

exit 0




