#!/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 build_hss
# brief
# 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: build_hss [OPTION]..."
  echo_error "Build the experimental HSS executable."
  echo_error " "
  echo_error "Options:"
  echo_error "Mandatory arguments to long options are mandatory for short options too."
  echo_error "  -b, --s6a-bind-addr-list       addr_list  Optionaly, s6a server bind addresses can be specified here"
  echo_error "  -c, --clean                               Clean the build generated files (build from scratch)"
  echo_error "  -d, --debug                               Compile with debug informations."
  echo_error "  -f, --fqdn                     fqdn       HSS Fully Qualified Domain Name (if not specified default is `hostname --fqdn`)."
  echo_error "  -h, --help                                Print this help."
  echo_error "  -i, --check-installed-software            Check installed software packages necessary to build and run HSS (support Ubuntu 14.04)."
  echo_error "  -I, --install-hss-files                   Install HSS database if necessary."
  echo_error "  -k, --operator-key             key        Operator key of the HSS."
  echo_error "                                            If not specified, default is 11111111111111111111111111111111 if operator ckey is not specified also."
  echo_error "  -K, --operator-ckey            ciph. key  Operator ciphered key of the HSS (Could be 8E27B6AF0E692E750F32667A3B14605D=OPC(11111111111111111111111111111111))."
  echo_error "                                            ( OPC()=RijndaelEncrypt(operator-key) Xor operator-key, LTE-K (prov database) scheduled in Rijndael algo before RijndaelEncrypt)."
  echo_error "                                            Note that you can have the RijndaelEncrypt() at http://extranet.cryptomathic.com/aescalc or"
  echo_error "                                                                                         at http://www.hanewin.net/encrypt/aes/aes-test.htm."
  echo_error "  -m, --connect-to-mme           fqdn       MME act as a S6A server, HSS as a client (reversed situation but allows MME and HSS be on the same host) ."
  echo_error "  -r, --realm                    realm      Realm of the HSS (optional parameter)."
  echo_error "  -s, --transport-sctp-only                 Diameter use SCTP (TCP disabled)."
  echo_error "  -t, --transport-tcp-only                  Diameter use TCP (SCTP disabled)."
  echo_error "  -T, --transport-prefer-tcp                Diameter prefer TCP."
  echo_error "  -v, --verbose                             Build process verbose."
}



