#!/usr/bin/env 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.

encodeFile::vorbis() {
	# Build oggenc quality-based encode command
	# (-Q = quiet, -q = quality level)
	oggencopts=(${ionice}oggenc -Q -q ${destinationquality[$destination]})
	# Embed Ogg comment tags using oggenc's -c "FIELD=value" syntax
	[ -n "$albumartist" ] && oggencopts+=(-c "ALBUMARTIST=$albumartist")
	[ -n "$album"	] && oggencopts+=(-l "$album")
	[ -n "$artist"	] && oggencopts+=(-a "$artist")
	[ -n "$composer" ] && oggencopts+=(-c "COMPOSER=$composer")
	[ -n "$disc"	] && oggencopts+=(-c "DISCNUMBER=$disc")
	[ -n "$genre"	] && oggencopts+=(-G "$genre")
	[ -n "$performer" ] && oggencopts+=(-c "PERFORMER=$performer")
	[ -n "$rating" ] && oggencopts+=(--comment "FMPS_RATING=$rating")
	[ -n "$releasecountry" ] \
		&& oggencopts+=(-c "RELEASECOUNTRY=$releasecountry")
	[ -n "$replaygain_alb" ] \
		&& oggencopts+=(-c "REPLAYGAIN_ALBUM_GAIN=$replaygain_alb")
	[ -n "$replaygain_trk" ] \
		&& oggencopts+=(-c "REPLAYGAIN_TRACK_GAIN=$replaygain_trk")
	[ -n "$title"	] && oggencopts+=(-t "$title")
	[ -n "$track"	] && oggencopts+=(-N "$track")
	[ -n "$year"	] && oggencopts+=(-d "$year")
	# -o output must come before input for oggenc
	oggencopts+=(-o "${destinationpath[$destination]}/$destdir/$destfile.ogg" "$tempdir/$tmpfile.wav")
	encodetaskid=$(
		Insert tasks <<-EOInsert
			key		${fileid}oggenc$destination
			requires	${soxtaskid:-$decodetaskid}
			fileid		$destfileid
			filename	$destdir/$destfile.ogg
			$(
				# Escape special characters that could
				# interfere with bash glob/brace expansion
				for key in ${!oggencopts[@]}
				do
					cleanedopts="${oggencopts[key]//\&/\\\&}"
					cleanedopts="${cleanedopts//\[/\\[}"
					cleanedopts="${cleanedopts//\]/\\]}"
					cleanedopts="${cleanedopts//\{/\\{}"
					cleanedopts="${cleanedopts//\}/\\\}}"
					echo "cmd_arg$key ${oggencopts[key]}"
				done
			)
			source_file	$fileid
			status		0
			rename_pattern	${destinationrenamepath[$destination]}/${destinationrename[$destination]}
			fat32compat	${destinationfat32compat["$destination"]}
			ascii		${destinationascii["$destination"]}
		EOInsert
	)
	# Increment parent task's required_by counter so it won't clean up
	# until all children finish
	parent_required=$(
		Select tasks required_by			\
			<<<"id = ${soxtaskid:-$decodetaskid}"
	)
	Update tasks required_by $((++parent_required))	\
		<<<"id = ${soxtaskid:-$decodetaskid}"
	progressSpin
	soxtaskid=''
}
