#!/bin/sh
#
# Copyright 2003 Meru Networks
# All rights reserved.
#
# Meru configuration
#
# chkconfig: 45 95 05
# description: The N Plus 1 Redundancy module.  On a master controller
#              it starts the advertising unit, np1adv.  On the slave it 
#              monitors the master with process np1slave and takes 
#              over if necessary.  On reboot a slave can only come up 
#              only as a slave.


# ****************************************************************************
# Filename:    np1main
# Component:   N Plus 1 Redundancy
# Subsystem:   scripts
# 
#      Copyright Meru Networks 2003, All Rights Reserved 
#  
# File Overview
# -------------
# Initialization script that is invoked after the Meru services are UP on the
# Slave and Master.  On the Master Controller it invokes the advertisement
# process (np1adv) and on the Slave it "stops" the Meru services and 
# starts up the server process (np1slave) on the Slave Controller.
# 
# 
# 
# Author: Kannan Konath
#         embedUR systems
#         42808 Christy St., Suite 102
#         Fremont, CA 94538
#         Phone: +1 (510) 353-9111
#
# Creation Date: November 21, 2005 (checkin into Perforce)
#
# Modifications:
# ---------------------------------------------------------------------------
# Author         Date        Comments
#                mm-dd-yy    
# ---------------------------------------------------------------------------
# Kannan         03-30-06    Filled out the stop routine. Processes were
#                            not being killed properly before. 
#              
# ****************************************************************************

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/opt/wlan/bin
export PATH

source /opt/wlan/np1/scripts/np1log-functions

source /etc/init.d/meru-functions > /dev/null 2>&1 

NP1_BASE=/opt/wlan/np1
NP1_CONFIG=${NP1_BASE}/config
NP1ADV=np1adv
NP1SLAVE=np1slave
NP1ADV_PATH=/opt/wlan/bin/np1adv
NP1SLAVE_PATH=/opt/wlan/bin/np1slave
NP1SLAVE_CONF=/opt/wlan/np1/config/slave.conf
NP1ROLE_FILE_f=/opt/wlan/np1/config/np1.role
NP1_CONF=/opt/wlan/etc/nplus1.conf


source /opt/wlan/etc/meru.conf > /dev/null 2>&1

function np1_start_syslogd
{
    # Sometimes we have seen the syslog daemon die and all the N+1 logs go out
    # to dinner. If syslog is dead restart it.

    if [ ! "$(/usr/bin/pgrep syslogd)" ]; then  
        /etc/init.d/syslog start
        np1_log_info "Starting syslogd ..."
    fi
}
function np1_start_convert2cef
{
    if [ ! "$(/usr/bin/pgrep convert2cef)" ]; then  
        /etc/init.d/ceflog start
        np1_log_info "Starting convert2cef ..."
    fi
}

