#!/bin/bash
################################################################################
# Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The OpenAirInterface Software Alliance licenses this file to You under
# the OAI Public License, Version 1.1  (the "License"); you may not use this file
# except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.openairinterface.org/?page_id=698
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#-------------------------------------------------------------------------------
# For more information about the OpenAirInterface (OAI) Software Alliance:
#      contact@openairinterface.org
################################################################################
# file build_vpp_upf
# brief
# author Rohan Kharade
# company Eurecom
# email: rohan.kaharde@openairinterface.org
#

set -o pipefail

INSTALL_DIR=/usr/local/bin
################################
# include helper functions
################################
THIS_SCRIPT_PATH=$(dirname $(readlink -f $0))
source $THIS_SCRIPT_PATH/build_helper.upf


function help()
{
  echo_error " "
  echo_error "Usage: build_vpp_upf [OPTION]..."
  echo_error "Build the VPP executable with Travelping upf plugin."
  echo_error " "
  echo_error "Options:"
  echo_error "Mandatory arguments to long options are mandatory for short options too."
#  echo_error "  -b, --build-type                          Build type as defined in Makefile, allowed values are: Debug Release"
  echo_error "  -c, --clean                               Clean the build generated files: config, object, executable files (build from scratch)"
  echo_error "  -f, --force                               No interactive script for installation of software packages."
  echo_error "  -h, --help                                Print this help."
  echo_error "  -I, --install-deps                        Check installed software necessary to build and run VPP UPF (support $SUPPORTED_DISTRO)."
  echo_error "  -V, --verbose                             Build process verbose."
  echo_error "  -w, --wipe                                Wipe out vpp source directory."
  echo_error " "
}

function main()
{
  local -i clean=0
  local -i force=0
  local -i debug=0
  local -i verbose=0
  local -i wipe=0
  local -i var_check_install_min_deps=0
  local -i var_check_install_deps=0
  local    cmake_args=" "
  export   make_args=" "


  until [ -z "$1" ]
    do
    case "$1" in
      -a | --auto-test)
        cmake_args="$cmake_args -DSGW_AUTOTEST=1"
        shift;
        ;;
      -b | --build-type)
        list_include_item "Debug Release" $2
        [[ $? -ne 0 ]] && echo_error "Build type $2 not recognized" && return $?
        cmake_args="$cmake_args -DCMAKE_BUILD_TYPE=$2"
        list_include_item "Debug" $2
        [[ $? -ne 0 ]] && debug=1
        shift 2;
        ;;
      -c | --clean)
        clean=1
        echo "Clean the build generated files (build from scratch)"
        shift;
        ;;
      -f | --force)
        force=1
        echo "Force set (no interactive)"
        shift;
        ;;
      -h | --help)
        help
        shift;
        return 0
        ;;
      -I | --install-deps)
        echo "Check installed software necessary to build and run VPP UPF (support $SUPPORTED_DISTRO):"
        set_openair_env
        var_check_install_deps=1
        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;
        ;;
      -V | --Verbose)
        echo "CMake build process verbose"
        verbose=1
        shift;
        ;;
      -w | --wipe)
        echo "Wipe out vpp source"
        wipe=1
        shift;
        ;;	
      *)
        echo "Unknown option $1"
        help
        return 1
        ;;
    esac
  done


  if [ ! -d /usr/local/etc/oai ]; then
    $SUDO mkdir -m 777 -p /usr/local/etc/oai
  fi

  set_openair_env

  local dlog=$OPENAIRCN_DIR/build/log
  local dext=$OPENAIRCN_DIR/build/ext

  mkdir -m 777 -p $dlog
  mkdir -m 777 -p $dext

  if [ $var_check_install_deps -gt 0 ];then
    disable_ipv6
    check_install_vpp_upf_deps  $force $debug
    if [[ $? -ne 0 ]]; then
        echo_error "Error: VPP UPF deps installation failed"
        return 1
    else
        echo_success "VPP UPF deps installation successful"
        echo_warning "VPP UPF not compiled, to compile it, re-run ./build_vpp_upf without -I option"
        return 0
    fi
  fi

  cmake_args="$cmake_args  -DBUILD_SHARED_LIBS=OFF"
  ##############################################################################
  # Wipe
  ##############################################################################
  cd $OPENAIRCN_DIR/
  if [ $wipe -ne 0 ]; then
     remove_vpp_source
  fi
  ##############################################################################
  # Clean
  ##############################################################################
  cd $OPENAIRCN_DIR/
  if [ $clean -ne 0 ]; then
    if [[ $verbose -eq 1 ]]; then
      echo_warning "Cleaning VPP_UPF: generated configuration files, obj files, executable"
    fi
    make wipe -C $OPENAIRCN_DIR/vpp  2>&1
  fi

  ##############################################################################
  # Compile VPP_UPF
  ##############################################################################
  cd $OPENAIRCN_DIR/
  if [ ! -d ./build ]; then
    mkdir -m 777 -p -v build
  fi

  cd ./vpp
  add_Travelping_upf_plugin
  compilations vpp vpp $OPENAIRCN_DIR/vpp $verbose
  vpp_upf_init
  ret=$?;[[ $ret -ne 0 ]] && return $ret

  return 0
}


main "$@"
