#!/bin/bash
echo "Bash version ${BASH_VERSION}..."

declare GENERATE_SEQ_DIAGRAM="no"
declare LOG_FILE_LTE="no"
#------------------------------------------------
declare MAKE_IP_DRIVER_TARGET="naslite_netlink_ether"
declare MAKE_LTE_ACCESS_STRATUM_TARGET="oaisim"
declare IP_DRIVER_NAME="oai_nw_drv"
#------------------------------------------------
declare -a NAS_IMEI=( 3 9 1 8 3 6 6 2 0 0 0 0 0 0 )
# first value is for ENB, then UE0, UE1, etc.
declare -a MARKING=( 1 2 3 4 5 6 7 8 9 10 11 12 14 15 16 )
# BE CAREFULL MAY NOT WORK WITH OTHER VALUES (TO DO)
declare -i NUM_ENB=1
#------------------------------------------------
# Notes:
# We would like to remove the ipv4 addresses swap in the IP driver code for oai virtualized.
# Let's say oai0 has ipv4 @ 10.0.1.1, oai1 has ipv4 @ 10.0.2.2 etc.
# In the past when we generated some local traffic on the computer and wanted to make it go
# through for example oai1 to oai0, the ipv4 destination address was 10.0.2.1
#     meaning 10.0.2.2 wants to send a paquet to 10.0.1.1,
#     so ip_destination[2] has the digit corresponding to the source
#     so ip_destination[3] has the digit corresponding to the destination
# then in the driver we modified at the receiving side (oai0 here) the ip source and destination
# addresses in this way:
# Originating packet:                               SRC 10.0.X.X -> 10.0.2.1
# Resulting modified packet at the receiving side:  SRC 10.0.1.2 -> 10.0.1.1
# So the packet could be accepted by the device (same dest @) and the fake source was was recorded (1.2).
# So the response to this packet could return to the interface by where the request was pulled.
# But this solution lead to recompute checksums of packets in the driver.
#
# So here we describe a solution where packets are not modified by the IP driver but by the kernel:
#
#        +---------------+         +---------------+         +---------------+
#        |APP "VIRTUALLY"|         |APP "VIRTUALLY"|         |APP "VIRTUALLY"|   etc
#        |running on eNB |         |running on UE0 |         |running on UE1 |
#        |sending traffic|         |               |         |               |
#        | to UE 1       |         |               |         |               |
#        +---------------+         +---------------+         +---------------+
#           |        ^                |        ^                |        ^
#           |        |                |        |                |        |
#    Tx packet (n1)  |                |        |        Tx packet (n2)   |
#    XXXX->10.0.1.3  |                |        |         XXXX->10.0.1.3  |
#           |        |                |        |                |        |
#           | Rx packet (n2)          |        |                | Rx packet (n1)
#           | 10.0.1.3->10.0.1.1      |        |                | 10.0.3.1->10.0.3.3
#           V        |                V        |                V        |
#        +-----------------------------------------------------------------------------+
#        |  IPTABLES Table mangle chain OUTPUT TARGET MARK OPERATION setmark           |
#        |  On packets coming from applications                                        |
#        |-----------------------------------------------------------------------------|
#        |  Example for packet n1:                                                     |
#        |  iptables -t mangle -A OUTPUT --dst 10.0.1.3 -j MARK --set-mark 0x0103      |
#        +-----------------------------------------------------------------------------+
#           |        |                |        |                |        |
#    Tx packet (n1)  |                |        |                |        |
#    XXXX->10.0.1.3  |                |        |                |        |
#    mark 0x0103     |                |        |                |        |
#           |        |                |        |                |        |
#           V        |                V        |                V        |
#        +-----------------------------------------------------------------------------+
#        |  IPTABLES Table nat chain POSTROUTING TARGET SNAT OPERATION --to Fake IP SRC|
#        |  On packets coming from applications                                        |
#        |-----------------------------------------------------------------------------|
#        |  Example for packet n1:                                                     |
#        |  iptables -t nat -A POSTROUTING -m mark --mark 0x0103 -j SNAT --to 10.0.3.1 |
#        +-----------------------------------------------------------------------------+
#           |        |                |        |                |        |
#           |        |                |        |                | Rx packet (n1)
#           |        |                |        |                | 10.0.3.1->10.0.3.3
#           |        |                |        |                | mark 0x0103
#           |        |                |        |                |        |
#        +-----------------------------------------------------------------------------+
#        |  IPTABLES Table mangle chain PREROUTING TARGET MARK OPERATION setmark       |
#        |  On packets coming from applications                                        |
#        |-----------------------------------------------------------------------------|
#        |  Example for packet n1:                                                     |
#        |  iptables -t nat -A PREROUTING -m mark --mark 0x0103 -j DNAT --to 10.0.3.3  |
#        +-----------------------------------------------------------------------------+
#           |        ^                |        ^                |        ^
#           |        |                |        |                |        |
#    Tx packet (n1)  |                |        |                | Rx packet (n1)
#    10.0.3.1->10.0.1.3               |        |                | 10.0.3.1->10.0.1.3
#    mark 0x0103     |                |        |                | mark 0x0103
#           |        |                |        |                |        |
#        +-----------------------------------------------------------------------------+
#        |  IPTABLES Table mangle chain PREROUTING TARGET MARK OPERATION setmark       |
#        |  On packets coming from applications                                        |
#        |-----------------------------------------------------------------------------|
#        |  Example for packet n1:                                                     |
#        |  iptables -t mangle -A PREROUTING --dst 10.0.1.3 -j MARK --set-mark 0x0103  |
#        +-----------------------------------------------------------------------------+
#           |        ^                |        ^                |        ^
#           |        |                |        |                |   Rx packet (n1)
#           |        |                |        |                |   10.0.3.1->10.0.1.3
#           |        |                |        |                |        |
#           |10.0.1.1|                |10.0.2.2|                |10.0.3.3|
#           V        |                V        |                V        |
#          +----------+              +----------+              +----------+
#          |  oai0    |              | oai1     |              | oai2     |
#          |  (eNB)   |              | (UE0)    |              | (UE1)    |
#          +----------+              +----------+              +----------+