start () {
    np1_log_info "---------------------------------------"
    np1_log_info "nPlus1 Redundancy Software invocation"
    np1_log_info "---------------------------------------"
    # First make sure we are not a slave controller
    if [ -e ${NP1SLAVE_CONF} ]; then


        np1_log_info "This is a Slave Controller"

        # First let us verify that the meru interface is up and if so,
        # bring it down
        np1_log_dbg_slave SCRIPT 1 "Bringing down the interfaces"
        /sbin/ifconfig meru > /dev/null 2>&1
        if [ $? == 0 ]; then
            /bin/cp -f /opt/wlan/etc/version ${NP1_CONFIG}/meru_sw_version
            np1_log_info "Stopping WLAN services"
            echo "Setting up this controller as a Passive Slave controller"

            logger -p local7.info "np1main: meru stop"
            /etc/init.d/meru stop > /dev/null 2>&1
        fi

        # We could have rebooted while we were playing the role 
        # of master controller. We should be rebooting as a Slave
        np1_log_dbg_slave SCRIPT 3 "Executing: source ${NP1SLAVE_CONF}"
        source  ${NP1SLAVE_CONF}

        source /etc/sysconfig/network-scripts/ifcfg-${REAL_IF}

        if [ "${IPADDR}" != "${SLAVE_IP}" ]; then
            # if no ip address is assigned, advice running setup and quit
            if [ -z "${IPADDR}" ]; then
                echo "ERROR: IP address not assigned or DHCP configured."
                echo "       Please run setup to assign static IP address before using nplus1"
                np1_log_err_warn "IP address not assigned. Please run setup before using nplus1"
                exit 1
            fi

            restart_syslog=1
            # Now we copy over the scripts
            np1_log_info "Current IP address ${IPADDR} and Slave IP address ${SLAVE_IP} do not match. Reset to Slave IP"
            np1_log_dbg_slave SCRIPT 1 "Executing:  /sbin/ifdown ${REAL_IF}"
            /sbin/ifdown ${REAL_IF} > /dev/null   2>&1 
        else
            restart_syslog=0
        fi


        np1_log_info "Executing: /opt/wlan/np1/scripts/np1mkslave.sh"
        /opt/wlan/np1/scripts/np1mkslave.sh

        np1_log_dbg_slave SCRIPT 2 "Executing: source /etc/sysconfig/network-scripts/ifcfg-${REAL_IF}"
        source  /etc/sysconfig/network-scripts/ifcfg-${REAL_IF}
        
        np1_log_dbg_slave SCRIPT 2 "Executing: source /etc/sysconfig/network"
        source  /etc/sysconfig/network

        # ${LOG_LINE_INFO} "${NP1_BASE}/scripts/fix_hosts.pl  ${SLAVE_IP} ${SLAVE_HOSTNAME}"
        # ${NP1_BASE}/scripts/fix_hosts.pl  ${SLAVE_IP} ${SLAVE_HOSTNAME}


        np1_log_dbg_slave SCRIPT 1 "Setting Hostname: ${HOSTNAME}"
        /bin/hostname ${HOSTNAME}

        np1_log_dbg_slave SCRIPT 1 "Executing:  /sbin/ifconfig ${REAL_IF} ${SLAVE_IP} netmask ${SLAVE_NETMASK} broadcast ${BROADCAST}  up"
        /sbin/ifconfig ${REAL_IF} ${SLAVE_IP} netmask ${SLAVE_NETMASK} broadcast ${SLAVE_BROADCAST} up  > /dev/null 2>&1

        np1_log_dbg_slave SCRIPT 1 "Executing:  /sbin/ifup ${REAL_IF}" 
        /sbin/ifup ${REAL_IF} > /dev/null 2>&1 

        
        # If we do not do this, we see the wrong hostname in the logs!
        if [ ${restart_syslog} -eq 1 ]; then
            /etc/init.d/syslog stop > /dev/null 2>&1
            /etc/init.d/ceflog stop > /dev/null 2>&1
            sleep 2
            /etc/init.d/syslog start > /dev/null 2>&1
            /etc/init.d/ceflog start > /dev/null 2>&1
        fi


        # Just in case we need to change any of the configurable
        # parameters in the field.
        if [ -f ${NP1_CONF} ]; then
            source ${NP1_CONF}
        fi

        # Verify that there is no master advertisement process
        # running.  If so, kill it!
        np1_log_dbg_slave SCRIPT 3 "Executing: pgrep ${NP1ADV_PATH}"
        pgrep ${NP1ADV_PATH} > /dev/null 2>&1
        if [ $? == 0 ]; then
            np1_log_err_slave "Killing ${NP1ADV_PATH} process on Slave"
            #np1_log_err_slave "Executing: pkill -f ${NP1ADV}"
            #pkill -f ${NP1ADV}
            #pkill -9 -f ${NP1ADV}
            np1_log_err_slave "Executing: /etc/init.d/${NP1ADV} stop"
            /etc/init.d/${NP1ADV} stop > /dev/null 2>&1
        fi


        # Verify that we do not have a slave proces running. If
        # so, bail
        np1_log_dbg_slave SCRIPT 3 "Executing: pgrep ${NP1SLAVE_PATH}"
        pgrep ${NP1SLAVE_PATH} > /dev/null 2>&1
        if [ $? -eq 0 ]; then
            np1_log_err_warn "${NP1SLAVE} process on Slave is running. Bailing out..."
            exit 1
        fi
        
        export HOSTNAME
        NP1_SLAVE_IF=${REAL_IF}
        export NP1_SLAVE_IF

        # restart np1 specific prcoesses after applying running hostname
        logger -p local7.info "np1main: meru.level1 start"
        /etc/init.d/meru.level1 start > /dev/null 2>&1

        # Start the Slave process
        np1_log_info "Executing: ${NP1SLAVE_PATH}"

        #${NP1SLAVE_PATH} > /dev/null 2>&1 < /dev/null &
        # Start np1slave by monit
        /etc/init.d/${NP1SLAVE} start

        kill -9 `pgrep np1-redun-ip.sh` > /dev/null 2>&1
        /opt/wlan/np1/scripts/np1-redun-ip.sh ${SLAVE_IP} ${NETMASK} ${GATEWAY} ${BROADCAST} > /dev/null 2>&1 &

        
        # Good to go
        np1_log_info "${NP1SLAVE_PATH} process started..."
 

        exit 0
    else
        np1_log_info "This is a Master Controller"
        /bin/cp -f /opt/wlan/etc/version ${NP1_CONFIG}/meru_sw_version

        # We assume that the meru services are up OR should we???

        # Verify that we do not have a slave proces running. If
        # so, bail
        np1_log_dbg_slave SCRIPT 3 "Executing: pgrep ${NP1SLAVE_PATH}"
        pgrep ${NP1SLAVE_PATH} > /dev/null 2>&1 
        if [ $? == 0 ]; then
            np1_log_err "${NP1SLAVE_PATH} process on Slave is running. Killing it..."
            #np1_log_err "Executing: pkill -x ${NP1SLAVE_PATH}"
            #pkill -f ${NP1SLAVE_PATH} > /dev/null 2>&1
            #pkill -9 -f ${NP1SLAVE_PATH} > /dev/null 2>&1
            np1_log_err "Executing: /etc/init.d/${NP1SLAVE} stop"
            /etc/init.d/${NP1SLAVE} stop > /dev/null 2>&1
        fi

        # Verify that there is no master advertisement process
        # running.  If so, bail
        np1_log_info  "Executing: pgrep ${NP1SLAVE_PATH}"
        pgrep ${NP1ADV_PATH} > /dev/null 2>&1
        if [ $? == 0 ]; then
            np1_log_err_warn "${NP1ADV_PATH} process on master is already running. Bailing out..."
            exit 1
        fi
        
        # Just in case we need to change any of the configurable
        # parameters in the field.
        if [ -f ${NP1_CONF} ]; then
            source ${NP1_CONF}
        fi

        # Start the Advertisement process
        np1_log_info  "Executing: ${NP1ADV_PATH}"

        #${NP1ADV_PATH} > /dev/null 2>&1 < /dev/null &
        # Start np1adv by monit
        /etc/init.d/${NP1ADV} start

        # Good to go
        np1_log_info  "${NP1ADV_PATH} process started..."
 
        exit 0
    fi
}


