#!/bin/sh
#
# gwck qMp module
#
# Copyright (C) 2008-2013 Simó Albert i Beltran
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

#############
# Variables #
#############

# Not disable offer if no other nodes offer Internet access.
[ -z "$GWCK_MOD_QMP_TEST_OTHER_OFFER" ] \
&& GWCK_MOD_QMP_TEST_OTHER_OFFER="true"

# Enable offer command.
[ -z "$GWCK_MOD_QMP_ENABLE_OFFER" ] \
&& GWCK_MOD_QMP_ENABLE_OFFER="qmpcontrol offer_default_gw"

# Enable search command.
[ -z "$GWCK_MOD_QMP_ENABLE_SEARCH" ] \
&& GWCK_MOD_QMP_ENABLE_SEARCH="qmpcontrol search_default_gw"


#############
# Functions #
#############

##
# Determine if other nodes are offered any internet access.
#
gwck_mod_qmp_are_inet_offered_by_other_node()
{
	# TODO
	return 0
}

##
# Change to offering Internet access.
#
gwck_mod_qmp_change_to_offer()
{
	$GWCK_MOD_QMP_ENABLE_OFFER 2>&1 | log \
	&& log "Offering Internet access with qMp."
}

##
# Change to searching Internet access.
#
gwck_mod_qmp_change_to_search()
{
	$GWCK_MOD_QMP_ENABLE_SEARCH 2>&1 | log \
	&& log "Searching Internet access with BMX6."
}


##############
# gwck hooks #
##############

gwck_mod_qmp_init()
{
	# Enable offer direct Internet access with qMp.
	gwck_mod_qmp_change_to_offer
}

gwck_mod_qmp_connect()
{
	# Enable offer direct Internet access with qMp.
	gwck_mod_qmp_change_to_offer
}

gwck_mod_qmp_disconnect()
{
	# Are other node offering any internet access?
	if gwck_mod_qmp_are_inet_offered_by_other_node || ! $GWCK_MOD_QMP_TEST_OTHER_OFFER
	then
		# Enabled searching Internet access with qMp.
		gwck_mod_qmp_change_to_search
	else
		# Print no change state message if don't are other modes offering internet access.
		"$LOOP" && log "No other node offering internet access with BMX6."
		# Save fake state of connection.
		connect = "true"
	fi
}

#gwck_mod_qmp_term()
#{
#	:
#}

#############
# Main code #
#############

# Actions in run-time load module.
:
