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

DEVICE_PATTERN="apN"

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 ${DEVICE_PATTERN})
	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})

	if ! device_exists ${port}; then
		exit ${EXIT_OK}
	fi

	hostapd_stop ${port}
	wireless_remove ${port}

	exit ${EXIT_OK}
}

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

config_read $(zone_dir ${zone})/${port}

	local device=$(devicify ${DEVICE_MAC})

	printf "        %-10s - " "${device}"
	if ! device_is_up ${device}; then
		echo -ne "${COLOUR_DOWN}   DOWN   ${COLOUR_NORMAL}"
	else
		local state=$(stp_port_state ${zone} ${device})
		local colour="COLOUR_STP_${state}"
		printf "${!colour}%10s${COLOUR_NORMAL}" ${state}
	fi

	echo -n " - DSR: $(stp_port_designated_root ${zone} ${device})"
	echo -n " - Cost: $(stp_port_pathcost ${zone} ${device})"
	echo

	exit ${EXIT_OK}
}

run $@
