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

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

if [ ${OSTYPE} != "Slack" -a ${OSTYPE} != "FreeBSD" -a ${OSTYPE} != "SunOS" -a ${OSTYPE} != "darwin7.0" -a ${OSTYPE} != "Debian" -a ${OSTYPE} != "Suse" ]; then
    # Source function library.
    . --INITDIR--/functions

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

# Sympa parameters
# Sympa binaries directory
sympadir="--SBINDIR--"

# Sympa config files directory
sympaconf="--CONFIG--"
wwsympaconf="--WWSCONFIG--"

##'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 [ ${OSTYPE} != "Slack" -a ${OSTYPE} != "FreeBSD" -a ${OSTYPE} != "SunOS" -a ${OSTYPE} != "darwin7.0"  -a ${OSTYPE} != "Debian" -a ${OSTYPE} != "Suse" ]; then

      status $1.pl

    else
       # First try "/u1/home/sympa/*.pid" files
       if [ -f --PIDDIR--/$1.pid ] ; then
             pid=`head -1 --PIDDIR--/$1.pid`
             if [ "$pid" != "" ] ; then
                 running=`ps -A | grep "$pid"`
                 if [ "$running" != "" ]; then
                     echo "$1 (pid $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 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 [ ${OSTYPE} = "Slack" -o ${OSTYPE} = "IRIX" -o ${OSTYPE} = "SunOS" -o ${OSTYPE} = "Debian" -o ${OSTYPE} = "Suse" ]; then
		$sympadir/$1.pl $startparam && echo "success" || echo "failure"
	else
		$sympadir/$1.pl $startparam && success || 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
			if [ ${OSTYPE} = "IRIX" -o ${OSTYPE} = "SunOS" ]; then
				if [ $? -eq 0 ]; then
        	                       	echo "$1.pl started: success"
	                       	else
                	               	echo "$1.pl not started: failure"
                       		fi
			fi
			;;
		1) 
			echo $echo_opt "Starting $1, overwritting old pid file."
			sympa_module_start $1
			if [ ${OSTYPE} = "IRIX" -o ${OSTYPE} = "SunOS" ]; then
				if [ $? -eq 0 ]; then
        	                       	echo "$1.pl started: success"
	                       	else
                	               	echo "$1.pl not started: failure"
                       		fi
			fi
			;;
		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 --PIDDIR--/$1.pid ]; then
		echo $echo_opt "Stopping module $1.pl: "
		pid=`head -1 --PIDDIR--/$1.pid`
		running=`ps -A | grep "$pid"`
		if [ "$running" != "" ]; then
		if [ ${OSTYPE} = "Slack" -o ${OSTYPE} = "IRIX" -o ${OSTYPE} = "SunOS" -o ${OSTYPE} = "Debian" -o ${OSTYPE} = "Suse" ]; then
			kill -TERM $pid && echo "success" || echo "failure"
		    else
			kill -TERM $pid && success || failure
		    fi		    
		else
		    echo "died"
		fi
		echo
	else
	        echo "Module $1.pl not running"
	fi
}


# Check that networking is up.
if [ ${OSTYPE} != "Slack" -a ${OSTYPE} != "FreeBSD" -a ${OSTYPE} != "SunOS" -a ${OSTYPE} != "darwin7.0" -a ${OSTYPE} != "Debian" -a ${OSTYPE} != "Suse" ]; 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 --LOCKDIR--/sympa ]; then
		echo "Starting Sympa subsystem: "
		sympa_start sympa
		sympa_start archived
		sympa_start bounced
		sympa_start task_manager
		touch --LOCKDIR--/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 sympa
	if [ -f --PIDDIR--/sympa-distribute.pid ]; then
		sympa_stop sympa-distribute
	fi
	if [ -f --PIDDIR--/sympa-creation.pid ]; then
		sympa_stop sympa-creation
	fi
	sympa_stop task_manager
	if [ -f --LOCKDIR--/sympa ]; then
		rm -f --LOCKDIR--/sympa
	fi
	;;
  status)
	echo "Status of Sympa subsystem: "
	if [ -f --LOCKDIR--/sympa ]; then
		echo "Status file for subsystem found."
	else
		echo "Status file for subsystem NOT found."
	fi
	sympa_status sympa
	sympa_status archived
	sympa_status bounced
	sympa_status task_manager
	;;
  restart)
	echo "Restarting Sympa subsystem: "
	$0 stop
	sleep 3
	$0 start
	echo
	;;
  *)
	echo "Usage: $0 {start|stop|status|restart}"
	exit 1
	;;
esac

exit 0




