if [ "$VERBOSE" == "False" ]; then
    # allow local debugging
    set -o xtrace
fi

NETWORKING_OVS_DPDK_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )

#dpdk settings
OVS_DPDK_MODE=${OVS_DPDK_MODE:-''}
OVS_DPDK_GIT_REPO=${OVS_DPDK_GIT_REPO:-http://dpdk.org/git/dpdk}
OVS_DPDK_GIT_TAG=${OVS_DPDK_GIT_TAG:-v18.11}
OVS_DPDK_DIR=${OVS_DPDK_DIR:-$DEST/DPDK-$OVS_DPDK_GIT_TAG}
RTE_TARGET=${RTE_TARGET:-x86_64-native-linuxapp-gcc}
OVS_DPDK_MEM_SEGMENTS=${OVS_DPDK_MEM_SEGMENTS:-256}
OVS_DPDK_RTE_LIBRTE_VHOST=${OVS_DPDK_RTE_LIBRTE_VHOST:-True}
OVS_DPDK_VHOST_USER_DEBUG=${OVS_DPDK_VHOST_USER_DEBUG:-False}
OVS_DPDK_BUILD_SHARED_LIB=${OVS_DPDK_BUILD_SHARED_LIB:-False}
OVS_DPDK_CONFIG_RTE_LIBRTE_VHOST_NUMA=${OVS_DPDK_CONFIG_RTE_LIBRTE_VHOST_NUMA:-True}
OVS_DPDK_USE_QEMU_KVM_WRAPPER=${OVS_DPDK_USE_QEMU_KVM_WRAPPER:-False}
OVS_DPDK_UPDATE_CPU_DEDICATED_SET=${OVS_DPDK_UPDATE_CPU_DEDICATED_SET:-True}

# ovs constants
OVS_DB_CONF_DIR=/etc/openvswitch
OVS_DB_SOCKET_DIR=/var/run/openvswitch

#ovs settings
OVS_INIT_POLICY=${OVS_INIT_POLICY:-'auto'}
OVS_VSWITCHD_INIT=${OVS_VSWITCHD_INIT:-'auto'}
OVS_DIR=${OVS_DIR:-$DEST/ovs}
OVS_GIT_REPO=${OVS_GIT_REPO:-https://github.com/openvswitch/ovs.git}
OVS_GIT_TAG=${OVS_GIT_TAG:-'branch-2.11'}
OVS_HUGEPAGE_MOUNT=${OVS_HUGEPAGE_MOUNT:-/mnt/huge}
OVS_HUGEPAGE_MOUNT_PAGESIZE=${OVS_HUGEPAGE_MOUNT_PAGESIZE:-''}
OVS_NUM_HUGEPAGES=${OVS_NUM_HUGEPAGES:-2048}
OVS_SOCKET_MEM=${OVS_SOCKET_MEM:-auto}
OVS_MEM_CHANNELS=${OVS_MEM_CHANNELS:-4}
OVS_CORE_MASK=${OVS_CORE_MASK:-2}
OVS_PMD_CORE_MASK=${OVS_PMD_CORE_MASK:-4}
OVS_DB_SOCKET=$OVS_DB_SOCKET_DIR/db.sock
OVS_DB_CONF=$OVS_DB_CONF_DIR/conf.db
OVS_ALLOCATE_HUGEPAGES=${OVS_ALLOCATE_HUGEPAGES:-True}
OVS_LOG_DIR=${OVS_LOG_DIR:-/tmp}
OVS_LOCK_DIR=${OVS_LOCK_DIR:-''}
OVS_INTERFACE_DRIVER=${OVS_INTERFACE_DRIVER:-'igb_uio'}
OVS_BOND_PORTS=${OVS_BOND_PORTS:-''}
OVS_BOND_MODE=${OVS_BOND_MODE:-''}
OVS_BRIDGE_MAPPINGS=${OVS_BRIDGE_MAPPINGS:-''}
OVS_DPDK_PORT_MAPPINGS=${OVS_DPDK_PORT_MAPPINGS:-''}
OVS_DPDK_BIND_PORT=${OVS_DPDK_BIND_PORT:-True}
OVS_TUNNEL_CIDR_MAPPING=${OVS_TUNNEL_CIDR_MAPPING:-''}
OVS_PCI_MAPPINGS=${OVS_PCI_MAPPINGS:-''}
OVS_EMC_SIZE=${OVS_EMC_SIZE:=''}
OVS_VHOST_USER_SOCKET_DIR=${OVS_VHOST_USER_SOCKET_DIR:=''}

OVS_DPDK_SERVICE_DEBUG_OUTPUT=${OVS_DPDK_SERVICE_DEBUG_OUTPUT:-False}

if [[ ! -z "$OVS_TUNNEL_CIDR_MAPPING" ]]; then
    TUNNEL_ENDPOINT_IP=`echo $OVS_TUNNEL_CIDR_MAPPING | cut -f 2 -d ":" | cut -f 1 -d "/"`
fi

OVS_ENABLE_SG_FIREWALL_MULTICAST=${OVS_ENABLE_SG_FIREWALL_MULTICAST:-False}
OVS_MULTICAST_SNOOPING_AGING_TIME=${OVS_MULTICAST_SNOOPING_AGING_TIME:-3600}

#patch settings
#do not install any patches by default.
OVS_PATCHES=${OVS_PATCHES:-''}
OVS_DPDK_PATCHES=${OVS_DPDK_PATCHES:-''}

# adjust variables based on OVS_DPDK_MODE & OVS_DPDK_INSTALL settings
if [ "$OVS_DPDK_MODE" == "controller_ovs" ]; then
    OVS_DPDK_INSTALL=$(trueorfalse False OVS_DPDK_INSTALL)
elif [[ "$OVS_DPDK_MODE" == "compute"  || "$OVS_DPDK_MODE" == "controller_ovs_dpdk" ]]; then
    OVS_DPDK_INSTALL=$(trueorfalse True OVS_DPDK_INSTALL)
    OVS_DATAPATH_TYPE=${OVS_DATAPATH_TYPE:-netdev}
else
    #OVS_DPDK_MODE not configured or configured badly
    OVS_DPDK_INSTALL=$(trueorfalse True OVS_DPDK_INSTALL)
fi

# append ovs mechanism driver if needed
if [ "$OVS_DPDK_MODE" != "compute" ]; then
    arr=$(echo $Q_ML2_PLUGIN_MECHANISM_DRIVERS | tr "," "\n")
    ovs_found="False"
    for x in $arr; do
        if [ "$x" == "openvswitch" ]; then
            ovs_found="True"
        fi
    done

    if [ "$ovs_found" == "False" ]; then
        Q_ML2_PLUGIN_MECHANISM_DRIVERS=$Q_ML2_PLUGIN_MECHANISM_DRIVERS,openvswitch
    fi
fi

if [ "$VERBOSE" == "False" ]; then
    # turn off debugging again
    set +o xtrace
fi

