#!/bin/sh
###############################################################################
#                                                                             #
# IPFire - An Open Source Firewall Solution                                   #
# Copyright (C) 2011 IPFire development team                                  #
#                                                                             #
# This program is free software: you can redistribute it and/or modify        #
# it under the terms of the GNU General Public License as published by        #
# the Free Software Foundation, either version 3 of the License, or           #
# (at your option) any later version.                                         #
#                                                                             #
# This program is distributed in the hope that it will be useful,             #
# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
# GNU General Public License for more details.                                #
#                                                                             #
# You should have received a copy of the GNU General Public License           #
# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
#                                                                             #
###############################################################################

# Change LOG_FACILITY that we will find our messages in syslog.
LOG_FACILITY=$(basename ${0})

. /usr/lib/network/functions

zone=${1}
action=${2}

assert isset zone
assert isset action

# Exit immediately, if zone configuration does not exist.
# This is for manually created bridges.
if ! zone_exists ${zone}; then
	exit 1
fi

# Check if mstpd is running. If not, try to start it.
if ! service_is_active mstpd; then
	service_start mstpd

	if ! service_is_active mstpd; then
		log ERROR "mstpd is not running. STP might not work."
		exit 1
	fi
fi

# Tell mstpd that STP has to be enabled/disabled.
case "${action}" in
	start)
		log DEBUG "Enabling STP for zone '${zone}'."
		exec mstpctl addbridge ${zone}
		;;
	stop)
		log DEBUG "Disabling STP for zone '${zone}'."
		exec mstpctl delbridge ${zone}
		;;
	*)
		log ERROR "Unknown action given: ${action}."
		exit 1
		;;
esac

log ERROR "Could not properly exec mstpctl."
exit 1
