#!/bin/sh
#set -x

kldstat -qm if_bridge && kldunload if_bridge
kldstat -qm if_lagg && kldunload if_lagg
kldstat -qm if_epair && kldunload if_epair

# test renamed interfaces
br0=$( ifconfig bridge create name br0 )
br1=$( ifconfig bridge create name br )
br2=$( ifconfig bridge create name bridge0a )
br2=$( ifconfig $br2 name bridge0b )
# test destroy renamed interfaces
ifconfig $br0 destroy
ifconfig $br1 destroy
ifconfig $br2 destroy

# test unit number release
br0=$( ifconfig bridge0 create name br0 )
br1=$( ifconfig bridge0 create name br )
br2=$( ifconfig bridge0 create name bridge0a )
ifconfig $br0 destroy
ifconfig $br1 destroy
ifconfig $br2 destroy

# test rename back
br0=$( ifconfig bridge create name br0 )
br1=$( ifconfig bridge create name br )
br2=$( ifconfig bridge create name bridge0a )
br3=$( ifconfig bridge create name bridge4 )
br0=$( ifconfig $br0 name bridge0 )
br1=$( ifconfig $br1 name bridge1 )
br2=$( ifconfig $br2 name bridge2 )
br3=$( ifconfig $br3 name bridge3 )
ifconfig $br0 destroy
ifconfig $br1 destroy
ifconfig $br2 destroy
ifconfig $br3 destroy

# test unit number leak
kldload if_lagg
lagg0=$( ifconfig lagg0 create )
br0=$( ifconfig bridge create name br0 )
br1=$( ifconfig bridge create name br )
br2=$( ifconfig bridge create name bridge0a )
br3=$( ifconfig bridge create name bridge4 )
br4=$( ifconfig bridge create name lagg4 )
kldunload if_lagg
kldunload if_bridge

# test unit number release
br0=$( ifconfig bridge0 create name br0 )
br1=$( ifconfig bridge create )
# test destroy w/o unit number
[ "$br1" = "bridge0" ] || {
  echo "Fail, exepect bridge0 but got $br1"
  exit 1
}
ifconfig $br0 destroy
ifconfig $br1 destroy


# test conflict name with other interface
ifconfig lagg0 create
ifconfig bridge0 create
br0=$( ifconfig bridge0 name lagg1 )

ifconfig $br0 name lagg0 >/dev/null 2>&1 && {
    echo "Fail, duplicate interface: lagg0"
    exit 1
}
ifconfig lagg1 create >/dev/null 2>&1 && {
    echo "Fail, duplicate interface: lagg1"
    exit 1
}
lagg2=$( ifconfig lagg create )
ifconfig lagg0 destroy
ifconfig $lagg2 destroy
ifconfig $br0 destroy
kldunload if_lagg

# test no auto-number cloner
ep=$( ifconfig epair create name ep )
ep=$( ifconfig $ep name epair0a )
ifconfig $ep destroy

# test vnet move
br0=$( ifconfig bridge create )
br1=$( ifconfig bridge create )
br2=$( ifconfig bridge create name br )
j=$( jail -ic vnet persist )
# move to vnet
ifconfig $br1 vnet $j
ifconfig $br2 vnet $j
ifconfig $br1 create >/dev/null 2>&1 || {
  echo "Should be able to create $br1 in home vnet"
  exit 1
}
ifconfig $br1 destroy
br2=$( jexec $j ifconfig $br2 name $br0 )
# move back
ifconfig $br1 -vnet $j
ifconfig $br2 -vnet $j
ifconfig $br0 destroy
ifconfig $br1 destroy
ifconfig $br2 destroy
jail -R $j

# test vnet move
br0=$( ifconfig bridge create )
br1=$( ifconfig bridge create )
br2=$( ifconfig bridge create name br )
j=$( jail -ic vnet persist )
ifconfig $br1 vnet $j
ifconfig $br2 vnet $j
br2=$( jexec $j ifconfig $br2 name $br0 )
jail -R $j
ifconfig $br0 destroy
ifconfig $br1 destroy
ifconfig $br2 destroy

# test physical interface rename, change eth
en="vmx0"
ifconfig -l ether | grep $en >/dev/null && {
  eth=$( ifconfig $en name eth0 )
  eth=$( ifconfig $eth name $en )

  j=$( jail -ic vnet persist )
  ifconfig $en vnet $j
  eth=$( jexec $j ifconfig $en name eth0 )
  eth=$( jexec $j ifconfig $eth name $en )
  ifconfig $eth -vnet $j
  jail -R $j
  
  j=$( jail -ic vnet persist )
  ifconfig $en vnet $j
  jail -R $j
  ifconfig $en >/dev/null 2>&1 || {
    echo "Expect ethernet $en return home"
    exit 1
  }
}
