#!/bin/sh

ifp1="vlan11"
ifp2="vlan12"
lagg="lagg0"
vlan1="vlan900"
vlan2="vlan901"
tmp_mac="52:54:00:00:00:00"
delay=5
dt='date +[%H:%M:%S]'
# ifconfig vlan12 ether 52:54:12:12:12:12
# ifconfig vlan11 ether 52:54:11:11:11:11

if [ $# -lt 2 ]; then
	echo "Usage: $0 if1 if2"
	exit 1
fi
ifp1=$1
ifp2=$2

ether1=`ifconfig $ifp1 ether | awk '$1~/ether/{print$2}'`
if [ "$ether1" == "" ]; then echo "Failed to get MAC from $ifp1" ; exit 1; fi
ether2=`ifconfig $ifp2 ether | awk '$1~/ether/{print$2}'`
if [ "$ether2" == "" ]; then echo "Failed to get MAC from $ifp2" ; exit 1; fi

echo "`$dt` Working on:"
echo "	$ifp1 $ether1"
echo "	$ifp2 $ether2"

echo "`$dt` Creating lagg $lagg on top of $ifp1"
kldload if_lagg > /dev/null 2>&1
ifconfig $lagg create laggport $ifp1
if [ $? -ne 0 ]; then echo "Failed to setup lagg"; exit 1 ; fi
ifconfig $lagg inet 10.100.0.1/24
echo -n "`$dt` Testing inherited mac.. "
mac=`ifconfig $lagg ether | awk '$1~/ether/{print$2}'`
if [ "$mac" == "$ether1" ] ; then echo "OK"; else echo "FAIL"; fi

sleep $delay
echo -n "`$dt` Creating vlan devices ($vlan1, $vlan2) on ${lagg}.. "
ifconfig $vlan1 create vlan 900 vlandev $lagg up
ifconfig $vlan1 inet 10.90.0.1/24

ifconfig $vlan2 create vlan 901 vlandev $lagg up
ifconfig $vlan2 inet 10.90.1.1/24
echo "DONE"

echo "`$dt` Checking if interfaces has been created with proper macs.."
for i in $vlan1 $vlan2; do
	echo -n "	$i " ;
	mac=`ifconfig $i ether | awk '$1~/ether/{print$2}'`
	if [ "$mac" == "$ether1" ] ; then echo "OK"; else echo "FAIL"; fi
done

sleep $delay
echo -n "`$dt` Adding (slave) port $ifp2 to ${lagg}.. "
ifconfig $lagg laggport $ifp2
echo "DONE"

echo -n "`$dt` Checking if slave port mac has changed.. "
mac=`ifconfig $ifp2 ether | awk '$1~/ether/{print$2}'`
if [ "$mac" == "$ether1" ] ; then echo "OK"; else echo "FAIL"; fi

sleep $delay
echo -n "`$dt` Removing primary port from lagg ($ifp2 is new primary).. "
ifconfig $lagg -laggport $ifp1
echo "DONE"

echo "`$dt` Checking if all interfaces has updated their macs.."
echo -n "	$ifp1 " ;
mac=`ifconfig $ifp1 ether | awk '$1~/ether/{print$2}'`
if [ "$mac" == "$ether1" ] ; then echo "OK"; else echo "FAIL"; fi
for i in $ifp2 $lagg $vlan1 $vlan2; do
	echo -n "	$i " ;
	mac=`ifconfig $i ether | awk '$1~/ether/{print$2}'`
	if [ "$mac" == "$ether2" ] ; then echo "OK"; else echo "FAIL"; fi
done

sleep $delay
echo -n "`$dt` Adding (slave) port $ifp1 back.. "
ifconfig $lagg laggport $ifp1
echo "DONE"

echo -n "`$dt` Checking if slave port mac has changed.. "
mac=`ifconfig $ifp1 ether | awk '$1~/ether/{print$2}'`
if [ "$mac" == "$ether2" ] ; then echo "OK"; else echo "FAIL"; fi

sleep $delay
echo -n "`$dt` Trying to change mac manually to ${tmp_mac}.. "
ifconfig $lagg ether $tmp_mac
echo "DONE"
echo "`$dt` Checking if all interfaces has updated their macs.."
for i in $ifp1 $ifp2 $lagg $vlan1 $vlan2; do
	echo -n "	$i " ;
	mac=`ifconfig $i ether | awk '$1~/ether/{print$2}'`
	if [ "$mac" == "$tmp_mac" ] ; then echo "OK"; else echo "FAIL"; fi
done

# sleep $delay
# echo "Removing primary port $ifp1 .."
# ifconfig $lagg -laggport $ifp1

sleep $delay
echo -n "`$dt` Cleaning up.. "
ifconfig $vlan1 destroy
ifconfig $vlan2 destroy
# ifconfig $lagg -laggport $ifp2
ifconfig $lagg destroy
echo "DONE"

echo "`$dt` Checking original macs.."
echo -n "	$ifp1 " ;
mac=`ifconfig $ifp1 ether | awk '$1~/ether/{print$2}'`
if [ "$mac" == "$ether1" ] ; then echo "OK"; else
	echo -n "FAIL (was $ether1) now $mac. Restoring.. ";
	ifconfig $ifp1 ether $ether1
	echo "DONE"
fi
echo -n "	$ifp2 " ;
mac=`ifconfig $ifp2 ether | awk '$1~/ether/{print$2}'`
if [ "$mac" == "$ether2" ] ; then echo "OK"; else
	echo -n "FAIL (was $ether2) now $mac. Restoring.. ";
	ifconfig $ifp2 ether $ether2
	echo "DONE"
fi









