# This script is sourced by the clagd startup script

#
#   Clean up any residue from any previous clagd instances. 
#
do_cleanup()
{
    # Remove any files left over in /var/run
    echo Cleanup is executing.
    /bin/rm -f /var/run/clagd.*

    # On all bonds change the ad_actor_system to the default value and the  
    # lacp fallback to not active
    # All clag bond slaves are shutdown
    if [ -r /sys/class/net/bonding_masters ]
    then
        while read line
        do
            for bond in $line
            do
                if [ -r /sys/class/net/${bond}/bonding/mode ] && 
                   /bin/grep -Fxq "802.3ad 4" /sys/class/net/${bond}/bonding/mode
                then
                    if [ -w /sys/class/net/${bond}/bonding/ad_actor_system ] 
                    then
                        init_state=$(( $(cat /sys/class/net/${bond}/flags) & 1 ))
                        [ ${init_state} ] && /bin/ip link set ${bond} down
                        bond_mac=$(cat /sys/class/net/${bond}/address)
                        echo "${bond_mac}" > /sys/class/net/${bond}/bonding/ad_actor_system
                        [ ${init_state} ] && /bin/ip link set ${bond} up
                    fi
                fi
            done
        done < /sys/class/net/bonding_masters
    fi

    clagBondCleanupScript="/lib/clagctl-utils/clagBondCleanup"
    if [ -x "$clagBondCleanupScript" ]
    then
        "$clagBondCleanupScript"
    fi

    # Check if the VxLANs were configured.
    doVxLans=0
    if [ -r /cumulus/switchd/config/vxlan/local_ip ]
    then
        # Get the anycast IP and local IP, if configured
        anycastIp=$(cat /cumulus/switchd/config/vxlan/local_ip | /usr/bin/cut -d \  -f 1)
        localIp=$(cat /cumulus/switchd/config/vxlan/local_ip | /usr/bin/cut -d \  -f 2)
        if [ -n "${anycastIp}" ] && [ "${anycastIp}" != "0.0.0.0" ] && 
           [ -n "${localIp}" ] && [ "${localIp}" != "0.0.0.0" ]
        then
            # Remove the anycast IP from the loopback device
            doVxLans=1
            /bin/ip addr del dev lo ${anycastIp}/32 scope global 2> /dev/null

            # Get a list of VxLAN devices and set them all protodown
            /bin/ip -d -o link show | grep " vxlan id " | /usr/bin/cut -d \  -f 2 | /usr/bin/tr -d : | while read vxlan
            do
                /bin/ip link set dev ${vxlan} protodown on
            done
        fi
    fi

    # Remove learning disable, peerlink, and duallink on all bridge interfaces
    tmpdir=$(/bin/mktemp -dt "$(basename ${0}).XXXXXXXXXX") && \
    /sbin/bridge link show | /usr/bin/cut -d \  -f 2 | while read intf
    do
        /bin/echo "link set dev $intf learning on peerlink off duallink off" >> ${tmpdir}/brbatch
    done && \
    /sbin/bridge -force -batch ${tmpdir}/brbatch &&
    /bin/rm -rf ${tmpdir} ||
    /sbin/bridge link show | /usr/bin/cut -d \  -f 2 | while read intf
    do
        /sbin/bridge link set dev $intf learning on peerlink off duallink off
    done

    # Change the local IP address of VxLAN devices back to the original value
    if [ ${doVxLans} -eq 1 ]
    then
        /bin/ip -d -o link show | grep " vxlan id " | /usr/bin/cut -d \  -f 2 | /usr/bin/tr -d : | while read vxlan
        do
            /bin/ip link set dev ${vxlan} type vxlan local ${localIp}
        done
        echo -n "0.0.0.0 0.0.0.0                " > /cumulus/switchd/config/vxlan/local_ip
    fi

    echo Cleanup is finished
}

do_cleanup
