#!/bin/sh

# bridge-network-interface - configure a network bridge
#
# This service checks whether a physical network device that has been added
# is one of the ports in a bridge config, and if so, brings up the related
# bridge

set -e

if [ -z "$INTERFACE" ]; then
	echo "missing \$INTERFACE" >&2
	exit 1
fi

. /lib/bridge-utils/bridge-utils.sh

mkdir -p /var/run/network
for i in $(ifquery --list --allow auto); do
	ports=$(ifquery $i | sed -n -e's/^bridge_ports: //p')
	for port in $(bridge_parse_ports $ports); do
		case $port in
			$INTERFACE|$INTERFACE.*)
				ifup --allow auto $i
				brctl addif $i $port && ifconfig $port 0.0.0.0 up
				break
				;;
		esac
	done
done
