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

hook_new() {
	local zone="${1}"
	shift

	local id=$(zone_config_get_new_id ${zone})
	log DEBUG "ID for the config is: ${id}"

	# Parse command line arguments
	if ! hook_parse_cmdline "${id}" "$@"; then
		# Return an error if the parsing of the cmd line fails
		return ${EXIT_ERROR}
	fi

	# Write configuration to disk
	if ! zone_config_settings_write "${zone}" "${HOOK}"; then
		return ${EXIT_ERROR}
	fi

	return ${EXIT_OK}
}

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

	local hook=$(config_get_hook_from_config ${config})
	local id=$(config_get_id_from_config ${config})

	assert isset hook
	assert isset id

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

	# Bring the config down
	if device_exists ${zone}; then
		if ! hook_config_cmd "down" "${zone}" "${hook}" "${hook}.${id}"; then
			log ERROR "Could not bring config ${config} down for zone ${zone}"
			return ${EXIT_ERROR}
		fi
	fi

	local ${HOOK_CONFIG_SETTINGS}

	# If reading the config fails we cannot go on
	if ! zone_config_settings_read "${zone}" "${config}"; then
		log ERROR "Could read the config ${config} for zone ${zone}"
		return ${EXIT_ERROR}
	fi

	if ! hook_parse_cmdline "${id}" "$@"; then
		# Return an error if the parsing of the cmd line fails
		return ${EXIT_ERROR}
	fi

	# Write the settings to the config file
	if ! zone_config_settings_write "${zone}" "${hook}" ${id}; then
		log ERROR "Could not write config settings file ${config} for ${zone}"
		return ${EXIT_ERROR}
	fi

	# Bring the config up
	if device_exists ${zone}; then
		if ! hook_config_cmd "up" "${zone}" "${hook}" "${hook}.${id}"; then
			log ERROR "Could not bring config ${config} up for zone ${zone}"
			return ${EXIT_ERROR}
		fi
	fi

	exit ${EXIT_OK}
}

hook_destroy() {
	return ${EXIT_OK}
}


# Returns the ID as a unique identifier
# Should always be overwritten by a hook
hook_hid() {
	local zone=${1}
	local config=${2}

	config_get_id_from_config "${config}"
}