####################################################################################
#  HERE IS A DISPLAY OF THE CONTENT OF THE NAT TABLE FOR THE DESCRIBED SETTING
####################################################################################
# Chain PREROUTING (policy ACCEPT 1108 packets, 242K bytes)
#  pkts bytes target     prot opt in     out     source               destination
#     0     0 DNAT       all  --  *      *       0.0.0.0/0            0.0.0.0/0           mark match 0x102 to:10.0.2.2
#     0     0 DNAT       all  --  *      *       0.0.0.0/0            0.0.0.0/0           mark match 0x103 to:10.0.3.3
#     0     0 DNAT       all  --  *      *       0.0.0.0/0            0.0.0.0/0           mark match 0x201 to:10.0.1.1
#     0     0 DNAT       all  --  *      *       0.0.0.0/0            0.0.0.0/0           mark match 0x203 to:10.0.3.3
#     0     0 DNAT       all  --  *      *       0.0.0.0/0            0.0.0.0/0           mark match 0x301 to:10.0.1.1
#     0     0 DNAT       all  --  *      *       0.0.0.0/0            0.0.0.0/0           mark match 0x302 to:10.0.2.2
#
# Chain POSTROUTING (policy ACCEPT 2118 packets, 175K bytes)
#  pkts bytes target     prot opt in     out     source               destination
#     0     0 SNAT       all  --  *      *       0.0.0.0/0            0.0.0.0/0           mark match 0x102 to:10.0.2.1
#     0     0 SNAT       all  --  *      *       0.0.0.0/0            0.0.0.0/0           mark match 0x103 to:10.0.3.1
#     0     0 SNAT       all  --  *      *       0.0.0.0/0            0.0.0.0/0           mark match 0x201 to:10.0.1.2
#     0     0 SNAT       all  --  *      *       0.0.0.0/0            0.0.0.0/0           mark match 0x203 to:10.0.3.2
#     0     0 SNAT       all  --  *      *       0.0.0.0/0            0.0.0.0/0           mark match 0x301 to:10.0.1.3
#     0     0 SNAT       all  --  *      *       0.0.0.0/0            0.0.0.0/0           mark match 0x302 to:10.0.2.3
#
# Chain OUTPUT (policy ACCEPT 2133 packets, 186K bytes)
#  pkts bytes target     prot opt in     out     source               destination
####################################################################################
#  HERE IS A DISPLAY OF THE CONTENT OF THE MANGLE TABLE FOR THE DESCRIBED SETTING
####################################################################################
#root@hades:~# iptables -t mangle -nvL
# Chain PREROUTING (policy ACCEPT 2008K packets, 316M bytes)
#  pkts bytes target     prot opt in     out     source               destination
#     0     0 MARK       all  --  *      *       0.0.0.0/0            10.0.1.2            MARK xset 0x102/0xffffffff
#     0     0 MARK       all  --  *      *       0.0.0.0/0            10.0.1.3            MARK xset 0x103/0xffffffff
#     0     0 MARK       all  --  *      *       0.0.0.0/0            10.0.2.1            MARK xset 0x201/0xffffffff
#     0     0 MARK       all  --  *      *       0.0.0.0/0            10.0.2.3            MARK xset 0x203/0xffffffff
#     0     0 MARK       all  --  *      *       0.0.0.0/0            10.0.3.1            MARK xset 0x301/0xffffffff
#     0     0 MARK       all  --  *      *       0.0.0.0/0            10.0.3.2            MARK xset 0x302/0xffffffff
#
# Chain INPUT (policy ACCEPT 2008K packets, 316M bytes)
#  pkts bytes target     prot opt in     out     source               destination
#
# Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
#  pkts bytes target     prot opt in     out     source               destination
#
# Chain OUTPUT (policy ACCEPT 1989K packets, 331M bytes)
#  pkts bytes target     prot opt in     out     source               destination
#     0     0 MARK       all  --  *      *       0.0.0.0/0            10.0.1.2            MARK xset 0x102/0xffffffff
#     0     0 MARK       all  --  *      *       0.0.0.0/0            10.0.1.3            MARK xset 0x103/0xffffffff
#     0     0 MARK       all  --  *      *       0.0.0.0/0            10.0.2.1            MARK xset 0x201/0xffffffff
#     0     0 MARK       all  --  *      *       0.0.0.0/0            10.0.2.3            MARK xset 0x203/0xffffffff
#     0     0 MARK       all  --  *      *       0.0.0.0/0            10.0.3.1            MARK xset 0x301/0xffffffff
#     0     0 MARK       all  --  *      *       0.0.0.0/0            10.0.3.2            MARK xset 0x302/0xffffffff
#
# Chain POSTROUTING (policy ACCEPT 1990K packets, 331M bytes)
#  pkts bytes target     prot opt in     out     source               destination
#
####################################################################################
# We also avoid the manipulation of ipv4 ARP packets by setting ourselves the neighbour cache:
####################################################################################
#root@hades:~# ip neigh show
#...
#10.0.3.1 dev oai1 lladdr 00:39:18:36:62:00 PERMANENT
#10.0.2.1 dev oai1 lladdr 00:39:18:36:62:00 PERMANENT
#10.0.1.1 dev oai1 lladdr 00:39:18:36:62:00 PERMANENT
#...
#10.0.1.2 dev oai0 lladdr 00:39:18:36:62:01 PERMANENT
#10.0.2.2 dev oai0 lladdr 00:39:18:36:62:01 PERMANENT
#10.0.3.2 dev oai0 lladdr 00:39:18:36:62:01 PERMANENT
#...
#10.0.1.3 dev oai0 lladdr 00:39:18:36:62:02 PERMANENT
#10.0.2.3 dev oai0 lladdr 00:39:18:36:62:02 PERMANENT
#10.0.3.3 dev oai0 lladdr 00:39:18:36:62:02 PERMANENT

