#!/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="HOOK ADDRESS PARENT TAG"

PORT_PARENTS_VAR="PARENT"

ADDRESS=$(mac_generate)

function _check() {
	assert isset PARENT
	assert ismac ADDRESS
	assert isinteger TAG

	if [ ${TAG} -gt 4096 ]; then
		error "TAG is greater than 4096."
		exit ${EXIT_ERROR}
	fi

	local reserved
	for reserved in 0 4095; do
		if [ "${TAG}" = "${reserved}" ]; then
			error "TAG=${reserved} is reserved."
			exit ${EXIT_ERROR}
		fi
	done
}

function _create() {
	while [ $# -gt 0 ]; do
		case "${1}" in
			--device=*)
				PARENT=${1#--device=}
				;;
			--mac=*)
				ADDRESS=${1#--mac=}
				;;
			--id=*)
				TAG=${1#--id=}
				;;
			*)
				warning "Unknown argument '${1}'"
				;;
		esac
		shift
	done

	local port="${PARENT}v${TAG}"

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

	exit ${EXIT_OK}
}

function _edit() {
	local port=${1}
	shift

	assert isset port

	config_read $(port_file ${port})

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

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

	exit ${EXIT_OK}	
}

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

	assert isset port

	config_read $(port_file ${port})

	if ! device_exists ${port}; then
		virtual_create ${PARENT} ${TAG} ${ADDRESS}
	fi

	exit ${EXIT_OK}
}

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

	assert isset port

	config_read $(port_file ${port})

	if ! device_exists ${port}; then
		exit ${EXIT_OK}
	fi

	virtual_remove ${port}

	exit ${EXIT_OK}
}
