#!/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 MIIMON MODE SLAVES"

ADDRESS=$(mac_generate)
SLAVES=""
MIIMON=100

function _check() {
	assert isset ADDRESS
	assert ismac ADDRESS

	#assert isset SLAVES
	assert isinteger MIIMON
}

function _create() {
	_edit $@
}

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

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

	DEVICE=${port}

	# XXX think this must move to _check()
	if ! isset DEVICE; then
		error "You must set a device name."
		exit ${EXIT_ERROR}
	fi

	if ! isset SLAVES; then
		error "You need to specify at least one slave port (e.g. --slave=port0)."
		exit ${EXIT_ERROR}
	fi

	local slave
	for slave in $(unquote ${SLAVES}); do
		if ! device_is_ethernet ${slave}; then
			error "Slave device '${slave}' is not an ethernet device."
			exit ${EXIT_ERROR}
		fi
	done

	# Remove any whitespace
	SLAVES=$(echo ${SLAVES})

	port_config_write ${port} ${HOOK_SETTINGS}

	exit ${EXIT_OK}
}

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

	port_config_read ${device}

	if device_exists ${device}; then
		log DEBUG "Bonding device '${device}' does already exist."

		device_set_address ${device} ${ADDRESS}
		device_set_up ${device}

		exit ${EXIT_OK}
	fi

	bonding_create ${device} --address="${ADDRESS}" --mode="${MODE}"
	local ret=$?

	[ ${ret} -eq ${EXIT_OK} ] || exit ${EXIT_ERROR}

	bonding_set_miimon ${device} ${MIIMON}
	device_set_up ${device}

	local slave
	for slave in $(unquote ${SLAVES}); do
		if ! device_exists ${slave}; then
			warning_log "${device}: configured slave '${slave}' is not available."
			continue
		fi

		bonding_enslave_device ${device} ${slave}
	done

	# Bring up the device.
	device_set_up ${device}

	exit ${EXIT_OK}
}

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

	bonding_remove ${device}

	local slave
	for slave in ${SLAVES}; do
		device_set_down ${slave}
	done

	exit ${EXIT_OK}
}
