#!/bin/bash 

#------------------------------------------------------------------------
# September, 2014, Balaji Krishnamoorthy
# Copyright (c) 2014-2015, 2019 by Cisco Systems, Inc.
# All Rights Reserved
#------------------------------------------------------------------------

MYADDR=$(ifconfig eth-vf1.3073 | grep 'inet addr:' | awk '{print $2}' | awk -F: '{print $2}')

CONFDADDR=$(ds_test_client -l confd6 | tail -1 | awk -F, '{print $2}' | awk -F= '{print $2}')

if [[ "$MYADDR" != "$CONFDADDR" ]]; then
    echo -n "Confd not active on this node: Run this command from VRF 0 of " >& 2
    H=$(ssh -q $CONFDADDR hostname)
    echo "$H" >& 2
    exit 1
fi

function usagexit() {
    echo ""
    echo "  USAGE : $0 receive-link 0|1"
    echo "    receive-link is provided as R/S/A/L where"
    echo "          R : Rack range LCC:0 to 15, FCC:F0 to F3"
    echo "          S : FC slot range LCC:FC0 to FC5, FCC:FC0 to FC11"
    echo "          A : SFE Asic range L-FC1:0 to 1, L-FC-M:0 to 3, FFC:0 to 2"
    echo "          L : SFE link number range 0 to 127."
    echo ""
    echo "    0 disables RX LOS application at both sides while 1 enables it"
    exit 1
} >&2

[[ $# -lt 2 ]] && { echo "Incorrect number of arguments!!" >&2; usagexit; }

thislink=$1
what=$2

feid=$(parselink -e $thislink)
[[ $feid -lt 768 ]] && { echo "link must be SFE link : $thislink !" >&2; usagexit; }

location=(`echo $thislink | tr '/' ' ' | tr [:lower:] [:upper:]`)
rack=${location[0]}
slot=${location[1]}
asic=${location[2]}
link=${location[3]}


# Validate the peer link
[ -f /tmp/link_info ] && rm /tmp/link_info
show_cmd "show controller sfe link-info rx $link $link topo instance $asic location $rack/$slot | inc FC | save /tmp/link_info" >& /dev/null
peerlink=$(awk '{print $(NF-1)}' /tmp/link_info)
rm /tmp/link_info
parselink $peerlink || { exit 33 ; }

create_rxlos_cint() {
    [ -f /tmp/cint_rxlos.c ] && rm -f /tmp/cint_rxlos.c

cat > /tmp/cint_rxlos.c <<-EOF
    cint_reset();
    int     rc;
    int     wb      = 0;
    int     unit    = $1;
    int     port    = $2;
    int     en      = $3;
    char    cmd[256];
  
    sprintf(cmd, "log file=/tmp/rxlos_enable_$1_$2 on", unit);
    bshell(unit, cmd);
    
    rc = rx_los_port_enable(unit, port, en, wb);
    sprintf(cmd, "\n echo Unit:%d, Port:%d, rxlos :%08x", unit, port, en);
    bshell(unit, cmd);

    rc = rx_los_dump(unit);

    sprintf(cmd, "log file=/tmp/rxlos_enable_$1_$2 off", unit);
    bshell(unit, cmd);
    
EOF

}

#
# Disable/Enable RX LOS at both ends.
#
for lnk in $thislink $peerlink
do
    feid=$(parselink -e $lnk)
    unit=$(parselink -u $lnk)
    asic=$(parselink -a $lnk)
    rack=$(parselink -r $lnk)
    slot=$(parselink -s $lnk)
    sfi=$(parselink -p $lnk)
    driver_ip=$(parselink -i $lnk)

    create_rxlos_cint $unit $sfi $what
    scp /tmp/cint_rxlos.c root@${driver_ip}:/tmp

    if [ "$MYADDR" = "$driver_ip" ]; then
        # this is local node.
        rm -f /tmp/rxlos_enable_${unit}_${sfi}
    else
        ssh -q $driver_ip rm -f /tmp/rxlos_enable_${unit}_${sfi}
    fi

    if [[ $feid -lt 768 ]]; then
        ssh -q $driver_ip /pkg/bin/fia_test diag_shell -u $unit -C \"cd /tmp/\" >& /dev/null
        ssh -q $driver_ip /pkg/bin/fia_test diag_shell -u $unit -C \"cint /tmp/cint_rxlos.c\" >& /dev/null
    else
        show_cmd "show controller sfe diagshell $asic \"cd /tmp/\" location $rack/$slot" >& /dev/null
        show_cmd "show controller sfe diagshell $asic \"cint /tmp/cint_rxlos.c\" location $rack/$slot" >& /dev/null
    fi

    if [ "$MYADDR" != "$driver_ip" ]; then
        scp -q root@${driver_ip}:/tmp/rxlos_enable_${unit}_${sfi} /tmp  >& /dev/null
    fi
    [ -f /tmp/rxlos_enable_${unit}_${sfi} ] && grep "\(Links\|Port ${sfi}:\)" /tmp/rxlos_enable_${unit}_${sfi}
done
