#!/usr/bin/env bash
encodeFile::mp3() {
	lameopts=(${ionice}lame --quiet --noreplaygain)
	lameopts+=(-v --abr ${destinationquality[$destination]})
	[ -n "$album"	] && lameopts+=(--tl "$album" )
	[ -n "$artist"	] && lameopts+=(--ta "$artist")
	[ -n "$genre"	] && lameopts+=(--tg "$genre")
	[ -n "$title"	] && lameopts+=(--tt "$title")
	[ -n "$track"	] && lameopts+=(--tn "$track")
	[ -n "$year"	] && lameopts+=(--ty "$year")
	[ -n "$albumartist" ] && lameopts+=(--tv TPE2="$albumartist")
	[ -n "$composer" ] && lameopts+=(--tv TCOM="$composer")
	[ -n "$performer" ] && lameopts+=(--tv TOPE="$performer")
	[ -n "$rating" ] && lameopts+=(--tv FMPS_RATING="$rating")
	[ -n "$releasecountry" ] \
		&& lameopts+=(--tv TXXX="MusicBrainz Album Release Country=$releasecountry")
	[ -n "$replaygain_alb" ] \
		&& lameopts+=(--tv "TXXX=REPLAYGAIN_ALBUM_GAIN=$replaygain_alb")
	[ -n "$replaygain_trk" ] \
		&& lameopts+=(--tv "TXXX=REPLAYGAIN_TRACK_GAIN=$replaygain_trk")
	[ -n "$disc"	] && lameopts+=(--tv TPOS="$disc")
	if (( ${destinationnoresample[$destination]:-0} == 1 ))
	then
		# If 'rate' is not one of these value, it cannot be encoded to
		# MP3, in which case we'd be better of letting lame decide which
		# rate to use.
		if [ -n "${destinationfrequency["$destination"]}" ]
		then
			case ${destinationfrequency["$destination"]} in
				48000)	lameopts+=(--resample 48)	;;
				44100)	lameopts+=(--resample 44.1)	;;
				32000)	lameopts+=(--resample 32)	;;
				24000)	lameopts+=(--resample 24)	;;
				22050)	lameopts+=(--resample 22.05)	;;
				16000)	lameopts+=(--resample 16)	;;
				12000)	lameopts+=(--resample 12)	;;
				11025)	lameopts+=(--resample 11.025)	;;
				8000)	lameopts+=(--resample 8)	;;
			esac
		elif (( rate > 48000 ))
		then
			lameopts+=(--resample 48)
		else
			case $rate in
				48000)	lameopts+=(--resample 48)	;;
				44100)	lameopts+=(--resample 44.1)	;;
				32000)	lameopts+=(--resample 32)	;;
				24000)	lameopts+=(--resample 24)	;;
				22050)	lameopts+=(--resample 22.05)	;;
				16000)	lameopts+=(--resample 16)	;;
				12000)	lameopts+=(--resample 12)	;;
				11025)	lameopts+=(--resample 11.025)	;;
				8000)	lameopts+=(--resample 8)	;;
			esac
		fi
	fi
	lameopts+=("$tempdir/$tmpfile.wav" "${destinationpath[$destination]}/$destdir/$destfile.mp3")
	encodetaskid=$(
		Insert tasks <<-EOInsert
			key		${fileid}lame$destination
			requires	${soxtaskid:-$decodetaskid}
			fileid		$destfileid
			filename	$destdir/$destfile.mp3
			$(
				for key in ${!lameopts[@]}
				do
					cleanedopts="${lameopts[key]//\&/\\\&}"
					cleanedopts="${cleanedopts//\[/\\[}"
					cleanedopts="${cleanedopts//\]/\\]}"
					cleanedopts="${cleanedopts//\{/\\{}"
					cleanedopts="${cleanedopts//\}/\\\}}"
					echo "cmd_arg$key $cleanedopts"
				done
			)
			source_file	$fileid
			status		0
			rename_pattern	${destinationrenamepath[$destination]}/${destinationrename[$destination]}
			fat32compat	${destinationfat32compat["$destination"]}
			ascii		${destinationascii["$destination"]}
		EOInsert
	)
	parent_required=$(
		Select tasks required_by			\
			<<<"id = ${soxtaskid:-$decodetaskid}"
	)
	Update tasks required_by $((++parent_required))	\
		<<<"id = ${soxtaskid:-$decodetaskid}"
	progressSpin
	soxtaskid=''
}
