#!/bin/bash

# Copyright © 2012-2026 ScriptFanix
# 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.

# 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.

# A copy of the GNU General Public License v3 is includded in the LICENSE file
# at the root of the project.

getConfigDestination() {
	case "$key" in
		'enabled')
			# 1 = process this destination, 0 = skip it
			destinationenabled["$destination"]="$value"
		;;
		'path')
			# Strip trailing slash for consistency
			destinationpath["$destination"]="${value%/}"
		;;
		'format')
			case "$value" in
				'mp3')
					destinationformat["$destination"]=mp3
					# Flag that lame must be present
					lameneeded=1
					# MP3 can't handle more than 2 channels
					[[ -z ${destinationchannels["$destination"]} ]]	\
						&& destinationchannels["$destination"]=2
				;;
				'opus')
					destinationformat["$destination"]=opus
					# Flag that opusenc must be present
					opusencneeded=1
				;;
				'vorbis')
					destinationformat["$destination"]=vorbis
					# Flag that oggenc must be present
					oggencneeded=1
				;;
				'copy')
					# Files are copied/hardlinked as-is
					destinationformat["$destination"]=copy
				;;
				*)
					echo "Unsupported destination format: $value" >&2
					exit $EFORMAT
				;;
			esac
		;;
		'quality')
			# Vorbis-only: oggenc quality scale (integer, typically 0-10)
			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')
			# Whether to normalize audio volume (via sox --norm)
			case $value in
				'true'|'on'|'yes'|'1')
					destinationnormalize["$destination"]=1
				;;
				'false'|'off'|'no'|'0')
					destinationnormalize["$destination"]=0
				;;
				*)
					echo "normalize takes values:"	  \
					"'yes' ,'true' ,'on', '1', 'no'," \
					"'false','off', '0'"
				;;
			esac
		;;
		'bitrate')
			# Opus/MP3: target bitrate in kbps
			# integer only; Bash doesn't support floats
			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')
			# Opus Forward Error Correction: expected packet loss percentage (0-100)
			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')
			# Up/downmix to this many channels if needed
			expr='^[0-9]*$'
			if ! [[ $value =~ $expr ]]
			then
				echo "Invalid channel count: $value" >&2
				exit $ECHANNEL
			fi
			unset expr
			destinationchannels["$destination"]=$value
		;;
		'frequency')
			# Resample to this sample rate in Hz (e.g. 44100)
			expr='^[0-9]*$'
			if ! [[ $value =~ $expr ]]
			then
				echo "Invalid frequency value: $value" >&2
				exit $ECHANNEL
			fi
			unset expr
			destinationfrequency["$destination"]=$value
		;;
		'noresample')
			# MP3-only: prevent lame from auto-downsampling at low
			# bitrates
			case $value in
				'true'|'on'|'yes'|'1')
					destinationnoresample["$destination"]=1
				;;
				'false'|'off'|'no'|'0')
					destinationnoresample["$destination"]=0
				;;
				*)
					echo "noresample takes values:"	\
					"'yes' ,'true' ,'on', '1', 'no',"\
					"'false','off', '0'"
				;;
			esac
		;;
		'rename')
			# File rename pattern using %{tag} tokens
			case "$value" in
				*/*)
					destinationrenamepath["$destination"]="${value%/*}"
				;;
			esac
			destinationrename["$destination"]="${value##*/}"
		;;
		'fat32compat')
			# Strip FAT32-illegal characters (? \ < > : * | ")
			# trim spaces/dots
			case $value in
				'true'|'on'|'yes'|'1')
					destinationfat32compat["$destination"]=1
				;;
				'false'|'off'|'no'|'0')
					destinationfat32compat["$destination"]=0
				;;
				*)
					echo "fat32compat takes values:" \
					"'yes' ,'true' ,'on', '1', 'no',"\
					"'false','off', '0'"
				;;
			esac
		;;
		'ascii-only')
			# Transliterate Unicode filenames to ASCII using
			#Requires Perl Text::Unidecode
			case $value in
				'true'|'on'|'yes'|'1')
					destinationascii["$destination"]=1
					# Signal that the perl coprocess will
					# be needed
					textunidecodeneeded=1
				;;
				'false'|'off'|'no'|'0')
					destinationascii["$destination"]=0
				;;
				*)
					echo "ascii-only takes values:"	\
					"'yes' ,'true' ,'on', '1', 'no',"\
					"'false','off', '0'"
				;;
			esac
		;;
		'skip_mime-type')
			# Accumulate pipe-separated list of mime patterns to
			# exclude entirely
			destinationskipmime[$destination]="${destinationskipmime[$destination]:+${destinationskipmime[$destination]}|}$value"
		;;
		'copy_mime-type')
			# Accumulate pipe-separated list of mime patterns to
			# copy verbatim (action=2)
			destinationcopymime[$destination]="${destinationcopymime[$destination]:+${destinationcopymime[$destination]}|}$value"
		;;
		'copy_extension')
			# Accumulate pipe-separated list of file extensions to
			# copy verbatim
			destinationcopyext[$destination]="${destinationcopyext[$destination]:+${destinationcopyext[$destination]}|}$value"
		;;
		'higher-than')
			# Only re-encode source files with bitrate ABOVE this
			# threshold (kbps)
			# Files at or below this bitrate will be
			# hardlinked/copied instead
			expr='^[0-9]*$'
			if ! [[ $value =~ $expr ]]
			then
				echo "Invalid higher-than bitrate value: $value" >&2
				exit $EMAXBPS
			fi
			unset expr
			destinationmaxbps[$destination]="$value"
		;;
	esac
}
