################################################################################
# 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_helper.amf
# brief
#
#######################################
################################
# include helper functions
################################
SCRIPT=$(readlink -f ${BASH_SOURCE})
THIS_SCRIPT_PATH=`dirname $SCRIPT`
source $THIS_SCRIPT_PATH/build_helper
source $THIS_SCRIPT_PATH/build_helper.fb_folly

#-------------------------------------------------------------------------------
#arg1 is force (0 or 1) (no interactive script)
#arg2 is debug (0 or 1) (install debug libraries)
install_fmt() {
  if [ $1 -eq 0 ]; then
    read -p "Do you want to install open-source formatting library for C++ ? <y/N> " prompt
    OPTION=""
  else
    prompt='y'
    OPTION="-y"
  fi

  echo "Install fmt from source"

  if [[ $prompt =~ [yY](es)* ]]
  then
    cd /tmp
    echo "Downloading fmt"
    $SUDO rm -rf /tmp/fmt*
    #git clone https://github.com/fmtlib/fmt.git
    git clone http://git.opensource5g.org/openxg/fmt.git
    ret=$?;[[ $ret -ne 0 ]] && return $ret
    cd fmt
    cmake -DFMT_TEST=FALSE  .
    ret=$?;[[ $ret -ne 0 ]] && return $ret
    make -j `nproc`
    ret=$?;[[ $ret -ne 0 ]] && return $ret
    $SUDO make install
    cd /tmp
    rm -rf /tmp/fmt*
  fi
  return 0
}

#-------------------------------------------------------------------------------
#arg1 is force (0 or 1) (no interactive script)
#arg2 is debug (0 or 1) (install debug libraries)
install_spdlog_from_git() {
  if [ $1 -eq 0 ]; then
    read -p "Do you want to install spdlog ? <y/N> " prompt
    OPTION=""
  else
    prompt='y'
    OPTION="-y"
  fi
  if [ $2 -eq 0 ]; then
    debug=0
  else
    debug=1
  fi


  if [[ $prompt =~ [yY](es)* ]]
  then
    #GIT_URL=https://github.com/gabime/spdlog.git
    GIT_URL=http://git.opensource5g.org/openxg/spdlog.git
    echo "Install spdlog from $GIT_URL"
    pushd $OPENAIRCN_DIR/build/ext
    echo "Downloading spdlog"
    if [[ $OPTION =~ -[yY](es)* ]]
    then
      $SUDO rm -rf spdlog
    fi

    git clone $GIT_URL
    cd spdlog && git checkout master
    ret=$?;[[ $ret -ne 0 ]] && popd && return $ret
    # enable syslog, Useless (defined in SMF code)
    sed -i '/#define SPDLOG_ENABLE_SYSLOG/s/^\/\///g' include/spdlog/tweakme.h
    popd
  fi
  return 0
}

#-------------------------------------------------------------------------------
#arg1 is force (0 or 1) (no interactive script)
#arg2 is debug (0 or 1) (install debug libraries)
install_pistache_from_git() {
  if [ $1 -eq 0 ]; then
    read -p "Do you want to install Pistache ? <y/N> " prompt
    OPTION="-y"
  else
    prompt='y'
    OPTION="-y"
  fi
  if [ $2 -eq 0 ]; then
    debug=0
  else
    debug=1
  fi


  if [[ $prompt =~ [yY](es)* ]]
  then
    #GIT_URL=https://github.com/oktal/pistache.git
    GIT_URL=http://git.opensource5g.org/openxg/pistache.git
    echo "Install Pistache from $GIT_URL"
    pushd $OPENAIRCN_DIR/build/ext
    echo "Downloading Pistache"
    if [[ $OPTION =~ -[yY](es)* ]]
    then
      $SUDO rm -rf pistache
    fi

    git clone $GIT_URL
    git submodule update --init
    cd pistache && git checkout e18ed9baeb2145af6f9ea41246cf48054ffd9907
    ret=$?;[[ $ret -ne 0 ]] && popd && return $ret
    mkdir _build && cd _build
    cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release ..
    ret=$?;[[ $ret -ne 0 ]] && popd && return $ret
    make
    ret=$?;[[ $ret -ne 0 ]] && popd && return $ret
    $SUDO make install
    ret=$?;[[ $ret -ne 0 ]] && popd && return $ret
    popd
  fi
  return 0
}

