#!/bin/sh

# You don't usually need to touch this file at all, the full configuration
# of the bridge can be done in a standard way on /etc/network/interfaces.

# Have a look at /usr/share/doc/bridge-utils/README.Debian.gz if you want
# more info about the way on wich a bridge is set up on Debian.

if [ ! -x /usr/sbin/brctl ]
then
  exit 0
fi

case "$IF_BRIDGE_PORTS" in
    "")
	exit 0
	;;
    none)
	INTERFACES=""
	;;
    *)
	INTERFACES="$IF_BRIDGE_PORTS"
	;;
esac

ifconfig $IFACE down

# TODO: Should be perhaps simply remove all interfaces that are currently
# attached to the bridge?  But what to do then with the invokations of
# `if-pre-up.d/vlan'?
all_interfaces= &&
unset all_interfaces &&
set x $INTERFACES &&
shift &&
while [ x"${1+set}" = xset ]
do
  # For compatibility: the `all' option.
  case $1 in
      all)
	  shift &&
	  set regex eth.\* noregex "$@"
	  ;;
  esac

  # Primitive state machine...
  case $1-`uname -s` in
      regex-Linux)
	  all_interfaces=`sed -n 's%^[\ ]*\([^:]*\):.*$%\1%p' < /proc/net/dev`
	  shift
	  ;;
      regex-*)
	  echo -n "$0 needs to be ported for your `uname -s` system.  " >&2
	  echo "Trying to continue nevertheless." >&2
	  shift
	  ;;
      noregex-*)
	  all_interfaces=
	  unset all_interfaces
	  shift
	  ;;
  esac

  case ${all_interfaces+regex}-${1+set} in
      regex-set)
	  # The following interface specification are to be parsed as regular
	  # expressions against all interfaces the system provides.
	  i=`egrep "^$1$" << EOAI
$all_interfaces
EOAI
`
	  shift
	  ;;
      *-set)
	  # Literal interfaces.
	  i=$1
	  shift
	  ;;
      *)
	  # No interface specification is following.
	  i=
	  ;;
  esac

  for port in $i
  do
    ifconfig $port down && brctl delif $IFACE $port && \
      if [ -x /etc/network/if-post-down.d/vlan ]; then
        env IFACE=$port /etc/network/if-post-down.d/vlan
      fi
  done
done

brctl delbr $IFACE
