#!/bin/bash
################################################################################
#   OpenAirInterface
#   Copyright(c) 1999 - 2014 Eurecom
#
#    OpenAirInterface is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) anylater version.
#
#
#    OpenAirInterface is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with OpenAirInterface.The full GNU General Public License is
#    included in this distribution in the file called "COPYING". If not,
#    see <http://www.gnu.org/licenses/>.
#
#  Contact Information
#  OpenAirInterface Admin: openair_admin@eurecom.fr
#  OpenAirInterface Tech : openair_tech@eurecom.fr
#  OpenAirInterface Dev  : openair4g-devel@eurecom.fr
#
#  Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE
#
################################################################################
# file run_epc
# brief run script for EPC (MME+SGW-LITE).
# author  Lionel GAUTHIER
# company Eurecom
# email:  lionel.gauthier@eurecom.fr 


################################
# include helper functions
################################
THIS_SCRIPT_PATH=$(dirname $(readlink -f $0))
source $THIS_SCRIPT_PATH/build_helper
declare -i g_run_msc_gen=0
declare    g_msc_dir="/tmp"

set_openair_env 


function help()
{
  echo_error " "
  echo_error "Usage: run_epc [OPTION]..."
  echo_error "Run the EPC executable (MME+SGW-LITE)."
  echo_error " "
  echo_error "Options:"
  echo_error "Mandatory arguments to long options are mandatory for short options too."
  echo_error "  -g, --gdb                          Run with GDB."
  echo_error "  -h, --help                         Print this help."
  echo_error "  -K, --itti-dump-file               ITTI dump file containing all ITTI events occuring during EPC runtime."
  echo_error "  -l, --local-enb                    EPC run on the same eNB host."
  echo_error "  -m, --mscgen           directory   Generate mscgen output files in a directory"
  echo_error "  -v, --verbosity-level              Verbosity level (0,1,2)."
  echo_error "                                       0 -> ASN1 XER printf off"
  echo_error "                                       1 -> ASN1 XER printf on and ASN1 debug off"
  echo_error "                                       2 -> ASN1 XER printf on and ASN1 debug on"
}

function do_msc_gen()
{
  cd $g_msc_dir
  last_created_file=`ls -t mscgen* | head -1 | tr -d ':'`
  $OPENAIR_DIR/targets/SCRIPTS/msc_gen.py
  sync
  last_created_file2=`ls -t mscgen* | head -1 | tr -d ':'`
        
  if [ x"$last_created_file" != x"$last_created_file2" ]; then
    if [ -f ./$last_created_file2/oai_mscgen_page_0.png ]; then 
      command -v eog 2>/dev/null &&  eog ./$last_created_file2/oai_mscgen_page_0.png
    fi
  fi 

}

function control_c()
# run if user hits control-c
{
  echo_warning "\nExiting by ctrl+c\n"
  if [ $g_run_msc_gen -eq 1 ]; then 
    do_msc_gen
  fi
  exit $?
}

function main()
{
  local -i run_gdb=0
  local    exe_arguments=" "
  local -i epc_local=0
  local -r epc_default_config_file="$OPENAIR_TARGETS/bin/epc.conf"
  local    epc_config_file=$epc_default_config_file

  until [ -z "$1" ]
    do
    case "$1" in
      -g | --gdb)
        run_gdb=1
        echo "setting GDB flag to: $GDB"
        shift;
        ;;
      -h | --help)
        help
        exit 0
        ;;
      -K | --itti-dump-file)
        local itti_dump_file=$2
        # can omit file name if last arg on the line
        if [ "x$itti_dump_file" = "x" ]; then
          itti_dump_file="/tmp/epc_itti.log"
        else
          shift 1;
        fi
        echo "setting ITTI dump file to: $itti_dump_file"
        exe_arguments="$exe_arguments -K $itti_dump_file"
        shift 1;
        ;;     
      -l | --local-enb)
        epc_local=1
        shift;
        ;;
      -m | --mscgen)
        g_msc_dir=$2
        if [ -d  "$g_msc_dir" ]; then
          echo "setting mscgen log files to dir: $g_msc_dir"
          g_run_msc_gen=1
          shift 2;
        else
          echo_error "Mscgen log dir does not exist"
          exit -1
        fi
        ;;      
      -v | --verbosity-level)
        local verbosity_level=$2
        echo "setting verbosity level to: $verbosity_level"
        exe_arguments="-v $verbosity_level $exe_arguments"
        shift 2;
        ;;
      *)   
        echo "Unknown option $1"
        help
        exit 0
        ;;
    esac
  done

  set_openair_env 
  cecho "OPENAIR_DIR    = $OPENAIR_DIR" $green


  if [ ! -e $OPENAIR_DIR/targets/bin/mme_gw.Rel10 ]; then
    echo_fatal "Cannot find $OPENAIR_DIR/targets/bin/mme_gw.Rel10 executable, have a look at the output of build_epc executable"
  fi

  if [ $epc_local -eq 1 ]; then
    epc_config_file="$OPENAIR_DIR/targets/bin/epc.local.enb.conf"
  fi

  if [ $g_run_msc_gen -eq 1 ]; then 
    rm -f /tmp/openair.msc.*
  fi

  exe_arguments="-O $epc_config_file $exe_arguments"

  if [ $run_gdb -eq 0 ]; then 
    # trap keyboard interrupt (control-c)
    trap control_c SIGINT
    $OPENAIR_DIR/targets/bin/mme_gw.Rel10  `echo $exe_arguments` 2>&1 
  else
    # trap keyboard interrupt (control-c) is done by gdb
    touch      ~/.gdb_mme_gw
    chmod 777  ~/.gdb_mme_gw
    echo "file $OPENAIR_DIR/targets/bin/mme_gw.Rel10" > ~/.gdb_mme_gw
    echo "set args $exe_arguments "        >> ~/.gdb_mme_gw
    echo "run"                             >> ~/.gdb_mme_gw
    cat ~/.gdb_mme_gw
    gdb -n -x ~/.gdb_mme_gw
    if [ $g_run_msc_gen -eq 1 ]; then 
      do_msc_gen
    fi
  fi
}

sudo echo
is_sudo=$?
if [[ "$is_sudo" -ne 0 ]]; then
  echo_error "This script must be run by root or a sudo'er"
  echo
  exit 1
fi

main "$@"
