#!/bin/sh

source logs.shinc
touch /mnt/ramdisk/getlogs.progress
progressmonitor /mnt/ramdisk/getlogs.progress &
PMPID=$!

# Checks for getlogs, codeload, etc. need to occur first
ctlr=`/usr/bin/readslot`

#check to see if getlogs is already running
set -o noclobber
(echo "" > $lockfile) 2> /dev/null
if [ $? -ne 0 ]; then
    msg "ERROR: There is a get logs operation already in progress."
    msg "       Please reboot Management Controller $ctlr if this error persists."
    kill -9 $PMPID
    exit 1
fi
set +o noclobber

# make sure we are not loading code to ourselves
source "fcns.shinc";  # lib of shell fcns
isPfuInProgress pfuInProgress
if $pfuInProgress
then
    msg "ERROR: Code load is running on this controller $ctlr, get logs cannot continue."
    echo "RETURN_CODE: 12" >> $progressfile
    mv $progressfile /cfg/getlogs.last
    rm -f $lockfile
    kill -9 $PMPID
    exit 1
fi

# =====================================================================
# No matter what, run this function on exit
# =====================================================================
exitTrapFcn()
{
    local SUCCESS=$?

    rm /mnt/ramdisk/do.*

    return $SUCCESS
}
trap exitTrapFcn SIGINT SIGTERM EXIT

cd /mnt/ramdisk

if [ "$1" != "heatmap" ];
then

    contact=""
    email=""
    phone=""
    comment=""

    if [ "$1" != "" ]; then
        contact="log-contact \"$1\""
    fi
    if [ "$2" != "" ]; then
        email="log-email \"$2\""
    fi
    if [ "$3" != "" ]; then
        phone="log-phone \"$3\""
    fi
    if [ "$4" != "" ]; then
        comment="log-comment \"$4\""
    fi

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


    OTHER_INSTALLED="$(otherinst)"
    OTHER_AVAILABLE="false"

    if [ $OTHER_INSTALLED == "true" ]
    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
        #echo "Pinging other managment controller $other"
        result=`ping -c 1 -W 10 othermcinner | grep "1 packets received" | wc -l`
        if [ "$result" == "1" ]
        then
            #echo "Controller $other responded to ping."
            OTHER_AVAILABLE="true"
            break
        else
            #echo "The other controller $other did not respond to ping -- sleeping 5 seconds."
            sleep 5
        fi
    done
    fi

    # Check if the other controller is installed and up if so, check if running PFU, codeload, or get logs.
    if [ "$OTHER_INSTALLED" == "true" ] && [ "$OTHER_AVAILABLE" == "true" ]
    then
    # Check if PFU is running on the other controller
    rpcsys othermcinner "ls /mnt/ramdisk/is.pfu > /mem/isPfuRunning" > /dev/null 2> /dev/null
    RESULT=$?
    if [ "$RESULT" == "0" ]
    then
        rpcget othermcinner /mem/isPfuRunning /mem/other_isPfuRunning > /dev/null
        if [ -f /mem/other_isPfuRunning ]
        then
            _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, get logs cannot continue"
                kill -9 $PMPID
                rm -f $lockfile
                exit 1
            fi
        fi
    fi

    # Check if code load is running on the other controller
    rpcsys othermcinner "ls /mnt/ramdisk/is.bundle > /mem/isCodeLoadRunning" > /dev/null 2> /dev/null
    RESULT=$?
    if [ "$RESULT" == "0" ]
    then
        rpcget othermcinner /mem/isCodeLoadRunning /mem/other_isCodeLoadRunning > /dev/null
        if [ -f /mem/other_isCodeLoadRunning ]
        then
            _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, get logs cannot continue"
                kill -9 $PMPID
                rm -f $lockfile
                exit 1
            fi
        fi
    fi

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

    else
    msg "STATUS: The other Managment Controller $other is not installed or not up."
    fi

    source /app/etc/app.env

    /usr/bin/getsclogs "${contact}" "${email}" "${phone}" "${comment}" | tee -a /mnt/ramdisk/getlogs.progress

    /usr/bin/getmclogs $OTHER_AVAILABLE | tee -a /mnt/ramdisk/getlogs.progress

    msg "STATUS: get logs operation is complete"

    zip -urq /mnt/ramdisk/logs.zip $progressfile
    mv /mnt/ramdisk/logs.zip logs
    msgPartialSize /mnt/ramdisk/logs

    echo "RETURN_CODE: 12" >> $progressfile
    # signal the systracker that this activity is finished
    echo "[action:done]" >> $progressfile
    cp $progressfile /cfg/getlogs.last

else
    source /app/etc/app.env
    CAPI_LOG="/mnt/ramdisk/capidebugdata"
    touch $CAPI_LOG
    Controller_A=0
    Controller_B=0

    local otherController=0
    thisController=`readslot`

    if [ $thisController == 'A' ]
    then
        otherController='B'
    else
        otherController='A'
    fi
    # Hotspot Data:
    echo -e "\n\n################################## IO Distribution Data from controller $thisController ####################################\n\n" >> "$CAPI_LOG"
    capi.app --get IO_DISTRIBUTION --output "$CAPI_LOG"_"$thisController" --live

    if [ $? -ne 0 ];
    then
        msg "STATUS: couldnot get the heatmap data"
        echo "RETURN_CODE: 12" >> $progressfile
        # signal the systracker that this activity is finished
        echo "[action:done]" >> $progressfile
    else
        cat "$CAPI_LOG"_"$thisController" >> "$CAPI_LOG"
        echo -e "\n\n################################## IO Distribution Data from controller $otherController ####################################\n\n" >> "$CAPI_LOG"
        capi.app --get IO_DISTRIBUTION --output "$CAPI_LOG"_"$otherController" --live --other

        if [ $? -eq 0 ];
        then
            msg "STATUS: get heatmap operation is complete"
            cat "$CAPI_LOG"_"$otherController" >> "$CAPI_LOG"
            mv $CAPI_LOG /mnt/ramdisk/logs
            msgPartialSize /mnt/ramdisk/logs
        else
            msg "STATUS: could not get IO distribution data from the other controller"
            mv $CAPI_LOG /mnt/ramdisk/logs
            msgPartialSize /mnt/ramdisk/logs
        fi
        echo "RETURN_CODE: 12" >> $progressfile
        # signal the systracker that this activity is finished
        echo "[action:done]" >> $progressfile
    fi
    cp $progressfile /cfg/getheatmap.last
    rm -f "$CAPI_LOG"
    rm -f "$CAPI_LOG"_"$thisController"
    rm -f "$CAPI_LOG"_"$otherController"
fi
rm -f $lockfile
kill -9 $PMPID
exit 0
