#! /bin/sh


# ==============================================================================
# Simple tap-networking for IncludeOS on Qemu
# ==============================================================================

# If you want to see the commands as they are are executed:
# set -x


# The name of the bridge VM's are added to
if [ -n "$INCLUDEOS_BRIDGE" ]; then
    BRIDGE=$INCLUDEOS_BRIDGE
else    
    BRIDGE="bridge43"
fi

# ==============================================================================
# Bringing up the bridge:
# ==============================================================================

#
# NOTE: Please make sure the BRIDGE actually exists and is up. 
#       We're not doing this in this script, since it adds overhead.
#       You can use "<repo>/etc/create_bridge.sh" as a starting point.

# NETWORK=10.0.0.0
# NETMASK=255.255.0.0
# GATEWAY=10.0.0.1
# DHCPRANGE=10.0.0.2,10.0.0.254

# sudo brctl addbr $BRIDGE
# sudo ifconfig $BRIDGE $GATEWAY netmask $NETMASK up

# ==============================================================================
# Bringing up the tap interface:
# ==============================================================================

if [ -n "$1" ];then
    
    # This takes up the 'tap'n interface (qemu keeps track of n)
    ifconfig $1 up

    # In the past we were supposedly required to do this to define a tap interface
    # sudo tunctl -u `whoami` -t $1
    
    # NOTE:
    # According to some guides we need to wait before adding the new interface to the bridge
    # sleep 0.5s
    if uname -s | grep Darwin > /dev/null 2>&1; then
    	ifconfig $BRIDGE addm $1
    else
    	brctl addif $BRIDGE $1
    fi
    exit 0

else
    echo "Error: no interface specified"
    exit 1
fi

