#!/bin/sh
HOSTNAME=`hostname`
IP=`ip link show | grep -v lo | grep UP | awk {'print $2'} | tr -d ":" | awk '{system("ip addr show dev "$1)}'  | grep inet | grep -v inet6 | awk {'print $2'} | awk -F "/" {'print $1'}`

# INTEL NIC UEFI FLASH SCRIPT
cd /root
# This link will be changed to our local mirror due to recent updates on intel side.
#wget https://downloadmirror.intel.com/29137/eng/Preboot_26.2.tar.gz
wget https://pxe.listabridge.clouvider.net/tools/Preboot_26.2.tar.gz

#BIOS
XML=("X11SCE-F")
MODEL=`dmidecode -t 2 | grep "Product Name:" | sed -e "s/Product Name://g" |  sed -e 's/^[ \t]*//'`

echo "Waiting 30 seconds (to MARS!)"
sleep 30

tar -zxvf Preboot_26.2.tar.gz
chmod +x  /root/APPS/BootUtil/Linux_x64/bootutil64e
pacman -Sy
pacman -S rpm-tools make gcc libffi core/glibc --noconfirm
rpm -i /root/APPS/BootUtil/Linux_x64/DRIVER/iqvlinux-*.noarch.rpm --nodeps --force
UEFI=`/root/APPS/BootUtil/Linux_x64/bootutil64e | grep UEFI | wc -l`

if [ $UEFI -gt 0 ]; then
     echo "NIC already flashed. Nothing to do"
else
     /root/APPS/BootUtil/Linux_x64/bootutil64e -up=pxe+efi -file=/root/APPS/BootUtil/BootIMG.FLB -ALL -QUIET
     if [[ " ${XML[@]} " =~ " ${MODEL} " ]]; then
	     curl http://91.238.82.19/bios/uefi.xml --output /root/uefi.xml
	     sum -c ChangeBiosCfg --file /root/uefi.xml
     fi
     sum
     # Code for pushing UEFI info to LB
	#Prepare JSON
	cat << EOF >> /tmp/uefi.json
	{
	    "host": {
	        "label": "$HOSTNAME",
		"ip": "$IP",
	        "hardware": {
	            "nic": "UEFI"
	        }
	    }
	}
EOF
    # Submit JSON to LB code

fi
reboot
