#!/bin/sh
#
# chkconfig: - 99 01
#
# description: This starts eciServiceProgram Daemon
# processname: eciServiceProgram
#
### BEGIN INIT INFO
# Provides: eciServiceProgram
# Required-Start: $ALL
# Should-Start: slpd
# Required-Stop: 
# Default-Start: 3 5
# Default-Stop: 0 1 2 4 6
# Short-Description: This starts eciServiceProgram Daemon
# Description: This starts eciServiceProgram Daemon
### END INIT INFO

if [ -r /etc/rc.d/init.d/functions ]
then
# RedHat/Asianux
    . /etc/rc.d/init.d/functions
    my_log_message()
    {
        ACTION=$1
        shift

        case "$ACTION" in
            success)
                success "$*"
                echo
                ;;
            failure)
                failure "$*"
                echo
                ;;
            *)
                ;;
        esac
    }
    log_success_msg()
    {
        my_log_message success "$*"
    }
    log_failure_msg()
    {
        my_log_message failure "$*"
    }

    kill_slpd()
    {
        if [ -x /sbin/slpd ]
        then
            # ESX4 no operation
            echo -n
        else
            kill `pgrep slpd`
        fi
    }

    check_firewall_setting()
    {
        # no operation
        echo -n
    }
else
# SuSE
    . /lib/lsb/init-functions

    kill_slpd()
    {
        # no operation
        echo -n
    }

    check_firewall_setting()
    {
        LOOP_CNT=0
        CHECK_FLAG=0

        if [ -L /etc/init.d/rc5.d/S*SuSEfirewall2_setup ]
        then
            if [ -e /sbin/SuSEfirewall2 ]; then
                val=`SuSEfirewall2 status 2>&1 | sed -n '/not active/p'`

                if [ -n "$val" ]; then
                    CHECK_FLAG=1
                fi
            fi

            if [ $CHECK_FLAG = 0 ]; then
                while [ "`iptables -L -n | sed -n '/^LOG.* prefix .SFW2.* /p'`" = "" \
                       -a $LOOP_CNT -lt 180 ]
                do
                    LOOP_CNT=`expr $LOOP_CNT + 1`
                    sleep 1
                done
            fi
        fi
    }
fi

AXIS2C_HOME=/opt/nec/axis2c
PROFILEDIR=/var/lock/subsys
prog="eciServiceProgram"

start()
{
    if [ -f $PROFILEDIR/$prog -a -n "`pgrep -f $AXIS2C_HOME/bin/eciServiceProgram`" ]
    then
        echo "Running $prog "
    else
        if [ -f $AXIS2C_HOME/scripts/start_eci.sh ]
        then
            echo -n "Starting $prog: "

            check_firewall_setting  

            $AXIS2C_HOME/scripts/start_eci.sh

            RETVAL=$?

            log_success_msg
            return $RETVAL
        else
            echo -n "$prog File Not Founds"
            log_failure_msg
            return 1
        fi
    fi
}

stop()
{
    if [ -f $PROFILEDIR/$prog -o -n "`pgrep -f $AXIS2C_HOME/bin/EUAgent`" ]
    then
        echo -n "Stopping $prog: "
        $AXIS2C_HOME/scripts/stop_eci.sh
        RETVAL=$?

        kill_slpd

        log_success_msg
        return $RETVAL
    else
        echo -n "Not Running $prog: "
        log_failure_msg
        return 1
    fi
}

restart()
{
    echo "Restarting $prog: "
    return 0
}

status()
{
    if [ -n "`pgrep -f /opt/nec/axis2c/bin/axis2_http_server`" ]
    then
        if [ -n "`pgrep  -f /opt/nec/axis2c/bin/eciServiceProgram`" ]
        then
            if [ -n "`pgrep slpd`" ]
            then
                echo running
            else
                echo slpd stopped
            fi
        else
            echo error occurred
        fi
    else
        echo stopped
    fi
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        stop
        start
        ;;
    status)
        status
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|status}"
esac

exit $RETVAL