stop() {
    local myrole
    np1_log_info "---------------------------------------"
    np1_log_info "Stopping nplus1 Redundancy"
    np1_log_info "---------------------------------------"
    if [ -e ${NP1SLAVE_CONF} ]; then

        myrole=passive

        np1_log_info  "Stopping service np1main on Slave controller with process ${NP1SLAVE_PATH}"
        if [ -f ${NP1ROLE_FILE_f} ]; then
            source ${NP1ROLE_FILE_f}
            myrole=${NP1ROLE}
        fi

        if [ "${myrole}" != "passive" ]; then
            np1_log_err "nplus1 can only be stopped in a Passive Slave"
            exit 1
        fi
        
        # First let us verify that the meru interface is up and if so,
        # bring it down
        np1_log_dbg_slave SCRIPT 1 "Checking if WLAN services are running"
        /sbin/ifconfig meru > /dev/null 2>&1
        if [ $? == 0 ]; then
            np1_log_info "Stopping WLAN services..."
            /etc/init.d/meru stop > /dev/null 2>&1
        fi

        kill -9 `pgrep np1-redun-ip.sh` > /dev/null 2>&1

        # stop np1 specific processes 
        np1_log_info "Stopping np1 specific processes..."
        /etc/init.d/meru.level1 stop > /dev/null 2>&1

        #pkill ${NP1SLAVE_PATH}
        #pkill -9 -f ${NP1SLAVE_PATH}
        # Stop np1slave by monit
        /etc/init.d/${NP1SLAVE} stop

        np1_log_info "Stopped ${NP1SLAVE_PATH}"

        # Let's make sure we start up again with the Slave's IP.  This
        # caters for the case where np1slave process crashed!
        np1_log_dbg SCRIPT 2 "Executing: source ${NP1SLAVE_CONF}"
        source  ${NP1SLAVE_CONF}

        np1_log_dbg SCRIPT 2 "Executing: source /etc/sysconfig/network-scripts/ifcfg-${REAL_IF}"
        source  /etc/sysconfig/network-scripts/ifcfg-${REAL_IF}
        
        np1_log_dbg SCRIPT 2  "Executing: source /etc/sysconfig/network"
        source  /etc/sysconfig/network

        if [ "${IPADDR}" != "${SLAVE_IP}" ]; then
            # Now we copy over the scripts
            np1_log_info  "System rebooted when configured with a non-slave IP address. Reset to Slave IP"
            /sbin/ifdown ${REAL_IF} > /dev/null 2>&1 
        fi

        # Fix for Bug ID 23561
        # Before restoring the configuration back let's copy the shadow file
        # back to backup dir of SLAVE_IP in case the password is changed on Passive Slave

        np1_log_dbg_slave SCRIPT 2 "Executing: /bin/cp -f /etc/shadow ${NP1_CONFIG}/${SLAVE_IP}/shadow"
        /bin/cp -f /etc/shadow ${NP1_CONFIG}/${SLAVE_IP}/shadow

        np1_log_info "Executing: /opt/wlan/np1/scripts/np1mkslave.sh"
        /opt/wlan/np1/scripts/np1mkslave.sh

        sleep 3
        kill -9 `pgrep np1-redun-ip.sh` > /dev/null 2>&1

        np1_log_dbg SCRIPT 2  "Executing: source /etc/sysconfig/network-scripts/ifcfg-${REAL_IF}"
        source  /etc/sysconfig/network-scripts/ifcfg-${REAL_IF}
        
        np1_log_dbg SCRIPT 2   "Executing: source /etc/sysconfig/network"
        source  /etc/sysconfig/network

        np1_log_dbg SCRIPT 2  "Executing:  /sbin/ifconfig ${REAL_IF} ${SLAVE_IP} netmask ${SLAVE_NETMASK} broadcast ${BROADCAST}  up"
        /sbin/ifconfig ${REAL_IF} ${SLAVE_IP} netmask ${SLAVE_NETMASK} broadcast ${SLAVE_BROADCAST} up > /dev/null 2>&1 

        np1_log_dbg SCRIPT 2  "Executing:  /sbin/ifup ${REAL_IF}"
        /sbin/ifup ${REAL_IF} > /dev/null 2>&1


        np1_log_info "Starting WLAN service"
        echo "Making this a normal controller. Starting WLAN services...."
        /sbin/service meru start > /dev/null 2>&1

        exit 0
    else
        np1_log_info "Stopping service np1main on Master controller with process ${NP1ADV_PATH}"

        #pkill -f ${NP1ADV_PATH}
        #pkill -9 -f ${NP1ADV_PATH}
        # Stop np1adv by monit
        /etc/init.d/${NP1ADV} stop

        np1_log_info "Stopped ${NP1ADV_PATH}"
        exit 0
    fi

}

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

exit 0
