#!/usr/bin/env bash

set -o xtrace

if [  -e <%= @ovs_dir %>/BUILD_COMPLETE ]; then
   echo "Build already done."
   cd <%= @ovs_dir %>
   sudo make install
else
   cd <%= @ovs_dpdk_dir %>
   make config T=<%= @rte_target %>

   if [ -e <%= @ovs_dpdk_dir %>/<%= @rte_target %> ]; then
      rm <%= @rte_target %>
   fi

   # block of code with more strict error handling
   set -o errexit
   ln -s -f build <%= @rte_target %>
   sed "s/CONFIG_RTE_BUILD_COMBINE_LIBS=n/CONFIG_RTE_BUILD_COMBINE_LIBS=y/" -i <%= @ovs_dpdk_dir %>/build/.config
   sed "s/CONFIG_RTE_MAX_MEMSEG=.*$/CONFIG_RTE_MAX_MEMSEG=<%= @ovs_dpdk_mem_segments %>/" -i <%= @ovs_dpdk_dir %>/build/.config
   sed "s/CONFIG_RTE_LIBRTE_VHOST=.*$/CONFIG_RTE_LIBRTE_VHOST=<%= @ovs_dpdk_rte_librte_vhost %>/" -i <%= @ovs_dpdk_dir %>/build/.config
   sed "s/CONFIG_RTE_LIBRTE_VHOST_DEBUG=.*$/CONFIG_RTE_LIBRTE_VHOST_DEBUG=<%= @ovs_dpdk_vhost_user_debug %>/" -i <%= @ovs_dpdk_dir %>/build/.config
   make -j `nproc` EXTRA_CFLAGS='-fPIC'
   sudo cp <%= @ovs_dpdk_dir %>/build/lib/*dpdk.* /lib
   cd <%= @ovs_dir %>
   ./boot.sh
   ./configure --with-dpdk=<%= @ovs_dpdk_dir %>/<%= @rte_target %> --prefix=/usr --with-dbdir=<%= @ovs_db_conf_dir %> --with-rundir=<%= @ovs_db_socket_dir %>
   make -j `nproc` CFLAGS='-O3 -march=native -fPIC'
   touch <%= @ovs_dir %>/BUILD_COMPLETE
   sudo make install
   set +o errexit
fi


set +o xtrace