#

###########################################################
IPTABLES=/sbin/iptables
THIS_SCRIPT_PATH=$(dirname $(readlink -f $0))
declare -x OPENAIR_DIR=""
declare -x OPENAIR1_DIR=""
declare -x OPENAIR2_DIR=""
declare -x OPENAIR3_DIR=""
declare -x OPENAIR_TARGETS=""
###########################################################


black='\E[30m'
red='\E[31m'
green='\E[32m'
yellow='\E[33m'
blue='\E[34m'
magenta='\E[35m'
cyan='\E[36m'
white='\E[37m'

ROOT_UID=0
E_NOTROOT=67


cecho()   # Color-echo
          # arg1 = message
          # arg2 = color
{
local default_msg="No Message."
    message=${1:-$default_msg}
    color=${2:-$black}
    echo -e "$color"
    echo -n "$message"
    tput sgr0
    echo
    return
}

echo_error() {
local my_string=""
  until [ -z "$1" ]
  do
    my_string="$my_string$1"
    shift
  done
  cecho "$my_string" $red
}

echo_warning() {
local my_string=""
  until [ -z "$1" ]
  do
    my_string="$my_string$1"
    shift
  done
  cecho "$my_string" $yellow
}

echo_success() {
local my_string=""
  until [ -z "$1" ]
  do
    my_string="$my_string$1"
    shift
  done
  cecho "$my_string" $green
}

