#!/bin/sh
#============================================================================
# $NetBSD: vif-bridge-nbsd,v 1.3 2005/11/08 00:47:35 jlam Exp $
#
# @PKG_SYSCONFDIR@/vif-bridge
#
# Script for configuring a vif in bridged mode with a dom0 interface.
# The xend(8) daemon calls a vif script when bringing a vif up or down.
# The script name to use is defined in @PKG_SYSCONFDIR@/xend-config.sxp
# in the ``vif-script'' field.
#
# Usage: vif-bridge up|down [var=value ...]
#
# Actions:
#    up		Adds the vif interface to the bridge.
#    down	Removes the vif interface from the bridge.
#
# Variables:
#    domain	name of the domain the interface is on (required).
#    vifq	vif interface name (required).
#    mac	vif MAC address (required).
#    bridge	bridge to add the vif to (required).
#
# Example invocation:
#
# vif-bridge up domain=VM1 vif=xvif1.0 mac="ee:14:01:d0:ec:af" bridge=bridge0
#
#============================================================================

# Exit if anything goes wrong
set -e 

echo "vif-bridge $*"

# Operation name.
OP=$1; shift

# Pull variables in args into environment
for arg ; do export "${arg}" ; done

# Required parameters. Fail if not set.
domain=${domain:?}
vif=${vif:?}
mac=${mac:?}
bridge=${bridge:?}

# Optional parameters. Set defaults.
ip=${ip:-''}   # default to null (do nothing)

# Are we going up or down?
case $OP in
up)	brcmd='add' ;;
down)	brcmd='delete' ;;
*)
	echo 'Invalid command: ' $OP
	echo 'Valid commands are: up, down'
	exit 1
	;;
esac

# Don't do anything if the bridge is "null".
if [ "${bridge}" = "null" ] ; then
	exit
fi

# Don't do anything if the bridge doesn't exist.
if ! ifconfig -l | grep "${bridge}" >/dev/null; then
	exit
fi

# Add/remove vif to/from bridge.
ifconfig x${vif} $OP
brconfig ${bridge} ${brcmd} x${vif}