#-------------------------------------------------------------------------------
#arg1 is force (0 or 1) (no interactive script)
#arg2 is debug (0 or 1) (install debug libraries)
install_nlohmann_from_git() {
  if [ $1 -eq 0 ]; then
    read -p "Do you want to install Nlohmann Json ? <y/N> " prompt
    OPTION=""
  else
    prompt='y'
    OPTION="-y"
  fi
  if [ $2 -eq 0 ]; then
    debug=0
  else
    debug=1
  fi


  if [[ $prompt =~ [yY](es)* ]]
  then
    #GIT_URL=https://github.com/nlohmann/json.git
    GIT_URL=http://git.opensource5g.org/openxg/json.git
    echo "Install Nlohmann Json from $GIT_URL"
    pushd $OPENAIRCN_DIR/build/ext
    echo "Downloading Nlohmann"
    if [[ $OPTION =~ -[yY](es)* ]]
    then
      $SUDO rm -rf json
    fi

    git clone $GIT_URL
    git submodule update --init
    cd json && git checkout master
    ret=$?;[[ $ret -ne 0 ]] && popd && return $ret
    mkdir _build && cd _build
    cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DJSON_BuildTests=OFF ..
    ret=$?;[[ $ret -ne 0 ]] && popd && return $ret
    make
    ret=$?;[[ $ret -ne 0 ]] && popd && return $ret
    $SUDO make install
    ret=$?;[[ $ret -ne 0 ]] && popd && return $ret
    popd
  fi
  return 0
}

#-------------------------------------------------------------------------------
#arg1 is force (0 or 1) (no interactive script)
#arg2 is debug (0 or 1) (install debug libraries)
check_install_amf_deps(){

  if [ $1 -gt 0 ]; then
    OPTION="-y"
  else 
    OPTION=""
  fi
  if [ $2 -eq 0 ]; then
    debug=0
  else
    debug=1
  fi

  echo "Check supported distribution" 
  check_supported_distribution
  [[ $? -ne 0 ]] && return $?

  # prevent lock on /var/lib/dpkg/lock
  if [[ $OS_DISTRO == "ubuntu" ]]; then
    #$SUDO systemctl mask apt-daily.service
    #$SUDO systemctl mask apt-daily.timer
    #$SUDO systemctl mask apt-daily-upgrade.service
    #$SUDO systemctl mask apt-daily-upgrade.timer
    $SUDO sed -i 's/1/0/g' /etc/apt/apt.conf.d/10periodic
  fi

  update_package_db


  check_enable_epel_repos

  # Compilers, generators, ...
  if [[ $OS_DISTRO == "ubuntu" ]]; then
    PACKAGE_LIST="\
      autoconf \
      automake \
      bison \
      build-essential \
      cmake \
      daemon \
      doxygen \
      flex \
      gdb \
      git"
  elif [[ "$OS_BASEDISTRO" == "fedora" ]]; then
    PACKAGE_LIST="\
      autoconf \
      automake \
      bison \
      $CMAKE \
      doxygen \
      flex \
      gdb \
      git"
  else
    echo_fatal "$OS_DISTRO is not a supported distribution."
  fi
  echo "Install build tools"
  $SUDO $INSTALLER install $OPTION $PACKAGE_LIST
  ret=$?;[[ $ret -ne 0 ]] && return $ret

  # Libraries
  if [[ $OS_DISTRO == "ubuntu" ]]; then
    case "$(get_distribution_release)" in
      "ubuntu18.04")
      if [ $IS_CONTAINER -eq 0 ]; then
        specific_packages="libconfig++-dev libasio-dev libboost-all-dev mysql-server"
      else
        specific_packages="libconfig++-dev libasio-dev libboost-all-dev"
      fi
        ;;
    esac
    # removed libspdlog-dev
    PACKAGE_LIST="\
      $specific_packages \
      mysql-server \
      mysql-client \
      libmysqlclient-dev \
      libcpprest-dev \
      guile-2.0-dev \
      libcurl4-gnutls-dev \
      libevent-dev \
      libgcrypt11-dev \
      libgmp-dev \
      libhogweed? \
      libidn2-0-dev \
      libidn11-dev \
      libpthread-stubs0-dev \
      libsctp-dev \
      libssl-dev \
      libtool \
      libxml2 \
      libxml2-dev \
      openssl \
      python \
      nettle-dev \
      libcurl4 \
      net-tools \
      pkg-config"
  elif [[ "$OS_BASEDISTRO" == "fedora" ]]; then
    PACKAGE_LIST="\
      guile-devel \
      libconfig-devel \
      libgcrypt-devel \
      gmp-devel \
      libidn2-devel \
      libidn-devel \
      lksctp-tools \
      lksctp-tools-devel \
      openssl-devel \
      libtool \
      libxml2 \
      libxml2-devel \
      openssl \
      check \
      python \
      pkgconfig"
  else
    echo_fatal "$OS_DISTRO is not a supported distribution."
  fi
  echo "Install distro libs"
  $SUDO $INSTALLER install $OPTION $PACKAGE_LIST
  ret=$?;[[ $ret -ne 0 ]] && return $ret

  # Use fmt lib included in spdlog
  install_fmt $1
  ret=$?;[[ $ret -ne 0 ]] && return $ret

  install_spdlog_from_git $1 $2
  ret=$?;[[ $ret -ne 0 ]] && return $ret 

  install_fb_folly_from_source $1 $2
  ret=$?;[[ $ret -ne 0 ]] && return $ret

  install_pistache_from_git $1 $2
  ret=$?;[[ $ret -ne 0 ]] && return $ret

  install_nlohmann_from_git $1 $2
  ret=$?;[[ $ret -ne 0 ]] && return $ret 

  $SUDO ldconfig
  return 0

}
