#!/bin/bash

FixI40eX7XX() {
    [[ -d /sys/kernel/debug ]] || { mount -t debugfs none /sys/kernel/debug; sleep 3; }
    [[ -d /sys/kernel/debug/i40e ]] || return 1
    find /sys/kernel/debug/i40e -name command -exec sh -c 'i="$1";echo "lldp stop" > $i' shell {} \;
    sleep 3
}

_install() {

    if [[ $ENVFLAG = 'offline' ]];then
        return 0
    fi
    if [[ $VMFLAG = 1 ]];then
        return 0
    fi

    # # sodpangu前置环节已有布线检测，此处跳过
    # if [[ $PANGU = 'sodpangu' ]];then
    #     return 0
    # fi
    yum install -y -q lldpd

}

_check() {

    _check="PASS"

    if [[ $ENVFLAG = 'offline' ]];then
        return 0
    fi
    if [[ $VMFLAG = 1 ]];then
        return 0
    fi

    # sodpangu前置环节已有布线检测，此处跳过
    if [[ $PANGU = 'sodpangu' ]];then
        return 0
    fi

    FixI40eX7XX
    service lldpd restart > /dev/null 2>&1
    sleep 3
    lldpcli config lldp status rx-and-tx
    sleep 5

    if [ ! -f "/tmp/eth_info.log" ];then
        return 0
    fi

    local eth_num=$(egrep -c 'eth|net' /tmp/eth_info.log)
    if [ $eth_num -eq 0 ];then
         #_check="FAIL"
        PRINT_ERR "[ERROR] CSV Sheet Port Info Is NULL"
        else
        # centos7 physical machine, eth rename to net
        local net_num=$(ip a|grep -c ": net[0-9]:")
        if [ "$net_num" -ne 0 ];then
            sed -i 's/eth/net/g' /tmp/eth_info.log
        fi

        for nic in `cat /tmp/eth_info.log|jq -r 'keys[]'`
        do
            local port=$(cat /tmp/eth_info.log|jq -r ".[\"$nic\"]")
            ip link set $nic up
            sleep 3s
            lldpcli show neighbors port $nic > /tmp/os_init/lldpd_info_$nic.log
            sleep 3s
            local lldp_port=$(cat /tmp/os_init/lldpd_info_$nic.log|grep "PortID"|awk -F "ifname " '{print $NF}')
            if [[ -z $lldp_port ]];then
                for i in `seq 3`
                do
                    sleep 3
                    local lldp_port=$(cat /tmp/os_init/lldpd_info_$nic.log|grep "PortID"|awk -F "ifname " '{print $NF}')
                    if [[ -n $lldp_port ]];then
                        break
                    fi
                done
            fi

            local port=$(echo $port|sed 's/[[:space:]]//g')
            local lldp_port=$(echo $lldp_port|sed 's/[[:space:]]//g')

            if [[ -z $lldp_port ]];then
                _check="FAIL"
                PRINT_ERR "[ERROR] Can Not Collect $nic's LLDP Info."
            fi
           
            if [[ "$port" != "$lldp_port" ]];then
                _check="FAIL"
                PRINT_ERR "[ERROR] NIC:$nic CSV_Sheet_Port=$port Not Match With RealPort=$lldp_port, Check CSV Sheet Info."
            fi

            # If eth2 eth4 Has same IP, but eth4 not up, then del eth4 ip
            if [[ $(/sbin/ip addr|grep -c $HOSTIP) -eq 2 ]];then
                if [[ $(echo $nic|grep -c eth) -ne 0 ]];then
                    local ipfull=$(/sbin/ip addr| grep $HOSTIP |awk '{print $2}')
                    /sbin/ip addr del dev eth4 $ipfull
                elif [[ $(echo $nic|grep -c net) -ne 0 ]];then
                    local ipfull=$(/sbin/ip addr| grep $HOSTIP |awk '{print $2}')
                    /sbin/ip addr del dev net4 $ipfull
                fi
            fi
        done
    fi
    service lldpd stop >/dev/null 2>&1
}
