#!/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 BROADCAST_SSID CHANNEL COUNTRY_CODE MODE PHY SSID"

ADDRESS=$(mac_generate)
BROADCAST_SSID=on
CHANNEL=1
COUNTRY_CODE="US"
MODE="g"
SSID=

function _check() {
	assert isset ADDRESS
	assert ismac ADDRESS
	assert isset BROADCAST_SSID
	assert isbool BROADCAST_SSID
	assert isset CHANNEL
	assert isset COUNTRY_CODE
	assert isset MODE
	assert isoneof MODE b g
	assert isset PHY
	assert ismac PHY
	assert isset SSID
}

function _create() {
	while [ $# -gt 0 ]; do
		case "${1}" in
			--broadcast-ssid=*)
				BROADCAST_SSID=$(cli_get_val ${1})
				;;
			--channel=*)
				CHANNEL=$(cli_get_val ${1})
				;;
			--country-code=*)
				COUNTRY_CODE=$(cli_get_val ${1})
				;;
			--mac=*)
				ADDRESS=$(cli_get_val ${1})
				;;
			--mode=*)
				MODE=$(cli_get_val ${1})
				;;
			--phy=*)
				PHY=$(cli_get_val ${1})
				;;
			--ssid=*)
				SSID=$(cli_get_val ${1})
				;;
			*)
				warning "Ignoring unknown argument '${1}'"
				;;
		esac
		shift
	done

	# Save address of phy do identify it again
	PHY=$(phy_get ${PHY})
	PHY=$(phy_get_address ${PHY})

	local port=$(port_find_free ${PORT_PATTERN_ACCESSPOINT})
	assert isset port

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

	exit ${EXIT_OK}
}

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

	assert isset port

	config_read $(port_file ${port})

	while [ $# -gt 0 ]; do
		case "${1}" in
			--broadcast-ssid=*)
				BROADCAST_SSID=$(cli_get_val ${1})
				;;
			--channel=*)
				CHANNEL=$(cli_get_val ${1})
				;;
			--country-code=*)
				COUNTRY_CODE=$(cli_get_val ${1})
				;;
			--ssid=*)
				SSID=$(cli_get_val ${1})
				;;
			--mode=*)
				MODE=$(cli_get_val ${1})
				;;
			*)
				warning "Unknown argument '${1}'"
				;;
		esac
		shift
	done

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

	exit ${EXIT_OK}	
}

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

	assert isset port

	config_read $(port_file ${port})

	if ! device_exists ${port}; then
		wireless_create ${port} ${PHY} __ap ${ADDRESS}
	fi

	if ! hostapd_is_running ${port}; then
		hostapd_start ${port} \
			--broadcast-ssid="${BROADCAST_SSID}" \
			--channel="${CHANNEL}" \
			--country-code="${COUNTRY_CODE}" \
			--mode="${MODE}" \
			--ssid="${SSID}"

		local ret=$?

		if [ ${ret} -eq ${EXIT_ERROR} ]; then
			error_log "Could not start '${port}' because hostapd crashed previously."
			( _down ${port} )
			exit ${EXIT_ERROR}
		fi
	fi

	exit ${EXIT_OK}
}

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

	config_read $(port_file ${port})

	# Stop the hostapd daemon.
	hostapd_stop ${port}

	# Remove the device if it is still present.
	if device_exists ${port}; then
		wireless_remove ${port}
	fi

	exit ${EXIT_OK}
}

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

	assert isset port
	assert isset phy
	assert port_exists ${port}

	# Read configuration of port.
	config_read $(port_file ${port})

	# Get the address of the phy.
	local phy_address=$(phy_get_address ${phy})

	# Check if the phy is the same we have
	# read from the configuration file.
	if [ "${PHY}" = "${phy_address}" ]; then
		wireless_create ${port} ${PHY} __ap ${ADDRESS}
	fi

	exit ${EXIT_OK}
}
