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

HOOK_SETTINGS="HOOK PHY MAC MTU SSID KEY ENCRYPTION"

# Default values
ADDRESS=$(mac_generate)
PHY=
MTU=1500
SSID=
KEY=
ENCRYPTION_MODE=

function _check() {
	assert isset SSID

	if isset ADDRESS; then
		assert ismac ADDRESS
	fi

	assert isinteger MTU
	assert ismac PHY

	if [ -n "${ENCRYPTION_MODE}" ]; then
		assert isset KEY
	fi
}

function _parse_cmdline() {
	while [ $# -gt 0 ]; do
		case "${1}" in
			--phy=*|--parent-device=*)
				PHY=$(cli_get_val ${1})
				;;
			--encryption-mode=*)
				ENCRYPTION_MODE=$(cli_get_val ${1})
				;;
			--address=*)
				ADDRESS=$(cli_get_val ${1})
				;;
			--ssid=*)
				SSID=$(cli_get_val ${1})
				;;
			--key=*)
				KEY=$(cli_get_val ${1})
				;;
			*)
				warning "Unrecognized option: ${1}"
				;;
		esac
		shift
	done

	# Just save the MAC address of the phy.
	PHY=$(phy_get ${PHY})
	PHY=$(phy_get_address ${PHY})
}

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

	# Read zone configuration.
	zone_config_read ${zone}

	if ! device_exists ${zone}; then
		#  Create the wireless interface.
		wireless_create ${zone} \
			--phy=${PHY} \
			--type="managed" \
			--address="${ADDRESS}" \
		|| exit $?
	fi

	# Start the WPA supplicant daemon.
	wpa_supplicant_start ${zone}

	zone_configs_up ${zone}

	exit ${EXIT_OK}
}

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

	if ! device_is_up ${zone}; then
		warning "Zone '${zone}' is not up"
		exit ${EXIT_OK}
	fi

	zone_configs_down ${zone}

	wpa_supplicant_stop ${zone}

	wireless_remove ${zone}

	exit ${EXIT_OK}
}

function _status() {
	local zone=${1}
	assert isset zone

	# Print the default header.
	cli_device_headline ${zone}

	# Exit if zone is down
	if ! zone_is_up ${zone}; then
		echo # Empty line
		exit ${EXIT_ERROR}
	fi

	cli_headline 2 "Wireless network information"
	cli_print_fmt1 2 "SSID" "$(wpa_cli_status_get ${zone} ssid)"
	cli_space

	cli_headline 3 "Access Point"
	local bssid=$(wpa_cli_status_get ${zone} bssid)
	assert isset bssid

	cli_print_fmt1 3 "BSSID" "${bssid}"
	cli_print_fmt1 3 "Frequency" \
		"$(wpa_cli_bss_get_frequency ${zone} ${bssid}) MHz"
	cli_print_fmt1 3 "Noise" \
		"$(wpa_cli_bss_get_noise ${zone} ${bssid})"
	cli_print_fmt1 3 "Quality" \
		"$(wpa_cli_bss_get_quality ${zone} ${bssid})"
	cli_print_fmt1 3 "Flags" \
		"$(wpa_cli_bss_get_flags ${zone} ${bssid})"
	cli_space

	cli_headline 3 "Encryption"
	cli_print_fmt1 3 "Mode" \
		"$(wpa_cli_status_get ${zone} key_mgmt)"
	cli_print_fmt1 3 "Pairwise cipher" \
		"$(wpa_cli_status_get ${zone} pairwise_cipher)"
	cli_print_fmt1 3 "Group cipher" \
		"$(wpa_cli_status_get ${zone} group_cipher)"
	cli_space

	cli_headline 2 "Configurations"
	zone_configs_cmd status ${zone}
	cli_space

	exit ${EXIT_OK}
}
