#!/bin/bash
# Version: 1.0

PATH=$PATH:/sbin:
bridge="vpn0"
eth="eth0"

if [[ ! $(ifconfig $bridge 2>/dev/null) ]]; then
	echo Bridge is not active, quitting...
	exit
fi

eth_addr=$(ifconfig $bridge 2>/dev/null | awk -F':| *' '/inet /{print $3}')
eth_mask=$(ifconfig $bridge 2>/dev/null | awk -F':| *' '/inet /{print $5}')
default_route=$(ip r | awk '/^default/ {print $3}')
default_if=$(ip r | awk '/^default/ {print $5}')

echo Bringing down bridge interface $bridge...
ifconfig $bridge down 2>/dev/null; brctl delbr $bridge 2>/dev/null

echo Turning promiscuous mode off for Ethernet interface $eth...
ifconfig $eth 0.0.0.0 -promisc up

if [[ -n $eth_addr && -n eth_mask ]]; then
	echo Setting Ethernet IP address $eth_addr with netmask $eth_mask...
	ifconfig $eth $eth_addr netmask $eth_mask
fi

if [[ $default_if == $bridge ]]; then
	echo Setting gateway for new Ethernet interface $bridge...
	ip r add default via $default_route dev $eth
fi

rm -f /etc/sysconfig/network/scripts/ifdown-$bridge 2>/dev/null

exit 0
