#!/usr/bin/env bash

set -ex

case "$1" in
    --help)
        echo "Run safely, e.g.: sudo -i sh -c '/home/oleg/bin/brctl-start; sleep 120; reboot'"
        ;;
    *)
        if [[ $(ip a) == *"tap0"* ]]
        then
            printf "\033[35mtap0 already exists, exiting.\033[0m\n"
            exit 1
        fi
        ip tuntap add tap0 mode tap

        ip address del 192.168.0.144/24 dev enp34s0

        # Could break office network if tap0 is openvpn connection:
        brctl addbr br0
        brctl addif br0 tap0
        brctl addif br0 enp34s0

        ip link set br0 up
        ip address add 192.168.0.144/24 dev br0
        ip route add default via 192.168.0.1

        ip link set tap0 up

        # https://bugzilla.redhat.com/show_bug.cgi?id=542405
        sysctl net.bridge.bridge-nf-call-iptables=0
        echo "Hint: Make sure tap10 interfaces inside the bridge by invoking 'brctl addif br0 tap10' and interface is up by invoking 'ip link set tap10 up'."
esac
