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

guessPath() {
	# For copy files (action=2) with a rename pattern, we don't know the
	# output directory ourselves: it was determined when the audio siblings
	# were transcoded (action=1). We infer it by finding a transcoded
	# sibling in the same source directory and reading back its destination
	# path.
	#
	# First query: check whether any transcoded sibling is up to date.
	# The LIKE pattern matches files directly inside $sourcedir only
	# the NOT LIKE excludes deeper subdirectorie to avoid crossing
	# boundaries.
	echo 'SELECT IFNULL( (
		SELECT	destination_files.last_change
		FROM		destination_files
		INNER JOIN	source_files
			ON	destination_files.source_file_id=source_files.id
		INNER JOIN	mime_type_actions
			ON
			mime_type_actions.id=source_files.mime_type
		INNER JOIN	destinations
			ON	destinations.id=destination_files.destination_id
		WHERE	destinations.id = '$destinationid'
		AND	source_files.filename LIKE
				"'"${sourcedir//\"/\"\"}"'/%"
		AND NOT	source_files.filename LIKE
				"'"${sourcedir//\"/\"\"}"'/%/%"
		AND	mime_type_actions.action = 1
		ORDER BY destination_files.last_change DESC
		LIMIT 1
	),"0.0");
	'>&3
	read -u4 -r -d $'\0' timestamp
	# IFNULL returns "0.0" when no transcoded sibling exists yet; strip the
	# decimal and treat as zero to detect the no-sibling case.
	if (( ${timestamp/./} == 0 ))
	then
		# No transcoded sibling found at all
		# caller should postpone this copy.
		return 2
	fi
	# Second query: retrieve the actual destination filename of the most
	# recently updated transcoded sibling so we can derive its parent
	# directory.
	echo 'SELECT IFNULL( (
		SELECT	destination_files.filename
		FROM		destination_files
		INNER JOIN	source_files
			ON	destination_files.source_file_id=source_files.id
		INNER JOIN	mime_type_actions
			ON
			mime_type_actions.id=source_files.mime_type
		INNER JOIN	destinations
			ON	destinations.id=destination_files.destination_id
		WHERE	destinations.id = '$destinationid'
		AND	source_files.filename LIKE
				"'"${sourcedir//\"/\"\"}"'/%"
		AND NOT	source_files.filename LIKE
				"'"${sourcedir//\"/\"\"}"'/%/%"
		AND	mime_type_actions.action = 1
		ORDER BY destination_files.last_change DESC
		LIMIT 1
	),"AtOM:NotFound");
	'>&3
	read -u4 -r -d $'\0' filename
	if [[ $filename != AtOM:NotFound ]]
	then
		# Strip the filename component to return only the directory
		# portion.
		echo "${filename%/*}"
	else
		# Sibling record exists but has no usable filename — skip this
		# file.
		return 1
	fi
}
