# This file is part of bish-bosh. It is subject to the licence terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/raphaelcohn/bish-bosh/master/COPYRIGHT. No part of bish-bosh, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
# Copyright © 2014-2015 The developers of bish-bosh. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/raphaelcohn/bish-bosh/master/COPYRIGHT.


bishbosh_validate_backends()
{
	local code=$1
	local category="$2"
	local name="$3"
	local value="$4"

	if [ ${#value} -eq 0 ]; then
		core_validate_exit "The $category '$name' must specify at least one backend. Valid backends are '$(core_variable_array_string _bishbosh_backend_registration ',')'."
	fi
	
	local IFS=','
	local backend
	for backend in $value
	do
		if ! core_variable_array_contains _bishbosh_backend_registration "$backend"; then
			core_validate_exit "The $category '$name' specifies a backend '$backend' which is not known. Valid backends are '$(core_variable_array_string _bishbosh_backend_registration ',')'."
			return 0
		fi
	done
}

bishbosh_validate_tunnel()
{
	local code=$1
	local category="$2"
	local name="$3"
	local value="$4"
	
	case "$value" in
		
		none)
			:
		;;
		
		tls)
			:
		;;
		
		cryptcat)
			:
		;;
		
	esac
}

bishbosh_validate_unsignedIntegerUpto65535Inclusive()
{
	local code=$1
	local category="$2"
	local name="$3"
	local value="$4"
	
	core_validate_isUnsignedInteger $code "$category" "$name" "$value"
	if [ $value -gt 65535 ]; then
		core_exitError $code  "The $category '$name' specifies a value '$value' which is not between 0 and 65,535 inclusive."
	fi
}

bishbosh_validate_address()
{
	local code=$1
	local category="$2"
	local name="$3"
	local value="$4"
}

bishbosh_validate_filesizeAlgorithm()
{
	local code=$1
	local category="$2"
	local name="$3"
	local value="$4"
	
	case "$value" in
		
		ls)
			:
		;;
		
		GNUAndBusyBoxStat)
			:
		;;
		
		BSDStat)
			:
		;;
		
		ToyboxStat)
			:
		;;
		
		*)
			core_validate_exit "The $category '$name' specifies a filesize algorithm '$value' which is not understood (valid values are 'ls', 'GNUAndBusyBoxStat', 'BSDStat', and 'ToyboxStat')."
		;;
		
	esac
}

bishbosh_validate_latency()
{
	local code=$1
	local category="$2"
	local name="$3"
	local value="$4"
	
	core_validate_isUnsignedInteger $code "$category" "$name" "$value"
	if [ $value -gt 1000 ]; then
		core_validate_exit "The $category '$name' must be between 0 and 1000 (inclusive)."
	fi
}

bishbosh_validate_transport()
{
	local code=$1
	local category="$2"
	local name="$3"
	local value="$4"
	
	case "$value" in
		
		inet)
			:
		;;
		
		inet4)
			:
		;;
		
		inet6)
			:
		;;
		
		unix)
			:
		;;
		
		serial)
			:
		;;
		
		*)
			core_validate_exit "The $category '$name' specifies a transport '$value' which is not understood (valid values are 'inet' for OS-preferred v4/v6 connections, 'inet4', for IPv4-only connections, 'inet6', for IPv6-only connections,  'unix', for an Unix domain socket, and 'serial' for a character device)."
		;;
		
	esac
}

bishbosh_validate_proxyKind()
{
	local code=$1
	local category="$2"
	local name="$3"
	local value="$4"
	
	case "$value" in
		
		SOCKS4|SOCKS4a|SOCKS5|HTTP)
			:
		;;
		
		none)
			:
		;;
		
		*)
			core_validate_exit "The $category '$name' specifies a proxy kind '$value' which is not understood (valid values are 'SOCKS4', 'SOCKS5', 'HTTP' and 'none')."
		;;
		
	esac
}

bishbosh_validate_proxyIsOff()
{
	if core_variable_isSet bishbosh_validate_proxyKind; then
		if [ "$core_variable_isSet" = "off" ]; then
			return 1
		fi
	fi
	return 0
}