#!/bin/bash
declare soxtaskid
decodeFile() {
	if [[ ${destinationformat["$destination"]} == copy ]]
	then
		copied=1
	else
		case "$mimetype" in
			'video/'*)
				(( disablevideo )) && continue
				extractAudio
				if (( ${destinationnormalize["$destination"]}))\
				|| (
				[ -n "${destinationfrequency["$destination"]}" ]\
				&& (( ${rate:-0} != ${destinationfrequency["$destination"]}))\
				) || (
				[ -n "${destinationchannels["$destination"]}" ]\
				&& (( ${channels:-0} != ${destinationchannels["$destination"]} ))
				)
				then
					sox_needed=1
				fi
			;;
			'audio/mpeg')
				if [[ ${destinationformat[$destination]} = mp3 ]] \
				&& checkCopy
				then
					copied=1
				else
					decodeSox
				fi
			;;
			'application/ogg opus')
				if [[ ${destinationformat[$destination]} = opus ]] \
				&& checkCopy
				then
					copied=1
				else
					(( disableopusdec )) && continue
					decodeOpusdec
					if (( ${destinationnormalize["$destination"]}))\
					|| (
					[ -n "${destinationfrequency["$destination"]}" ]\
					&& (( ${rate:-0} != ${destinationfrequency["$destination"]}))\
					) || (
					[ -n "${destinationchannels["$destination"]}" ]\
					&& (( ${channels:-0} != ${destinationchannels["$destination"]} ))
					)
					then
						sox_needed=1
					fi
				fi
			;;
			'audio/ogg opus')
				if [[ ${destinationformat[$destination]} = opus ]] \
				&& checkCopy
				then
					copied=1
				else
					(( disableopusdec )) && continue
					decodeOpusdec
					if (( ${destinationnormalize["$destination"]}))\
					|| (
					[ -n "${destinationfrequency["$destination"]}" ]\
					&& (( ${rate:-0} != ${destinationfrequency["$destination"]}))\
					) || (
					[ -n "${destinationchannels["$destination"]}" ]\
					&& (( ${channels:-0} != ${destinationchannels["$destination"]} ))
					)
					then
						sox_needed=1
					fi
				fi
			;;
			'application/ogg'*)
				if [[ ${destinationformat[$destination]} = vorbis ]] \
				&& checkCopy
				then
					copied=1
				else
					decodeSox
				fi
			;;
			'audio/ogg'*)
				if [[ ${destinationformat[$destination]} = vorbis ]] \
				&& checkCopy
				then
					copied=1
				else
					decodeSox
				fi
			;;
			'audio/x-flac')
				decodeSox
			;;
			'audio/flac')
				decodeSox
			;;
			*)
				extendedtype=$(file -b "$sourcepath/$filename")
				case "$extendedtype" in
					*'Musepack '*)
						(( disablempcdec )) && continue
						decodeMpcdec
						if (( ${destinationnormalize["$destination"]}))\
						|| (
						[ -n "${destinationfrequency["$destination"]}" ]\
						&& (( ${rate:-0} != ${destinationfrequency["$destination"]}))\
						) || (
						[ -n "${destinationchannels["$destination"]}" ]\
						&& (( ${channels:-0} != ${destinationchannels["$destination"]} ))
						)
						then
							sox_needed=1
						fi
					;;
					*)
						if (( disablevideo ))
						then
							decodeSox
						else
							extractAudio
							if (( ${destinationnormalize["$destination"]}))\
							|| (
							[ -n "${destinationfrequency["$destination"]}" ]\
							&& (( ${rate:-0} != ${destinationfrequency["$destination"]}))\
							) || (
							[ -n "${destinationchannels["$destination"]}" ]\
							&& (( ${channels:-0} != ${destinationchannels["$destination"]} ))
							)
							then
								sox_needed=1
							fi
						fi
					;;
				esac
			;;
		esac
		if ! (( copied ))
		then
			if ! decodetaskid=$(
				Select tasks id <<<"key = $tmpfile"
			)
			then
				decodetaskid=$(
					Insert tasks <<-EOInsert
						key		$tmpfile
						source_file	$fileid
						$(
							for key in ${!commandline[@]}
							do
								echo "cmd_arg$key ${commandline[key]}"
							done
						)
						status		0
						cleanup		$tmpfile.wav
					EOInsert
				)
				progressSpin
			fi
			if (( sox_needed ))
			then
				decodeSox "$tempdir/$tmpfile.wav"
				if ! soxtaskid=$(
					Select tasks id <<<"key = $tmpfile"
				)
				then
					parent_required=$(
						Select tasks required_by			\
							<<<"id = $decodetaskid"
					)
					Update tasks required_by $((++parent_required))	\
						<<<"id = $decodetaskid"
					soxtaskid=$(
						Insert tasks <<-EOInsert
							key		$tmpfile
							source_file	$fileid
							$(
								for key in ${!commandline[@]}
								do
									echo "cmd_arg$key ${commandline[key]}"
								done
							)
							requires	$decodetaskid
							status		0
							cleanup		$tmpfile.wav
						EOInsert
					)
					progressSpin
				fi
			fi
		fi
	fi
}
