#!/bin/sh

mmip=(127.2.1.1 127.2.1.2 127.2.1.0 127.2.2.0 127.2.1.15 127.2.1.16)
lcip=(127.2.1.1 127.2.1.2 127.2.1.3 127.2.1.4 127.2.1.7 127.2.1.8 127.2.1.9 127.2.1.10 127.2.1.11 127.2.1.12 127.2.1.13 127.2.1.14 127.2.2.1 127.2.2.2 127.2.2.3 127.2.2.4 127.2.2.7 127.2.2.8 127.2.2.9 127.2.2.10 127.2.2.11 127.2.2.12 127.2.2.13 127.2.2.14 )
SWBD=`/sbin/sin 2>/dev/null | /bin/grep SWBD | /usr/bin/cut -d ' ' -f 2 | /usr/bin/cut -d ',' -f 1`

_debug_cpu_alive_check ()
{
    #/usr/bin/rsh $1 'ls -al > /dev/null' >  /dev/null 2>&1
    /bin/ping $1 -c1 -W1  > /dev/null 2>&1
    return $?
}

_debug_mptool_all_lc_data_trigger ()
{
    for lc in ${lcip[@]};
    do
        _debug_cpu_alive_check ${lc}
        if [ ! $? = 0 ];
        then
            continue
        fi

        /usr/bin/rsh ${lc} /fabos/link_sbin/mptool --memdump all > /dev/null
    done
}

_debug_mptool_all_mm_data_trigger ()
{
    for mm in ${mmip[@]};
    do
        _debug_cpu_alive_check ${mm}
        if [ ! $? = 0 ];
        then
            continue
        fi

        /usr/bin/rsh ${mm} /fabos/link_sbin/mptool --memdump all > /dev/null
    done
}

_debug_mptool_data_trigger ()
{
    #Local and Remote trigger
    _debug_mptool_all_mm_data_trigger

    #No difference between 1000 and 1001 from number of MM/LC point of view.
    if [ $SWBD = "SWBD1001" ];
    then
        SWBD="SWBD1000"
    fi

    if [ $SWBD = "SWBD1000" ];
    then
        _debug_mptool_all_lc_data_trigger
    fi
}

_debug_mptool_all_lc_data_copy ()
{
    for lc in ${lcip[@]};
    do
        _debug_cpu_alive_check ${lc}
        if [ ! $? = 0 ];
        then
            continue
        fi

        mkdir /tmp/mptool/lc_${lc}/
        /usr/bin/rsh ${lc} /fabos/bin/nos_mptool -l
        /bin/rcp ${lc}:/tmp/mptool.tar.gz /tmp/mptool/lc_${lc}/
        cd /tmp/mptool/lc_${lc}/
        tar xfz mptool.tar.gz
        rm -f mptool.tar.gz
        /usr/bin/rsh ${lc} /bin/rm -f /tmp/mptool.tar.gz
    done
}

_debug_mptool_all_mm_data_copy ()
{
    for mm in ${mmip[@]};
    do
        _debug_cpu_alive_check ${mm}
        if [ ! $? = 0 ];
        then
            continue
        fi

        mkdir /tmp/mptool/mm_${mm}/
        /usr/bin/rsh ${mm} /fabos/bin/nos_mptool -l
        /bin/rcp ${mm}:/tmp/mptool.tar.gz /tmp/mptool/mm_${mm}/
        cd /tmp/mptool/mm_${mm}/
        tar xfz mptool.tar.gz
        rm -f mptool.tar.gz
        /usr/bin/rsh ${mm} /bin/rm -f /tmp/mptool.tar.gz
    done
}

_debug_mptool_data_copy ()
{
    /bin/rm -fr /tmp/mptool/
    /bin/mkdir /tmp/mptool/

    #Local and Remote trigger
    _debug_mptool_all_mm_data_copy

    #No difference between 1000 and 1001 from number of MM/LC point of view.
    if [ $SWBD = "SWBD1001" ];
    then
        SWBD="SWBD1000"
    fi

    if [ $SWBD = "SWBD1000" ];
    then
        _debug_mptool_all_lc_data_copy
    fi

    var=`/bin/date +"%F-%H-%M"`
    mv /tmp/mptool /tmp/mptool_${var}
    cd /tmp/
    tar cfz /var/log/mptool_${var}.tar.gz mptool_${var}
    /bin/rm -fr /tmp/mptool_${var}

    echo "mptool data available under /var/log/mptool_${var}.tar.gz"
}

_debug_mptool_data_trigger_and_copy ()
{
    #Dump
    _debug_mptool_data_trigger

    #copy
    _debug_mptool_data_copy
}

_debug_mptool_local_data_copy ()
{
    /bin/rm -fr /tmp/mpt/
    /bin/mkdir /tmp/mpt/
    /bin/mv /var/log/mp_* /tmp/mpt/
    cd /tmp/mpt/
    tar cfz /tmp/mptool.tar.gz *
    /bin/rm -fr /tmp/mpt/
}

_debug_mptool_all_lc_start ()
{
    for lc in ${lcip[@]};
    do
        _debug_cpu_alive_check ${lc}
        if [ ! $? = 0 ];
        then
            continue
        fi

        /usr/bin/rsh ${lc} /fabos/link_sbin/mptool --mtrace all > /dev/null
    done
}

_debug_mptool_all_mm_start ()
{
    for mm in ${mmip[@]};
    do
        _debug_cpu_alive_check ${mm}
        if [ ! $? = 0 ];
        then
            continue
        fi

        /usr/bin/rsh ${mm} /fabos/link_sbin/mptool --mtrace all > /dev/null
    done
}

_debug_mptool_all_start ()
{
    _debug_mptool_all_mm_start

    #No difference between 1000 and 1001 from number of MM/LC point of view.
    if [ $SWBD = "SWBD1001" ];
    then
        SWBD="SWBD1000"
    fi

    if [ $SWBD = "SWBD1000" ];
    then
        _debug_mptool_all_lc_start
    fi
}

#Main Body

if [ ! $# = 1 ];
then
    echo "Usage: nos_mptool < -s | -r | -e >"
    echo "      -s: Start mptool"
    echo "      -r: Collect mptool data before reboot"
    echo "      -e: Collect and copy mptool data under /tmp/mptool/"
    exit 1
fi

if [ $1 = "-s" ];
then
    /bin/cat /etc/fabos/nos_debug.conf | /bin/grep VG > /dev/null
    if [ $? = 0 ];
    then
        echo "Valgrind already enabled in this build. Not starting MPTOOL"
        exit 0
    fi

    _debug_mptool_all_start
fi

if [ $1 = "-r" ];
then
    _debug_mptool_data_trigger_and_copy
fi

if [ $1 = "-e" ];
then
    _debug_mptool_data_trigger_and_copy
fi

#Undocumented -l mode.
#This mode is used for internal implementation
if [ $1 = "-l" ];
then
    _debug_mptool_local_data_copy
fi

exit 0