set_openair() {
    path=`pwd`
    declare -i length_path
    declare -i index
    length_path=${#path}

    index=`echo $path | grep -b -o 'targets' | cut -d: -f1`
    #echo ${path%$token*}
    if [[ $index -lt $length_path  && index -gt 0 ]]
       then
           declare -x OPENAIR_DIR
           index=`expr $index - 1`
           openair_path=`echo $path | cut -c1-$index`
           #openair_path=`echo ${path:0:$index}`
           export OPENAIR_DIR=$openair_path
           export OPENAIR1_DIR=$openair_path/openair1
           export OPENAIR2_DIR=$openair_path/openair2
           export OPENAIR3_DIR=$openair_path/openair3
           export OPENAIR_TARGETS=$openair_path/targets
           return 0
    fi
    return -1
}

bash_exec() {
  output=$($1 2>&1)
  result=$?
  if [ $result -eq 0 ]
     then
        echo_success "$1"
     else
        echo_error "$1: $output"
  fi
}

wait_process_started () {
  if  [ -z "$1" ]
  then
    echo_error "WAITING FOR PROCESS START: NO PROCESS"
    return 1
  fi
  ps -C $1 > /dev/null 2>&1
  while [ $? -ne 0 ]; do
    echo_warning "WAITING FOR $1 START"
    sleep 2
    ps -C $1 > /dev/null 2>&1
  done
  echo_success "PROCESS $1 STARTED"
  return 0
}

assert() {
  # If condition false
  # exit from script with error message
  E_PARAM_ERR=98
  E_PARAM_FAILED=99
  
  if [ -z "$2" ] # Not enought parameters passed.
  then
    return $E_PARAM_ERR
  fi
  
  lineno=$2
  if [ ! $1 ]
  then
    echo "Assertion failed:  \"$1\""
    echo "File \"$0\", line $lineno"
    exit $E_ASSERT_FAILED
  fi
}

ctrl_c() {
  bash_exec "pkill oaisim"
  bash_exec "ip link set $LTEIF down"

  bash_exec "rmmod $IP_DRIVER_NAME"

  bash_exec "$IPTABLES -P INPUT ACCEPT"
  bash_exec "$IPTABLES -F INPUT"
  bash_exec "$IPTABLES -P OUTPUT ACCEPT"
  bash_exec "$IPTABLES -F OUTPUT"
  bash_exec "$IPTABLES -P FORWARD ACCEPT"
  bash_exec "$IPTABLES -F FORWARD"
  bash_exec "$IPTABLES -t nat    -F"
  bash_exec "$IPTABLES -t mangle -F"
  bash_exec "$IPTABLES -t filter -F"
  bash_exec "ip route flush cache"
}


set_openair
cecho "OPENAIR_DIR     = $OPENAIR_DIR" $green
cecho "OPENAIR1_DIR    = $OPENAIR1_DIR" $green
cecho "OPENAIR2_DIR    = $OPENAIR2_DIR" $green
cecho "OPENAIR3_DIR    = $OPENAIR3_DIR" $green
cecho "OPENAIR_TARGETS = $OPENAIR_TARGETS" $green


declare -i NUM_MOBILES
declare -i INSTANCE
declare -i IP_SOURCE
declare -i IP_DEST
declare -i IP
declare -i RB_ID
declare -i CLASSIFIER_ID


trap ctrl_c INT

bash_exec "ip route flush cache"
bash_exec "$IPTABLES -P INPUT ACCEPT"
bash_exec "$IPTABLES -F INPUT"
bash_exec "$IPTABLES -P OUTPUT ACCEPT"
bash_exec "$IPTABLES -F OUTPUT"
bash_exec "$IPTABLES -P FORWARD DROP"
bash_exec "$IPTABLES -F FORWARD"
bash_exec "$IPTABLES -t nat    -F"
bash_exec "$IPTABLES -t mangle -F"
bash_exec "$IPTABLES -t filter -F"


NUM_MOBILES=$1

for ((i=0 ; i < `expr $NUM_MOBILES + $NUM_ENB`; i++ ))
do
    TEST_OAI=`/sbin/ifconfig | grep oai$i | awk '{print $1}'`
    if [ "$TEST_OAI" = oai"$i"x ]; then
        bash_exec "ip link set oai$i down"
    fi
done



echo "Bringup OAI network interfaces"

bash_exec "rmmod $IP_DRIVER_NAME"
cecho "make $MAKE_IP_DRIVER_TARGET $MAKE_LTE_ACCESS_STRATUM_TARGET....." $green
bash_exec "make $MAKE_IP_DRIVER_TARGET $MAKE_LTE_ACCESS_STRATUM_TARGET"
bash_exec "insmod  $OPENAIR2_DIR/NAS/DRIVER/LITE/$IP_DRIVER_NAME.ko oai_nw_drv_IMEI=${NAS_IMEI[0]},${NAS_IMEI[1]},${NAS_IMEI[2]},${NAS_IMEI[3]},${NAS_IMEI[4]},${NAS_IMEI[5]},${NAS_IMEI[6]},${NAS_IMEI[7]},${NAS_IMEI[8]},${NAS_IMEI[9]},${NAS_IMEI[10]},${NAS_IMEI[11]},${NAS_IMEI[12]},${NAS_IMEI[13]}"


NUM_MOBILES=$1
IP=1

########################
#                      #
#  CONFIGURE OAI IF    #
#                      #
########################
for ((i=0 ; i < `expr $NUM_MOBILES + $NUM_ENB`; i++ ))
do
    bash_exec "ip link set oai$i broadcast ff:ff:ff:ff:ff:ff"
    bash_exec "ip link set oai$i up"
    sleep 1

    bash_exec "ifconfig oai$i 10.0.$IP.$IP netmask 255.255.255.0 broadcast 10.0.$IP.255"

    sleep 1
    bash_exec "ip addr add dev oai$i 2001:$IP::$IP/64"

    # avoid arp in virtualization
    for ((j=0 ; j <= $NUM_MOBILES ; j++ ))
    do
        if [[ $i -ne $j ]]
        then
            bash_exec "ip neighbour add to 10.0.$IP.$IP lladdr 00:${NAS_IMEI[0]}${NAS_IMEI[1]}:${NAS_IMEI[2]}${NAS_IMEI[3]}:${NAS_IMEI[4]}${NAS_IMEI[5]}:${NAS_IMEI[6]}${NAS_IMEI[7]}:${NAS_IMEI[8]}$i nud permanent dev oai$j"
        fi
    done
    let IP=IP+1
done

for ((src=0 ; src < `expr $NUM_ENB + $NUM_MOBILES` ; src++))
do
    let IP_SRC=$src+1
    for ((dst=0 ; dst < `expr $NUM_ENB + $NUM_MOBILES` ; dst++))
    do
        if [[ $src -ne $dst ]]
        then
            let IP_DST=$dst+1
            let marksrc=${MARKING[$src]}
            marksrc16=`printf '%02x' $marksrc`
            let markdst=${MARKING[$dst]}
            markdst16=`printf '%02x' $markdst`
            bash_exec "iptables -t mangle -A OUTPUT --dst 10.0.$IP_SRC.$IP_DST -j MARK --set-mark 0x$marksrc16$markdst16"
            bash_exec "iptables -t nat -A POSTROUTING -m mark --mark 0x$marksrc16$markdst16 -j SNAT --to 10.0.$IP_DST.$IP_SRC"

            # we can use same mark since not same path (PREROUTING)
            bash_exec "iptables -t mangle -A PREROUTING --dst 10.0.$IP_SRC.$IP_DST -j MARK --set-mark 0x$marksrc16$markdst16"
            bash_exec "iptables -t nat -A PREROUTING      -m mark --mark 0x$marksrc16$markdst16 -j DNAT --to 10.0.$IP_DST.$IP_DST"
        fi
    done
done

echo "########################################################################################"
echo "#          IPTABLES MANGLE                                                             #"
echo "########################################################################################"
iptables -t mangle -nvL
echo "########################################################################################"
echo "#          IPTABLES NAT                                                             #"
echo "########################################################################################"
iptables -t nat -nvL

########################
#                      #
#  CONFIGURE RABS      #
#                      #
########################
# Loop Cx 0 -> NUM_MOBILES - 1 for configuring default RABS on eNB
CLASSIFIER_ID=4
for ((instance=0 ; instance < $NUM_ENB ; instance++ ))
do
    let IP_SRC=instance+1
    for ((cx=0 ; cx < $NUM_MOBILES ; cx++ ))
    do
        let IP_DEST=cx+2
        if [[ $IP_SRC -ne $IP_DEST ]]
        then
            # ON eNB
            let RB_ID=(cx*8)+3
            bash_exec "$OPENAIR2_DIR/NAS/DRIVER/LITE/RB_TOOL/rb_tool -a -c$cx -i$instance -f$CLASSIFIER_ID -z0 -s 10.0.$IP_DEST.$IP_SRC/32   -t 10.0.$IP_SRC.$IP_DEST/32   -r $RB_ID"
            let CLASSIFIER_ID=CLASSIFIER_ID+2

            # ON UE
            let IP_SOURCE=instance+1
            let instance_ue=cx+$NUM_ENB
            RB_ID=3
            bash_exec "$OPENAIR2_DIR/NAS/DRIVER/LITE/RB_TOOL/rb_tool -a -c$instance -i$instance_ue -f$CLASSIFIER_ID -z0 -s 10.0.$IP_SRC.$IP_DEST/32 -t 10.0.$IP_DEST.$IP_SRC/32      -r $RB_ID"
            let CLASSIFIER_ID=CLASSIFIER_ID+2

            # ON eNB
            bash_exec "ip neighbour add to 10.0.$IP_SRC.$IP_DEST lladdr 00:${NAS_IMEI[0]}${NAS_IMEI[1]}:${NAS_IMEI[2]}${NAS_IMEI[3]}:${NAS_IMEI[4]}${NAS_IMEI[5]}:${NAS_IMEI[6]}${NAS_IMEI[7]}:${NAS_IMEI[8]}$instance_ue nud permanent dev oai$instance"

            # ON UE
            bash_exec "ip neighbour add to 10.0.$IP_DEST.$IP_SRC lladdr 00:${NAS_IMEI[0]}${NAS_IMEI[1]}:${NAS_IMEI[2]}${NAS_IMEI[3]}:${NAS_IMEI[4]}${NAS_IMEI[5]}:${NAS_IMEI[6]}${NAS_IMEI[7]}:${NAS_IMEI[8]}$instance nud permanent dev oai$instance_ue"
        fi
    done
done


IP=1
for ((i=0 ; i < `expr $NUM_MOBILES + $NUM_ENB`; i++ ))
do
    # avoid arp in virtualization
    for ((j=0 ; j <= $NUM_MOBILES ; j++ ))
    do
        if [[ $i -ne $j ]]
        then
            bash_exec "ip neighbour add to 10.0.$IP.$IP lladdr 00:${NAS_IMEI[0]}${NAS_IMEI[1]}:${NAS_IMEI[2]}${NAS_IMEI[3]}:${NAS_IMEI[4]}${NAS_IMEI[5]}:${NAS_IMEI[6]}${NAS_IMEI[7]}:${NAS_IMEI[8]}$i nud permanent dev oai$j"
        fi
    done
    let IP=IP+1
done

# echo "start the emulation with 1eNB and" $1 "UE"
if [ "$GENERATE_SEQ_DIAGRAM"x = yesx ]; then
    if [ -e /tmp/msc_log.txt ]; then
        bash_exec "rm -f /tmp/msc_log.txt"
    fi
    
    if [ "$LOG_FILE_LTE"x = yesx ]; then
        if [ -e /tmp/lte_log.txt ]; then
            bash_exec "rm -f /tmp/lte_log.txt"
        fi
        $OPENAIR_TARGETS/SIMU/USER/oaisim -a -u $1 > /tmp/lte_log.txt
        chmod 777 /tmp/lte_log.txt
        cat /tmp/lte_log.txt | grep MSC_ > /tmp/msc_log.txt
    else
        $OPENAIR_TARGETS/SIMU/USER/oaisim -a -u $1 | grep MSC_ > /tmp/msc_log.txt
    fi
    chmod 777 /tmp/msc_log.txt
    echo "End of emulation, now generating sequence diagrams..."
    $OPENAIR_TARGETS/SCRIPTS/msc_gen.py /tmp/msc_log.txt
else
    if [ "$LOG_FILE_LTE"x = yesx ]; then
        if [ -e /tmp/lte_log.txt ]; then
            bash_exec "rm -f /tmp/lte_log.txt"
        fi
        $OPENAIR_TARGETS/SIMU/USER/oaisim -a -u $1 > /tmp/lte_log.txt
        chmod 777 /tmp/lte_log.txt
    else
        $OPENAIR_TARGETS/SIMU/USER/oaisim -a -u $1 -F -P wireshark > /tmp/lte_log.txt
    fi
    echo "End of emulation"
fi


