#!/bin/bash

pickup_defaults
init_netprofile
pickup_options

#if profiled_filename PROF_BRCTL "$MYIFACEDIR/brctl"; then
#	print_error "ignoring brctl-style options in $PROF_BRCTL"
#fi

[ -x "${BRCTL:=$DEFAULT_BRCTL}" ] || {
	print_error "$BRCTL does not exist or is not executable. Try installing bridge-utils RPM."
	exit 1
}

[ -x "${BRIDGE:=$DEFAULT_BRIDGE}" ] || {
	print_error "$BRIDGE does not exist or is not executable. Try installing iproute2 RPM."
	exit 1
}

$IP link add $NAME type bridge
is_yes "$VLAN_AWARE" && BRIDGE_OPTIONS="${BRIDGE_OPTIONS:+"$BRIDGE_OPTIONS" }vlan_filtering 1"
$IP link set $NAME type bridge $BRIDGE_OPTIONS

# Give brctl a chance to set necessary parameters on a bridge before adding ports into it,
# so we don't suddenly plug unconfigured bridge into network. The only thing we can't
# set this way are port priority and cost (which shouldn't be critical in most cases).
if profiled_filename PROF_BRCTL "$MYIFACEDIR/brctl"; then
	$DENOISE $PROF_BRCTL |
		trim |
		sed "s/ AUTO / $NAME /" |
		xargs --max-lines=1 $BRCTL
fi && print_progress

for host in $HOST; do
	$IP link set $host master $NAME && print_progress
	is_yes "$VLAN_AWARE" && {
		if [ -n "$VIDS" ]; then
			for vid in $VIDS; do
				$BRIDGE vlan add dev $host vid $vid && print_progress
			done
		fi
	}
done

profiled_filename PROF_BRIDGE "$MYIFACEDIR/bridge" &&
	xargise_file "$PROF_BRIDGE" "$BRIDGE"

exit 0
