#!/bin/bash
###############################################################################
#                                                                             #
# IPFire.org - A linux based firewall                                         #
# Copyright (C) 2011  Michael Tremer & Christian Schmidt                      #
#                                                                             #
# 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/>.       #
#                                                                             #
###############################################################################

. /lib/network/functions

# Do nothing, if the network isn't running.
# That happens for example on boot time.
if ! network_is_running; then
	exit 0
fi

# Check if the udev environment variables are properly set.
assert isset ACTION
assert isset INTERFACE

# Check, if the device is a physical network interface and
# if we can handle it.
if ! device_is_real ${INTERFACE}; then
	exit ${EXIT_OK}
fi

# Check, if there is a configuration for that device.
if port_exists ${INTERFACE}; then
	port=${INTERFACE}

else
	# If the given device was not configured,
	# we create an initial configuration.
	port_create ethernet ${INTERFACE}
	exit ${EXIT_OK}
fi

case "${ACTION}" in
	add|register)
		zone=$(port_zone ${port})

		# Check, if the device is configured in a zone.
		# If not, there is nothing to do.
		isset zone || exit ${EXIT_OK}

		boot=$(zone_config_option ${zone} BOOT)
		if enabled boot; then
			zone_up ${zone}
		fi
		;;

	remove|unregister)
		port_down ${port}
		;;
esac

exit ${EXIT_OK}
