#!/bin/sh
# GenVer = 10.0.0.175
#
# Copyright (C) 2003 Scalix Corporation.  All rights reserved.
# (c) Copyright Hewlett-Packard Company 1989-1999.  All Rights Reserved.
# @(#) $Header: /cvsroot/unsupported/suse_ha.sh,v 1.1 2006/02/10 22:30:32 kbrake Exp $
#
# @(#) 'rc' script to startup / shutdown Scalix. 
#
# NOTE:    This script is not configurable!  Any changes made to this
#          script will be overwritten when you upgrade to the next
#          release of Scalix.
#
### BEGIN INIT INFO
# Provides:       scalix
# Required-Start: $network $remote_fs $time sendmail
# X-UnitedLinux-Should-Start: ypbind httpd
# Required-Stop:
# Default-Start:  3 5
# Default-Stop:
# Description:    Start Scalix Server
### END INIT INFO
#

# Source function library.
. /etc/rc.status

test -s /etc/sysconfig/scalix && \
    . /etc/sysconfig/scalix

rc_reset

PATH=/opt/scalix/bin:/bin:/sbin:/usr/sbin:/usr/bin
export PATH

is_running() {
	pid="$(omsetsvc -p cam)"
	test -n "$pid" && kill -0 $pid 2>/dev/null
}

#
# Handle instance name argument
#
instance=$2

case "$1" in
    start)

        if [ "$START_SCALIX" != "1" ]
        then
            # Just fail quietly
            exit 0
        fi

        echo -n "Initializing Scalix Server"

        if [ ! -x /opt/scalix/bin/omrc ]
        then
            echo -e "\nStartup script /opt/scalix/bin/omrc not executable"
            rc_failed 1
            rc_status -v2
            rc_exit
        fi

        # check kernel parameters
        if test -n "$KERNEL_MIN_file_max" -a \
            "$KERNEL_MIN_file_max" -gt $(cat /proc/sys/fs/file-max)
        then
            echo $KERNEL_MIN_file_max >/proc/sys/fs/file-max
        fi

	if [ -n "$instance" ]
	then
		# Start specified instance if it is there
		export OMCURRENT=$instance
		if test -f $(omrealpath '~/sys/sysdata') && ! is_running
		then
			echo
			echo -n "Starting Scalix services ($OMCURRENT):"
			if omrc $SCALIX_OMRC_ARGS >/dev/null 2>&1
			then
				rc_status -v
			else
				rc_failed 1
				rc_status -v1
				rc_exit
			fi
		fi
	else
	    for name in $(omcheckgc -l); do
		export OMCURRENT=$name
		if test -f $(omrealpath '~/sys/sysdata') && omcheckgc -a && ! is_running
		then
		    echo -n "Starting Scalix services ($OMCURRENT):"
		    if omrc $SCALIX_OMRC_ARGS >/dev/null 2>&1
		    then
			rc_status -v
		    else
			rc_failed 1
			rc_status -v1
			rc_exit
		    fi
		    rc_status -v
		fi
	    done
	fi
	echo
	touch /var/lock/subsys/scalix
        ;;

    stop)

        echo -n "Shutting down Scalix Server"
        if [ ! -x /opt/scalix/bin/omshut ]
        then
            rc_failed 5
            rc_status -v
            rc_exit
        fi

	if [ -n "$instance" ]
	then
		export OMCURRENT=$instance
		if test -f $(omrealpath '~/sys/sysdata') && is_running
		then
			echo
			echo -n "Stopping Scalix services ($OMCURRENT):"
			if omshut >/dev/null 2>&1
			then
				rc_status -v
			else
				rc_failed 1
				rc_status -v
				rc_exit
			fi
		fi
	else
            for name in $(omcheckgc -l); do
                export OMCURRENT=$name
                if test -f $(omrealpath '~/sys/sysdata')
                then            
		    echo
                    echo -n "Stopping Scalix services ($OMCURRENT):"
                    if omshut >/dev/null 2>&1
                    then
                        rc_status -v
                    else
                        rc_failed 1
                        rc_status -v
                        rc_exit
                    fi
                fi
            done
        fi
	echo
        rm -f /var/lock/subsys/scalix
        ;;

    status)
	if [ -n "$instance" ]
	then
		export OMCURRENT=$instance
		echo $OMCURRENT daemons
		omstat -a
		echo
		echo $OMCURRENT services
		omstat -s
		echo
	else
            for instance in $(omcheckgc -l)
            do
                export OMCURRENT=$instance
                echo $OMCURRENT daemons
                omstat -a
                echo 
                echo $OMCURRENT services
                omstat -s
                echo 
            done
        fi
        ;;

    restart | reload)
        $0 stop $instance; $0 start $instance;
        ;;

    *)
        echo "Usage: scalix {start|stop|status|reload|restart} {instance}"
        exit 1
        ;;
esac

