#!/bin/sh
#
#    Copyright (c) 2011-2013 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    Initialization script to start/stop/restart confd.
#

# Source common library
. /etc/init.d/functions

SWBD=`sin | swbd 2> ${NULL}`
CONFD_DIR=/usr/confd
CONFD=${CONFD_DIR}"/bin/confd"
CONFD_FLAGS="--addloadpath ${CONFD_DIR}/etc/brcd"
START_FLAGS="--ignore-initial-validation"
DCMD_TRC=/etc/fabos/Dcmd/Dcmd.Linux.powerpc.trc
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/fabos/lib

export CONFD_DIR
ulimit -c 1000000000 

wait_for_dcmd_and_resume()
{

    # Wait for DCMD to become ready
    print_msg=0
    waited_for=0
    clientport=10240
    while [ 1 ]
    do
        /fabos/libexec/DcmClient.Linux.powerpc -connectionretries 300 -clientip 127.0.0.1 -serverip 127.0.0.1 -clientport $clientport dcmdebug isdcmdready > /dev/null 2>&1
        if [ $? -eq 0 ]
        then
           let clientport=$clientport+1
           break;  
        fi

        if [ $print_msg -eq 0 ]
        then
            echo -n `/bin/date`
            echo -n " :: "
            echo "Confd: Waiting for Dcmd to become ready..."
            print_msg=1
        fi

        sleep 5;
        let waited_for=$waited_for+5
        let clientport=$clientport+1
    done

    # Wait for CCMD (only in FC mode) to become ready
    # Because Dcmd is ready at this point, we can assume fabos script has completed its work
    # In FWDL case, primary partition has the copied cluster.conf file from secondary partition

    local dcm_mode=$(get_dcm_mode)
    if [ $dcm_mode -ne 0 ]; then
        echo "Confd: No need to wait for Ccmd..."
    else
    print_msg=0
    waited_for=0
    clientport=11240
    while [ 1 ]
    do
        /fabos/libexec/DcmClient.Linux.powerpc -connectionretries 300 -clientip 127.0.0.1 -serverip 127.0.0.1 -clientport $clientport -serverport 7110 dcmdebug isdcmdready > /dev/null 2>&1
        if [ $? -eq 0 ]
        then
           let clientport=$clientport+1
           break;
        fi

        if [ $print_msg -eq 0 ]
        then
            echo -n `/bin/date`
            echo -n " :: "
            echo "confd: Waiting for Ccmd to become ready..."
            print_msg=1
        fi

        sleep 5;
        let waited_for=$waited_for+5
        let clientport=$clientport+1
    done
    fi
    
    # Wait for NosSMD
#    print_msg=0
#    waited_for=0
#    clientport=11540
#   while [ 1 ]
#   do
#       /fabos/libexec/DcmClient.Linux.powerpc -connectionretries 300 -clientip 127.0.0.1 -serverip 127.0.0.1 -clientport $clientport -serverport 19110 dcmdebug isNosSmdReady > /dev/null 2>&1
#          if [ $? -eq 0 ]
#        then
#           let clientport=$clientport+1
#           break;
#        fi
#
#        if [ $print_msg -eq 0 ]
#        then
#            echo -n `/bin/date`
#            echo -n " :: "
#            echo "confd: Waiting for NosSMD to become ready..."
#            print_msg=1
#        fi
#
#        sleep 5;
#        let waited_for=$waited_for+5
#        let clientport=$clientport+1
#    done

	sleep 5
    let waited_for=$waited_for+5

    echo -n `/bin/date`
    echo -n " :: "
    echo "Triggering confd load phase1 after waiting for $waited_for secs"
    
    # Capture the return status and error string if any
    retval_p1=1
    retval_p2=1
    phase1_retries=0
    max_phase1_retries=20

    while [ $phase1_retries -lt $max_phase1_retries ]
    do
        output=`$CONFD -c $CONFD_DIR/etc/confd.conf $CONFD_FLAGS $START_FLAGS --timeout 150 --start-phase1 2>&1`
        retval_p1=$?

        if [ $retval_p1 -eq 0 ]
        then
            break;
        else
            let phase1_retries=$phase1_retries+1
            sleep 5
        fi
    done

    if [ $retval_p1 -ne 0 ]
    then
        echo -n `/bin/date`
        echo -n " :: "
        echo "Confd phase1 load returned after $phase1_retries retries : $retval_p1"
        echo "Output: $output"
    fi

    # load confd phase2 only if phase1 load is successful
    # retval_p1 was 1 once! For now, loading phase2 without any check on return status from phase1.
    ## if [ $retval_p1 -eq 0 ]
    ## then
        output=`$CONFD -c $CONFD_DIR/etc/confd.conf $CONFD_FLAGS $START_FLAGS --timeout 150 --start-phase2 2>&1`
        retval_p2=$?
        if [ $retval_p2 -ne 0 ]
        then
            echo "Confd phase2 load returned: $retval_p2"
            echo "Output: $output"
        fi
    ## else
    ##     echo "Confd phase1 load failed and hence not loading phase2"
    ## fi

	sleep 2

    # retval_p2 was 1 once! For now, going ahead without any check on return status from phase2.
    ## if [ $retval_p2 -eq 0 ]
    ## then
    /fabos/libexec/DcmClient.Linux.powerpc -connectionretries 300 -clientip 127.0.0.1 -serverip 127.0.0.1 -clientport $clientport dcmdebug notifyconfdphase2complete

    if [ $dcm_mode -eq 0 ]
    then
    /fabos/libexec/DcmClient.Linux.powerpc -connectionretries 300 -clientip 127.0.0.1 -serverip 127.0.0.1 -clientport $clientport -serverport 7110 dcmdebug notifyconfdphase2complete
    fi

    ## else
    ##     echo "Confd phase1/phase2 load failed and hence not notifying DCMd"
    ## fi
}

