#!/bin/sh
echo "Bringup eNB interface"

sudo rmmod nasmesh
sudo insmod $OPENAIR2_DIR/NAS/DRIVER/MESH/nasmesh.ko 

#avoid conflict with reserved multicast addresses (224.0.0.11,224.0.0.13, 224.0.0.16)
#sudo route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
sudo ip route add 224.0.0.160/28 dev eth0


echo "bring up oai0 interface for enb"
sudo ifconfig oai0 10.0.1.1 netmask 255.255.255.0 broadcast 10.0.1.255
echo "bring up oai1 interface for ue 1"
sudo ifconfig oai1 10.0.2.2 netmask 255.255.255.0 broadcast 10.0.2.255

# enb -> ue1
$OPENAIR2_DIR/NAS/DRIVER/MESH/RB_TOOL/rb_tool -a -c0 -i0 -z0 -s 10.0.1.1 -t 10.0.1.2 -r 1
# ue1 -> enb
$OPENAIR2_DIR/NAS/DRIVER/MESH/RB_TOOL/rb_tool -a -c0 -i1 -z0 -s 10.0.2.2 -t 10.0.2.1 -r 1

if   [ $1 -ge 2 ]; then 
    echo "bring up oai2 interface for ue 2"
    sudo ifconfig oai2 10.0.3.3 netmask 255.255.255.0 broadcast 10.0.3.255
    
# enb -> ue2
    $OPENAIR2_DIR/NAS/DRIVER/MESH/RB_TOOL/rb_tool -a -c1 -i0 -z0 -s 10.0.1.1 -t 10.0.1.3 -r 12
# ue2 -> enb
    $OPENAIR2_DIR/NAS/DRIVER/MESH/RB_TOOL/rb_tool -a -c1 -i2 -z0 -s 10.0.3.3 -t 10.0.3.1 -r 1
fi    

if [ $1 -ge 3 ]; then 
    echo "bring up oai3 interface for ue 3"
    sudo ifconfig oai3 10.0.4.4 netmask 255.255.255.0 broadcast 10.0.4.255
    
# enb -> ue2
    $OPENAIR2_DIR/NAS/DRIVER/MESH/RB_TOOL/rb_tool -a -c2 -i0 -z0 -s 10.0.1.1 -t 10.0.1.4 -r 23
# ue2 -> enb
    $OPENAIR2_DIR/NAS/DRIVER/MESH/RB_TOOL/rb_tool -a -c2 -i3 -z0 -s 10.0.4.4 -t 10.0.4.1 -r 1
fi
    
if [ $1 -ge 4 ]; then 
    
    echo "add oai interfaces for more UEs here"
    
fi

echo "start the emulation with 1eNB and 2 UE"
echo "$OPENAIR_TARGETS/SIMU/USER/oaisim -c40 > /dev/null"
$OPENAIR_TARGETS/SIMU/USER/oaisim -c40 > /dev/null 


echo "End"
