#!/usr/bin/env bash

if [[ $(grep -c bridge /proc/modules) -ne 0 ]];then
	rmmod bridge
fi
/etc/init.d/openvswitch restart >/dev/null 2>&1

ovs-vsctl --may-exist add-br br0
ovs-vsctl set bridge br0 protocols=OpenFlow13
ovs-vsctl set-fail-mode br0 secure
ovs-vsctl set-controller br0 tcp:127.0.0.1:6633
ovs-vsctl set bridge br0 other-config:disable-in-band=true

ovs-vsctl --may-exist add-port br0 wildcard_gre -- set Interface wildcard_gre type=gre options:remote_ip=flow options:local_ip=flow options:key=flow ofport_request=64200
ovs-vsctl set interface wildcard_gre options:df_default="false"

# controller log to syslog
if [ ! -d "/var/log/unet3" ];then
	mkdir /var/log/unet3
fi

cat <<EOF > /etc/logrotate.d/controller
/var/log/unet3/controller.log
{
    compress
    notifempty
    rotate 7
    daily
    sharedscripts
    postrotate
    /bin/kill -HUP \`cat /var/run/syslogd.pid 2> /dev/null\` 2> /dev/null || true
    endscript
}
EOF

cat <<EOF > /etc/rsyslog.d/controller.conf
local0.*                         -/var/log/unet3/controller.log
local0.*                         ~
EOF

sed -i '/imudp/s/^#//' /etc/rsyslog.conf
sed -i '/ModLoad imudp/a\
$UDPServerAddress 127.0.0.1' /etc/rsyslog.conf
sed -i '/UDPServerRun/s/^#//' /etc/rsyslog.conf

service rsyslog restart > /dev/null 2>&1
