#!/bin/bash
getConfigDestination() {
	case "$key" in
		'enabled')
			destinationenabled["$destination"]="$value"
		;;
		'path')
			destinationpath["$destination"]="${value%/}"
		;;
		'format')
			case "$value" in
				'mp3')
					destinationformat["$destination"]=mp3
					lameneeded=1
					# MP3 can't handfle more than 2 channels
					[[ -z ${destinationchannels["$destination"]} ]]	\
						&& destinationchannels["$destination"]=2
				;;
				'opus')
					destinationformat["$destination"]=opus
					opusencneeded=1
				;;
				'vorbis')
					destinationformat["$destination"]=vorbis
					oggencneeded=1
				;;
				'copy')
					destinationformat["$destination"]=copy
				;;
				*)
					echo "Unsupported destination format: $value" >&2
					exit $EFORMAT
				;;
			esac
		;;
		'quality')
			expr='^[0-9]*$'
			if ! [[ $value =~ $expr ]]
			then
				echo "Invalid quality value: $value" >&2
				exit $EQUALITY
			fi
			unset expr
			case "${destinationformat["$destination"]}" in
				'vorbis')
					destinationquality["$destination"]="$value"
				;;
				*)
					echo "Invalid parameter \"$key\" for format \"${destinationformat["$destination"]}\"" >&2
					exit $EFMTINVPARM
				;;
			esac
		;;
		'normalize')
			case $value in
				'true'|'on'|'yes')
					destinationnormalize["$destination"]=1
				;;
				'false'|'off'|'no')
					destinationnormalize["$destination"]=0
				;;
				*)
					echo "normalize takes values:"	\
					"'yes' ,'true' ,'on', 'no', 'false',"\
					"'off'"
				;;
			esac
		;;
		'bitrate')
			expr='^[0-9]*$'
			if ! [[ $value =~ $expr ]]
			then
				echo "Invalid bitrate value: $value" >&2
				exit $EQUALITY
			fi
			unset expr
			case "${destinationformat["$destination"]}" in
				'opus')
					destinationquality["$destination"]="$value"
				;;
				'mp3')
					destinationquality["$destination"]="$value"
				;;
				*)
					echo "$Invalid parameter \"$key\" for format \"${destinationformat["$destination"]}\"" >&2
					exit $EFMTINVPARM
				;;
			esac
		;;
		'loss')
			expr='^[0-9]*$'
			if ! [[ $value =~ $expr ]]
			then
				echo "Invalid loss value: $value" >&2
				exit $EQUALITY
			fi
			unset expr
			case "${destinationformat["$destination"]}" in
				'opus')
					destinationloss["$destination"]="$value"
				;;
				*)
					echo "$Invalid parameter \"$key\" for format \"${destinationformat["$destination"]}\"" >&2
					exit $EFMTINVPARM
				;;
			esac
		;;
		'channels')
			expr='^[0-9]*$'
			if ! [[ $value =~ $expr ]]
			then
				echo "Invalid channel count: $value" >&2
				exit $ECHANNEL
			fi
			unset expr
			destinationchannels["$destination"]=$value
		;;
		'frequency')
			expr='^[0-9]*$'
			if ! [[ $value =~ $expr ]]
			then
				echo "Invalid frequency value: $value" >&2
				exit $ECHANNEL
			fi
			unset expr
			destinationfrequency["$destination"]=$value
		;;
		'noresample')
			case $value in
				'true'|'on'|'yes')
					destinationnoresample["$destination"]=1
				;;
				'false'|'off'|'no')
					destinationnoresample["$destination"]=0
				;;
				*)
					echo "noresample takes values:"	\
					"'yes' ,'true' ,'on', 'no', 'false',"\
					"'off'"
				;;
			esac
		;;
		'rename')
			case "$value" in
				*/*)
					destinationrenamepath["$destination"]="${value%/*}"
				;;
			esac
			destinationrename["$destination"]="${value##*/}"
		;;
		'fat32compat')
			case $value in
				'true'|'on'|'yes')
					destinationfat32compat["$destination"]=1
				;;
				'false'|'off'|'no')
					destinationfat32compat["$destination"]=0
				;;
				*)
					echo "fat32compat takes values:"	\
					"'yes' ,'true' ,'on', 'no', 'false',"\
					"'off'"
				;;
			esac
		;;
		'ascii-only')
			case $value in
				'true'|'on'|'yes')
					destinationascii["$destination"]=1
					textunidecodeneeded=1
				;;
				'false'|'off'|'no')
					destinationascii["$destination"]=0
				;;
				*)
					echo "ascii-only takes values:"	\
					"'yes' ,'true' ,'on', 'no', 'false',"\
					"'off'"
				;;
			esac
		;;
		'skip_mime-type')
			destinationskipmime[$destination]="${destinationskipmime[$destination]:+${destinationskipmime[$destination]}|}$value"
		;;
		'copy_mime-type')
			destinationcopymime[$destination]="${destinationcopymime[$destination]:+${destinationcopymime[$destination]}|}$value"
		;;
		'copy_extension')
			destinationcopyext[$destination]="${destinationcopyext[$destination]:+${destinationcopyext[$destination]}|}$value"
		;;
		'higher-than')
			expr='^[0-9]*$'
			if ! [[ $value =~ $expr ]]
			then
				echo "Invalid higher-than bitrate value: $value" >&2
				exit $EMAXBPS
			fi
			unset expr
			destinationmaxbps[$destination]="$value"
		;;
	esac
}
