#!/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_hss
# brief run script for HSS (!EXPERIMENTAL!).
# 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


function help()
{
  echo_error " "
  echo_error "Usage: run_hss [OPTION]..."
  echo_error "Run the HSS executable (experimental)."
  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."
}



function main() 
{
  local -i run_gdb=0
  local exe_arguments=" -c ./hss.conf"

  until [ -z "$1" ]
    do
    case "$1" in
      -g | --gdb)
        run_gdb=1
        echo "setting GDB flag to: $GDB"
        shift;
        ;;
      -h | --help)
        help
        exit 0
        ;;
      *)   
        echo "Unknown option $1"
        help
        exit 0
        ;;
    esac
  done

  check_for_root_rights

  set_openair_env 
  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 "OPENAIRCN_DIR   = $OPENAIRCN_DIR" $green
  cecho "OPENAIR_TARGETS = $OPENAIR_TARGETS" $green


  if [ ! -e $OPENAIR_TARGETS/bin/openair-hss ]; then
    echo_fatal "Cannot find $OPENAIR_TARGETS/bin/openair-hss executable"
  fi

  # for file config relative path
  cd $OPENAIR_TARGETS/bin

  if [ $run_gdb -eq 0 ]; then 
    exec $OPENAIR_TARGETS/bin/openair-hss  `echo $exe_arguments` 2>&1 
  else
    touch      ~/.gdb_hss
    chmod 777  ~/.gdb_hss
    echo "file $OPENAIR_TARGETS/bin/openair-hss" > ~/.gdb_hss
    echo "set args $exe_arguments   " >> ~/.gdb_hss
    echo "run"                        >> ~/.gdb_hss
    cat ~/.gdb_hss
    gdb -n -x ~/.gdb_hss 2>&1
  fi
}


main "$@"
