#!/bin/bash

# This script is intended to be run from command-line or etcnet
# initscript.

usage_setup_bri()
{
	echo "Usage: $0 <interface>" >&2
	exit 1
}

if [ -z "$NAME" ]; then
	[ -z "$1" ] && usage_setup_bri
	export NAME=$1
fi

if [ -z "$SCRIPTDIR" ]; then
	export SCRIPTDIR=/etc/net/scripts
	. $SCRIPTDIR/functions
fi

pickup_defaults
if [ -d $IFACEDIR/$NAME@$NETHOST ]; then
	MYIFACEDIR=$IFACEDIR/$NAME@$NETHOST
else
	MYIFACEDIR=$IFACEDIR/$NAME
fi

[ -d "$MYIFACEDIR" ] || {
	print_error "interface configuration directory '$MYIFACEDIR' not found"
	exit 1
}
export MYIFACEDIR NETPROFILE

init_netprofile
pickup_options

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

for host in $HOST; do
	$BRCTL addif $NAME $host
done

PROF_BRCTL=`profiled_filename $MYIFACEDIR/brctl`
[ -s "$PROF_BRCTL" ] && {
	$DENOISE $PROF_BRCTL | trim | \
	sed "s/ AUTO / $NAME /" | \
	xargs --max-lines=1 $BRCTL
} && print_progress