start_phase0() {
        retval_p0=1
        output=`$CONFD -c $CONFD_DIR/etc/confd.conf $CONFD_FLAGS $START_FLAGS --start-phase0 2>&1`
        retval_p0=$?
        if [ $retval_p0 -eq 15 ]
        then
            echo "Confd phase0 load returned: $retval_p0 which implies CDB is corrupt. Removing the same and rebooting the switch"
            echo "Output: $output"
            /bin/rm -f /var/confd/cdb/A.cdb
            /bin/rm -f /var/confd/cdb/C.cdb
            /sbin/reboot -f
        fi

        wait_for_dcmd_and_resume &
}

start() {
	echo "Waiting to starting configuration management service"
    
    case ${SWBD##SWBD} in
    '98' | '1004' | '1005' | '1006' | '1007' | '1008' | '1009' | '1010' | '1011')
        ;;
    *)

    	echo "Starting configuration management service"

    if [ -f /etc/fabos/Dcmd/vcs.conf.fwdl ]
    then
        /bin/rm -rf /var/confd/log/*
        /bin/cp -f /mnt/var/confd/log/* /var/confd/log/
    fi

    #Ensure confd dir existence in core_files
    mkdir -p /core_files/confd
    cd /core_files/confd


    # The following check is to prevent a "Running DB may be inconsistent" message banner
    # continually being printed from confd_cli.  This can occur if we crash during confd
    # s_commit phase (Reference: Confd 3.5.2.brocade.1 User Guide: Section 23.11.3. Transaction commit failure).  
    # Our data provider's DB (DCMd/CCMd) will never be inconsistent here so we will simply
    # prevent this persistent warning banner from being displayed to the user.

    running_db_inconsistent_trigger=/var/confd/state/running.invalid

    if [ -e "$running_db_inconsistent_trigger" ]
    then
        /bin/rm $running_db_inconsistent_trigger
    fi

    # Do the required log rotation if the log files have grown
    # beyond the limit
    /etc/commonlogrotatescript.sh bootup

	if [ ! -e /etc/config/confd ] || [ $CHECK confd ]; then
	    # Phase 0 is run in background so that fabos script could be launched earlier and boot up time is saved.
        start_phase0 &
    fi

        ;;
    esac
}

stop() {
    $ECHO "Stopping configuration management service"

    $CONFD --stop 2> ${NULL}
}

case "$1" in

'start')
    start
    ;;

'stop')
    stop
    ;;

'restart')
    stop
    start
    ;;

'reload')
    $CONFD  --reload
    ;;

*)
    echo "usage: $0 {start | stop | restart | reload}"
    ;;

esac

