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

HOOK_SETTINGS="HOOK DELAY"

# Default settings.
DELAY=0

function _check() {
	assert isset DELAY
	assert isinteger DELAY
}

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

	while [ $# -gt 0 ]; do
		case "${1}" in
			--delay=*)
				DELAY=${1#--delay=}
				;;
		esac
		shift
	done

	config_write $(zone_dir ${zone})/configs/${HOOK} ${HOOK_SETTINGS}

	exit ${EXIT_OK}
}

function _up() {
	local zone=${1}
	local config=${2}
	shift 2

	if ! device_exists ${zone}; then
		error "Zone '${zone}' doesn't exist."
		exit ${EXIT_ERROR}
	fi

	# Start dhclient for IPv4 on this zone.
	dhclient_start ${zone} ipv4

	exit ${EXIT_OK}
}

function _down() {
	local zone=${1}
	local config=${2}
	shift 2

	if ! device_exists ${zone}; then
		error "Zone '${zone}' doesn't exist."
		exit ${EXIT_ERROR}
	fi

	# Stop dhclient for IPv4 on this zone.
	dhclient_stop ${zone} ipv4

	exit ${EXIT_OK}
}

function _status() {
	local zone=${1}
	local config=${2}
	shift 2

	if ! device_exists ${zone}; then
		error "Zone '${zone}' doesn't exist."
		exit ${EXIT_ERROR}
	fi
	
	config_read $(zone_dir ${zone})/configs/${config}

	local status
	if dhclient_status ${zone} ipv4; then
		status="${MSG_HOOK_UP}"
	else
		status="${MSG_HOOK_DOWN}"
	fi
	cli_statusline 3 "${HOOK}" "${status}"

	cli_print_fmt1 3 "IPv4 address" "$(routing_db_get ${zone} ipv4 local-ip-address)"
	local gateway=$(routing_db_get ${zone} ipv4 remote-ip-address)
	if isset gateway; then
		cli_print_fmt1 3 "Gateway" "${gateway}"
	fi
	cli_space

	exit ${EXIT_OK}
}
