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

. /lib/network/header-port

HOOK_SETTINGS="HOOK DEVICE_MAC"

function _check() {
	assert ismac DEVICE_MAC
}

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

	assert isset port

	DEVICE_MAC=$(device_get_address ${port})

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

	exit ${EXIT_OK}
}

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

	assert isset port

	device_set_up ${port}

	exit ${EXIT_OK}
}

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

	assert isset port

	device_set_down ${port}

	exit ${EXIT_OK}
}

function _hotplug() {
	local port=${1}
	local device=${2}

	assert isset port
	assert isset device

	config_read $(port_file ${port})

	if [ "${DEVICE_MAC}" = "$(device_get_address ${device})" ]; then
		exit ${EXIT_OK}
	fi

	exit ${EXIT_ERROR}
}

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

	assert isset port

	echo "${port}"

	local status=$(device_get_status ${port})
	printf "${DEVICE_PRINT_LINE1}" "Status:" "$(echo -ne ${STATUS_COLOUR[${status}]}${STATUS_TEXT[${status}]}${COLOUR_NORMAL})"

	cli_headline "  Ethernet information:"
	printf "${DEVICE_PRINT_LINE1}" "Address:" $(device_get_address ${port})
	printf "${DEVICE_PRINT_LINE1}" "MTU:" $(device_get_mtu ${port})
	printf "${DEVICE_PRINT_LINE1}" "Promisc mode:" $(device_is_promisc ${port} && echo "yes" || echo "no")

	if device_is_bonded ${port}; then
		cli_headline "  Bonding information:"

		local master=$(bonding_slave_get_master ${port})
		printf "${DEVICE_PRINT_LINE1}" "Master:" "${master}"

		local active
		if [ "$(bonding_get_active_slave ${master})" = "${port}" ]; then
			active="yes"
		else
			active="no"
		fi
		printf "${DEVICE_PRINT_LINE1}" "Active slave:" "${active}"
	fi

	cli_headline "  Statistics:"
	printf "${DEVICE_PRINT_LINE1}" "Received:" \
		"$(beautify_bytes $(device_get_rx_bytes ${port})) ($(device_get_rx_packets ${port}) packets)"
	printf "${DEVICE_PRINT_LINE1}" "Sent:" \
		"$(beautify_bytes $(device_get_tx_bytes ${port})) ($(device_get_tx_packets ${port}) packets)"

	echo

	exit ${EXIT_OK}
}

run $@