function main()
{
  local -i clean=0
  local -i verbose=0
  local    cmake_args=" "
  local    make_args="-j $NUM_CPU"
  local    realm=""
  local    hss_hostname=""
  local    mme_hostname=""
  local    mme_ip=""
  local    mme_fqdn=""
  

  until [ -z "$1" ]; do
    case "$1" in
      -b | --s6a-bind-addr-list)
        echo "S6A server bind addresses: $2"
        cmake_args="$cmake_args -DFD_SERVER_IP_BIND_LIST=$2"
        shift 2;
        ;;
      -c | --clean)
        clean=1
        echo "Clean the build generated files (build from scratch)"
        shift;
        ;;
      -d | --debug)
        cmake_args="$cmake_args -DDEBUG=1"
        echo "Compile with debug informations"
        shift;
        ;;
      -f | --fqdn)
        echo "FQDN of the HSS: $2"
        cmake_args="$cmake_args -DHSS_FQDN=$2"
        shift 2;
        ;;
      -h | --help)
        help
        exit 0
        ;;
      -i | --check-installed-software)
        echo "Check installed software packages necessary to build and run HSS (support Ubuntu 14.04):"
        check_install_oai_software
        exit 0
        ;;
      -I | --install-hss-files)
        echo "Install HSS files: .conf files, database (if you want to reinstall database, drop it by hand before)."
        cmake_args="$cmake_args -DINSTALL_HSS_FILES=1"
        shift;
        ;;
      -k | --operator-key)
        echo "Operator key of the HSS: $2"
        cmake_args="$cmake_args -DOPERATOR_key=$2"
        shift 2;
        ;;
      -K | --operator-ckey)
        echo "Operator ciphered key of the HSS: $2"
        cmake_args="$cmake_args -DOPERATOR_ckey=$2"
        shift 2;
        ;;
      -m | --connect-to-mme)
        mme_fqdn=$2
        shift 2;
        ;;
      -r | --realm)
        echo "Realm of the HSS: $2"
        cmake_args="$cmake_args -DREALM=$2"
        shift 2;
        ;;
      -s | --transport-sctp-only)
        echo "Diameter use SCTP (TCP disabled), this is the default option."
        cmake_args="$cmake_args -DTRANSPORT_option=No_TCP"
        shift;
        ;;
      -t | --transport-tcp-only)
        echo "Diameter use TCP (SCTP disabled)."
        cmake_args="$cmake_args -DTRANSPORT_option=No_SCTP"
        shift;
        ;;
      -T | --transport-prefer-tcp)
        echo "Diameter prefer TCP (TCP, SCTP enabled)."
        cmake_args="$cmake_args -DTRANSPORT_PREFER_TCP_option=Prefer_TCP"
        shift;
        ;;
      -v | --verbose)
        echo "Make build process verbose"
        cmake_args="$cmake_args -DCMAKE_VERBOSE_MAKEFILE=ON"
        make_args="VERBOSE=1 $make_args"
        verbose=1
        shift;
        ;;
      *)   
        echo "Unknown option $1"
        help
        exit 1
        ;;
    esac
  done

  # extra arguments processing
  if [[ z$hss_fqdn = z ]]; then
    hss_fqdn=`hostname --fqdn`
    cmake_args="$cmake_args -DHSS_FQDN=$hss_fqdn"
    if [[ z$realm = z ]]; then
      realm=$hss_fqdn
      realm=${realm#*.}
      cmake_args="$cmake_args -DREALM=$realm"
    fi
  fi
  
  if [[ z$mme_fqdn != z ]]; then
    cmake_args="$cmake_args -DMME_FQDN=$mme_fqdn"
    mme_hostname=${mme_fqdn%%.*}
    cmake_args="$cmake_args -DMME_HOSTNAME=$mme_hostname"
  
    mme_ip=`resolveip --silent $mme_hostname`
    if [[ z$mme_ip = z ]]; then
      mme_ip=`resolveip --silent $mme_fqdn`
    fi
    if [[ z$mme_ip = z ]]; then
      echo_fatal "Unable to get MME IP addr of $mme_fqdn"
    fi
    cmake_args="$cmake_args -DHSS_CONNECT_TO_MME=1 -DMME_IP=$mme_ip"
  fi

  set_openair_env
  if [[ $verbose -eq 1 ]]; then
    cecho "OPENAIR_DIR    = $OPENAIR_DIR" $green
  fi
  
  if [[ z$OPENAIR_DIR = z ]]; then
    echo_fatal "OPENAIR_DIR env variable not set, exiting"
  fi


  local dbin=$OPENAIR_DIR/targets/bin
  local dlog=$OPENAIR_DIR/cmake_targets/log
  local dconf=OPENAIR_DIR/targets/bin
  
  mkdir -m 777 -p $dbin $dlog

  # for conf files copy in this bash script
  if [ -d /usr/lib/freeDiameter ]; then
    export FREEDIAMETER_PREFIX=/usr
  else
    if [ -d /usr/local/lib/freeDiameter ]; then
      export FREEDIAMETER_PREFIX=/usr/local
    else
      echo_fatal "FreeDiameter prefix not found, install freeDiameter if EPC, HSS"
    fi
  fi
  
 
  
  cmake_args="$cmake_args -DOPENAIR_DIR=$OPENAIR_DIR"

  rm -f $OPENAIR_DIR/targets/bin/openair-hss
  cd $OPENAIR_DIR/cmake_targets/hss_build
  if [ $clean -ne 0 ]; then
    if [[ $verbose -eq 1 ]]; then
      echo "Cleaning HSS"
    fi
    $SUDO rm -Rf build 2>&1
    if [[ $verbose -eq 1 ]]; then
      echo "Cleaning /usr/*/etc/freeDiameter"
    fi
    $SUDO rm -f /usr/local/etc/freeDiameter/hss* 2>&1
    $SUDO rm -f /usr/local/etc/freeDiameter/acl.conf 2>&1
    $SUDO rm -f /usr/etc/freeDiameter/hss* 2>&1
    $SUDO rm -f /usr/etc/freeDiameter/acl.conf 2>&1
    mkdir -m 777 -p -v build
  fi
  if [ ! -d ./build ]; then
    mkdir -m 777 -p -v build
  fi
  cd ./build
  cmake  $cmake_args ..
  make $make_args
  make install
  
  
  # bash doesn't like space char around = char
  cp -uv $OPENAIR_DIR/cmake_targets/hss_build/build/hss.conf $dbin
  $SUDO cp -uv $OPENAIR_DIR/cmake_targets/hss_build/build/hss_fd.conf $OPENAIR_DIR/cmake_targets/hss_build/build/acl.conf $FREEDIAMETER_PREFIX/etc/freeDiameter
  
}


main "$@"

