#!/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

HOOK_SETTINGS="ADDRESS"

hook_check_settings() {
	assert ismac ADDRESS
}

hook_parse_cmdline() {
	while [ $# -gt 0 ]; do
		case "${1}" in
			--address=*)
				ADDRESS=$(cli_get_val "${1}")
				;;
			*)
				warning "Unknown argument '${1}'"
				;;
		esac
		shift
	done

	if isset ADDRESS; then
		if ! ismac ADDRESS; then
			error "Invalid MAC address given: ${ADDRESS}"
			return ${EXIT_ERROR}
		fi

	# Generate a random but static MAC address if none was set
	else
		ADDRESS=$(mac_generate)
	fi
}

hook_new() {
	if ! hook_parse_cmdline "$@"; then
		return ${EXIT_ERROR}
	fi

	local port=$(port_find_free ${DUMMY_PORT_PATTERN})
	assert isset port

	if port_settings_write "${port}" ${HOOK_SETTINGS}; then
		log INFO "New dummy port '${port}' has been created"
	fi

	exit ${EXIT_OK}
}

hook_create() {
	local port="${1}"
	assert isset port

	# Read configuration
	port_settings_read "${port}" ${HOOK_SETTINGS}

	# Create the dummy device
	dummy_create "${port}" "${ADDRESS}"

	exit ${EXIT_OK}
}

hook_remove() {
	local port="${1}"
	assert isset port

	# Remove the dummy device
	dummy_remove "${port}"
}

hook_up() {
	local port="${1}"
	assert isset port

	# Bring up the port.
	device_set_up ${port}

	exit ${EXIT_OK}
}

hook_down() {
	local port="${1}"
	assert isset port

	# Tear down the port.
	device_set_down ${port}

	exit ${EXIT_OK}
}

hook_hotplug_rename() {
	local port=${1}
	assert isset port

	local device=${2}
	assert isset device

	port_settings_read "${port}" ${HOOK_SETTINGS}

	if [ "${ADDRESS}" = "$(device_get_address ${device})" ]; then
		log DEBUG "Device '${device}' equals port '${port}'."
		exit ${EXIT_OK}
	fi

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