#!/bin/bash
set -eux

template_dir="/usr/share/tripleo/templates"

BASE=$(dirname $0)/../
BRIDGE_SUFFIX=${1:-''} # support positional arg for legacy support
BRIDGE_NAMES='brbm'
VLAN_TRUNK_IDS=''

SCRIPT_NAME=$(basename $0)

function show_options {
    echo "Usage: $SCRIPT_NAME [-n num] [-b space delimited bridge names ]"
    echo
    echo "Setup libvirt networking and OVS bridges for TripleO."
    echo
    echo "    -n                -- Bridge number/suffix. Added to all bridges."
    echo "                         Useful when creating multiple environments"
    echo "                         on the same machine."
    echo "    -b                -- Space delimited list of baremetal bridge"
    echo "                         name(s). Defaults to brbm."
    echo
    exit 1
}

TEMP=$(getopt -o h,n:,b: -n $SCRIPT_NAME -- "$@")
if [ $? != 0 ]; then
    show_options;
fi

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"

while true ; do
    case "$1" in
        -h) show_options ;;
        -n) BRIDGE_SUFFIX="$2" ; shift 2 ;;
        -b) BRIDGE_NAMES="$2" ; shift 2 ;;
        --) shift ; break ;;
        *) echo "Error: unsupported option $1." ; show_options ;;
    esac
done

function create_bridge {
    local BRIDGE_NAME=$1

    # Only add bridge if missing
    (sudo ovs-vsctl list-br | grep ${BRIDGE_NAME}$) || sudo ovs-vsctl add-br ${BRIDGE_NAME}

    # remove bridge before replacing it.
    (virsh net-list --persistent | grep "${BRIDGE_NAME} ") && virsh net-destroy ${BRIDGE_NAME}
    (virsh net-list --inactive --persistent | grep "${BRIDGE_NAME} ") && virsh net-undefine ${BRIDGE_NAME}

    virsh net-define <(sed -e "s/%NETWORK_NAME%/$BRIDGE_NAME/"  $template_dir/net.xml)
    virsh net-autostart ${BRIDGE_NAME}
    virsh net-start ${BRIDGE_NAME}

}

for NAME in $BRIDGE_NAMES; do
    create_bridge "$NAME$BRIDGE_SUFFIX"
done

# start default if needed and configure it to autostart
default_net=$(sudo virsh net-list --all --persistent | grep default | awk 'BEGIN{OFS=":";} {print $2,$3}')
state=${default_net%%:*}
autostart=${default_net##*:}

if [ "$state" != "active" ]; then
    virsh net-start default
fi

if [ "$autostart" != "yes" ]; then
    virsh net-autostart default
fi
