### Factory-tool include file
#
# This file includes general functions.

. /usr/share/factory-tool/scripts/factory_tool_config


### Functions

## Check channel input parameter
# Check if channel is available
# show_help is script dependent
# Parameters:
# $1 - channel to check
# $2 - maximum channel number to check against
check_channel()
{
	local channel="$1"
	local max_channel="$2"
	if echo "$channel" | grep -o "^[1-9]\{0,1\}[[:digit:]]\{1\}$" > /dev/null; then
		if [ "$channel" -gt "$max_channel" ]; then
			echo "Error: channel number exceeds maximum"
			show_help
			exit 1
		elif [ "$channel" -eq "0" ]; then
			echo "Error: channel can't be 0"
			show_help
			exit 1
		fi
	elif [ "$channel" != "all" ]; then
		echo "Error: wrong channel parameter"
		show_help
		exit 1
	fi
}

## Generates channel specific mix-matrix
# Parameters:
# $1 (in_channels) - total amount of input channels (1-99)
# $2 (out_channels) - total amount of output channels (1-99)
# $3 (in_ch_used) - input channel(s) to be active, one specific channel (1-99) or all
# $4 (out_ch_used) - output channel(s) to be active, one specific channel (1-99) or all
# Note: in case "all" is used for two last parameters - following rules apply
# 	Input channels will be mapped to corresponding output channels.
# 	When there are less input channels, than output channels - input channels will
# 	be repeatedly mapped for all extra channels
generate_channel_mix_matrix()
{
	# Input parameters
	in_channels="$1"
	out_channels="$2"
	# Use "all", if last two parameters are not present
	if [ "$3" = "" ]; then
		in_ch_used="all"
	else
		in_ch_used="$3"
	fi
	if [ "$4" = "" ]; then
		out_ch_used="all"
	else
		out_ch_used="$4"
	fi

	# Some checks to avoid improper input
	if [ "$in_ch_used" != "all" ] && [ "$in_ch_used" -gt "$in_channels" ]; then
		echo "Error: Requested active input channel ($in_ch_used) is grater than total input channels provided ($in_channels)"
		exit 1
	elif [ "$out_ch_used" != "all" ] && [ "$out_ch_used" -gt "$out_channels" ]; then
		echo "Error: Requested active output channel ($out_ch_used) is grater than total output channels provided ($out_channels)"
		exit 1
	elif [ "$in_channels" = "0" ] || [ "$out_channels" = "0" ] || [ "$in_ch_used" = "0" ] ||  [ "$out_ch_used" = "0" ]; then
		echo "Error: none of parameters can be 0"
		exit 1
	fi

	mix_matrix="<"

	for out_ch in `seq "$out_channels"`; do

		if [ "$out_ch_used" = "all" ] || [ "$out_ch_used" = "$out_ch" ]; then
			out_ch_active="yes"
		else
			out_ch_active="no"
		fi

		output_channel_already_set="no"

		mix_matrix="$mix_matrix<"

		for in_ch in `seq "$in_channels"`; do

			if [ "$in_ch_used" = "all" ] || [ "$in_ch_used" = "$in_ch" ]; then
				in_ch_active="yes"
			else
				in_ch_active="no"
			fi

			# Do not set more than one input active for one output
			if [ "$output_channel_already_set" = "yes" ]; then
				volume="0"
			# Special handling if all is used for last two parameters
			elif [ "$out_ch_used" = "all" ] && [ "$in_ch_used" = "all" ]; then
				if [ "$in_channels" -ge "$out_channels" ] && [ "$out_ch" = "$in_ch" ]; then
					volume="1"
				elif [ "$((out_ch%in_channels))" = "$((in_ch%in_channels))" ]; then
					volume="1"
				else
					volume="0"
				fi
			elif [ "$out_ch_active" = "yes" ] && [ "$in_ch_active" = "yes" ]; then
				volume="1"
			else
				volume="0"
			fi

			mix_matrix="$mix_matrix(float)$volume.0"

			if [ "$volume" = "1" ]; then
				output_channel_already_set="yes"
			fi

			if [ "$in_ch" != "$in_channels" ]; then
				mix_matrix="$mix_matrix,"
			fi
		done

		mix_matrix="$mix_matrix>"

		if [ "$out_ch" != "$out_channels" ]; then
			mix_matrix="$mix_matrix,"
		fi
	done
	mix_matrix="$mix_matrix>"

	echo "$mix_matrix"
}
