#!/bin/bash
###############################################################################
#                                                                             #
# IPFire.org - A linux based firewall                                         #
# Copyright (C) 2010  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/>.       #
#                                                                             #
###############################################################################

. /usr/lib/network/header-port

# DEVICE equals the actual MAC address of the device.
# If ADDRESS is set, the device will get ADDRESS set for its MAC address.

HOOK_SETTINGS="HOOK ADDRESS DEVICE"

function _check() {
	assert ismac DEVICE

	if isset ADDRESS; then
		assert ismac ADDRESS
	fi
}

function _create() {
	local port=${1}
	assert isset port
	shift

	ADDRESS=""
	DEVICE=$(device_get_address ${port})

	config_write $(port_file ${port}) ${HOOK_SETTINGS}

	exit ${EXIT_OK}
}

function _up() {
	local port=${1}
	assert isset port

	# Read in the confguration file.
	port_config_read ${port}

	# Check if the MAC address is the right one.
	if isset ADDRESS; then
		device_set_address "${device}" "${ADDRESS}"
	fi

	# Bring up the device.
	device_set_up ${port}

	exit ${EXIT_OK}
}

function _down() {
	local port=${1}
	assert isset port

	# Set down the device.
	device_set_down ${port}

	exit ${EXIT_OK}
}

function _hotplug_rename() {
	local port=${1}
	assert isset port

	local device=${2}
	assert isset device

	# Read in the conifguration file.
	port_config_read ${port}

	# Get the current MAC address of the device.
	local address=$(device_get_address ${device})
	assert isset address

	# Check if the address matches with the configuration.
	if list_match "${address}" ${DEVICE} ${ADDRESS}; then
		log DEBUG "Device '${device}' equals port '${port}'."

		# Change the MAC address, if needed.
		if isset ADDRESS; then
			device_set_address "${device}" "${ADDRESS}"
		fi

		# Everything is done.
		exit ${EXIT_OK}
	fi

	log DEBUG "Device '${device}' does not equal port '${port}'."
	exit ${EXIT_ERROR}
}
