#!/bin/bash

#the postscript to configure interface in ONIE compatible switches

if ! cat /etc/os-release |grep -i  '^NAME=[ "]*Cumulus Linux[ "]*$' >/dev/null 2>&1 ; then
    echo "This script is only supported on Cumulus OS in ONIE switch"
    exit 2
fi

xcat_intf="/etc/network/interfaces.d/xCAT.intf"

if [ -f /xcatpost/mypostscript ]; then
    MASTER=`grep '^MASTER_IP=' /xcatpost/mypostscript |cut -d= -f2|sed s/\'//g`
fi

if [ -z "$MASTER" ]; then
    MASTER=$(cat /var/lib/dhcp/dhclient.eth0.leases|sed -n 's/.*cumulus-provision-url.*http:\/\+\([^\/]\+\)\/.*/\1/p'|tail -1)
    MASTER=$(echo $MASTER | awk -F: '{print $1}')
    if [ -z "$MASTER" ]; then
        echo "xCAT Master unset! Cannot download interface description"
        exit 2
    fi
fi

#Validate if this IP is reachable
ping $MASTER -c 1 >/dev/null
if [ $? -ne 0 ]; then
   echo "ERROR: The xCAT Master ip address $MASTER is not reachable";
   exit 1;
fi

ORIGFILE=/tmp/xCAT.intf.orig
TMPINT=/tmp/xCAT.intf
rm -f $TMPINT 2>/dev/null
UPDATED=0
DOWNLOADED=0

for name in $NODE ${GROUP//,/ } default; do
    curl -s -o $TMPINT -f http://${MASTER}/install/custom/sw_os/cumulus/interface/$name
    if [ -f $TMPINT ]; then
        DOWNLOADED=1
        if ! diff $TMPINT $xcat_intf > /dev/null; then
            rm -f $ORIGFILE
            cp $xcat_intf $ORIGFILE
            mv -f $TMPINT $xcat_intf
            UPDATED=1
            echo "New interface file downloaded, keep old one to $ORIGFILE";
        fi
        break
    fi
done

if [ $DOWNLOADED -eq 1 ] && [ $UPDATED -eq 0 ]; then
   echo "New interface file downloaded to $TMPINT, same as $xcat_intf file";
fi

if [ $DOWNLOADED -eq 0 ] && [ -f $xcat_intf ]; then
   echo "NO new interface file downloaded, keep same $xcat_intf file";
fi

if [ ! -f $xcat_intf ]; then
    UPDATED=1
    echo "NO new interface file downloaded, create a default $xcat_intf file";

    echo "#This is sample interface file provided by xCAT" > $xcat_intf
    echo "# bridge-vlan-aware: set to yes to indicate that the bridge is VLAN-aware. " >> $xcat_intf
    echo "# bridge-access: declares the access port. " >> $xcat_intf
    echo "# bridge-pvid: specifies native VLANs if the ID is other than 1. " >> $xcat_intf
    echo "# bridge-vids: declares the VLANs associated with this bridge. " >> $xcat_intf
    echo " " >> $xcat_intf

    #create default bridge
    echo "auto br0" >> $xcat_intf
    echo "iface br0" >> $xcat_intf
    echo "  bridge-vlan-aware yes" >> $xcat_intf
    echo "  bridge-ports glob swp1-52" >> $xcat_intf
    echo "  bridge-stp on" >> $xcat_intf
    echo "  bridge-vids 1 " >> $xcat_intf
    echo "  bridge-pvid 1" >> $xcat_intf
    echo " " >> $xcat_intf

    #create each interface

    for i in `seq 1 52`;
    do
        echo "auto swp$i">> $xcat_intf
        echo "iface swp$i" >> $xcat_intf
        echo "  mstpctl-portadminedge yes" >> $xcat_intf
        echo " " >> $xcat_intf
    done
fi

# license needs to set before start switchd
/usr/cumulus/bin/cl-license 2>/dev/null
if [ $? -ne 0 ]; then
   echo "ERROR: cumulus license does not exist";
   exit 1;
fi

if [ $UPDATED -eq 1 ]; then
    systemctl enable switchd
    systemctl start switchd
    ifreload -a
fi

