#!/bin/sh

source logs.shinc

ctlr=`/usr/bin/readslot`

cd /mnt/ramdisk

# collect local MC logs
getmclocallogs

if [[ $ctlr == "A" ]]
then
    other="B"
else
    other="A"
fi

# Only check the status if we have not done so already
if [ "$1" == "" ]
then

    # Can we talk to the other MC, we can't take too long doing this or ftp will time out. Ross 042112
    for i in $(seq 1 5)
    do
        #msg "Pinging Managment Controller $other"
        result=`ping -c 1 -W 10 othermcinner | grep "1 packets received" | wc -l`
        if [ "$result" == "1" ]
        then
            #msg "Management Controller $other responded to ping."
            break
        else
            #msg "Management Controller $other did not respond to ping -- Sleeping 5 seconds."
            sleep 5
        fi

        if [ "$result" == "1" ]
        then
            OTHER_AVAILABLE="true"
        else
            msg "ERROR: Cannot collect Management Controller $other's logs because the other controller is not up."
        fi

        if [ "$OTHER_AVAILABLE" == "true" ]
        then
            # Check if PFU is running on the other controller 
            rm -f /mem/other_isPfuRunning
            rpcsys othermcinner "ls /mnt/ramdisk/is.pfu > /mem/isPfuRunning"
            RESULT=$?
            if [ "$RESULT" == "0" ]
            then
                rpcget othermcinner /mem/isPfuRunning /mem/other_isPfuRunning > /dev/null
                _flength=$(wc -l /mem/other_isPfuRunning | awk '{ print $1; }')
                rm -f /mem/other_isPfuRunning
                if [ $_flength -gt 0 ]
                then
                    msg "The other controller $other is running PFU, getmclogs cannot continue"
                    exit 1
                fi
            fi
        fi
        # Check if code load is running on the other controller
        rpcsys othermcinner "ls /mnt/ramdisk/is.bundle > /mem/isCodeLoadRunning"
        RESULT=$?
        if [ "$RESULT" == "0" ]
        then
            rpcget othermcinner /mem/isCodeLoadRunning /mem/other_isCodeLoadRunning > /dev/null
            _flength=$(wc -l /mem/other_isCodeLoadRunning | awk '{ print $1; }')
	    	rm -f /mem/other_isCodeLoadRunning
            if [ $_flength -gt 0 ]
            then
                msg "The other controller $other is running code load, getmclogs cannot continue"
                exit 1
            fi
        fi

        # Check if get logs is running on the other controller
        rpcsys othermcinner "ls /mnt/ramdisk/is.getlogs > /mem/isGetLogsRunning"
        RESULT=$?
        if [ "$RESULT" == "0" ]
        then
            rpcget othermcinner /mem/isGetLogsRunning /mem/other_isGetLogsRunning > /dev/null
            _flength=$(wc -l /mem/other_isGetLogsRunning | awk '{ print $1; }')
    	    rm -f /mem/other_isGetLogsRunning
            if [ $_flength -gt 0 ]
            then
                msg "The other controller $other is running getlogs, getmclogs cannot continue"    
                exit 1
            fi
        fi

    done
else
    OTHER_AVAILABLE=$1
fi

if [ "$OTHER_AVAILABLE" == "true" ]
then

   msg "STATUS: Getting logs from Partner Managment Controller $other"

   rpcsys othermcinner /usr/bin/getmclocallogs
   for i in $(seq 1 5)
   do
       rpcget othermcinner /mnt/ramdisk/logs.zip /mnt/ramdisk/$other-logs.zip > /dev/null
       rc=$?
       if [ "$rc" == "0" ]
       then
          #msg "Received controller $other logs.zip rc=$rc"
          break
       else
          msg "retrying in 5 seconds"
          sleep 5
       fi
   done
   #check if file exists, then add other-logs to logs.zip
   if [[ -f /mnt/ramdisk/$other-logs.zip ]]
   then
       mkdir /mnt/ramdisk/$other
       unzip -q /mnt/ramdisk/$other-logs.zip -d /mnt/ramdisk/$other
       zip -urq /mnt/ramdisk/logs.zip $other > /dev/null
       rm -f $other-logs.zip
       rm -fr $other
       rpcsys othermcinner "rm /mnt/ramdisk/logs.zip"
       msg "STATUS: Finished getting the Partner Managment Controller $other logs"
   else
       msg "STATUS: Unable to get logs from Partner Management Controller $other"
       exit 1
   fi
fi
exit 0

