#!/bin/bash
getDestDir() {
	if [ -n "${destinationrenamepath[$destination]}" ]		\
	&& (
		(
			[[ ${destinationrenamepath[$destination]} ==	\
				*?([^[])%\{album\}?([^\]])* ]]		\
			&& [ -n "$album" ]
		) || (
			[[ ${destinationrenamepath[$destination]} ==	\
				*?([^[])%\{albumartist\}?([^\]])* ]]	\
			&& [ -n "$albumartist" ]
		) || (
			[[ ${destinationrenamepath[$destination]} ==	\
				*?([^[])%\{releasecountry\}?([^\]])* ]]		\
			&& [ -n "$releasecountry" ]
		) || (
			[[ ${destinationrenamepath[$destination]} ==	\
				*?([^[])%\{artist\}?([^\]])* ]]		\
			&& [ -n "$artist" ]
		) || (
			[[ ${destinationrenamepath[$destination]} ==	\
				*?([^[])%\{genre\}?([^\]])* ]]		\
			&& [ -n "$genre" ]
		) || (
			[[ ${destinationrenamepath[$destination]} ==	\
				*?([^[])%\{title\}?([^\]])* ]]		\
			&& [ -n "$title" ]
		) || (
			[[ ${destinationrenamepath[$destination]} ==	\
				*?([^[])%\{track\}?([^\]])* ]]		\
			&& [ -n "$track" ]
		) || (
			[[ ${destinationrenamepath[$destination]} ==	\
				*?([^[])%\{year\}?([^\]])* ]]		\
			&& [ -n "$year" ]
		) || (
			[[ ${destinationrenamepath[$destination]} ==	\
				*?([^[])%\{disc\}?([^\]])* ]]		\
			&& [ -n "$disc" ]
		)
	)
	then
		destdir=""
		if (( ${destinationascii["$destination"]} ))
		then
			echo "$album" >&${toascii[1]}
			read -r -u${toascii[0]} album
			echo "$albumartist" >&${toascii[1]}
			read -r -u${toascii[0]} albumartist
			echo "$releasecountry" >&${toascii[1]}
			read -r -u${toascii[0]} releasecountry
			echo "$artist" >&${toascii[1]}
			read -r -u${toascii[0]} artist
			echo "$genre" >&${toascii[1]}
			read -r -u${toascii[0]} genre
			echo "$title" >&${toascii[1]}
			read -r -u${toascii[0]} title
			echo "$tracknumber" >&${toascii[1]}
			read -r -u${toascii[0]} tracknumber
			echo "$year" >&${toascii[1]}
			read -r -u${toascii[0]} year
			echo "$disc" >&${toascii[1]}
			read -r -u${toascii[0]} disc
		fi
		replace=$(sanitizeFile "$album" dir)
		destdir+="${destinationrenamepath[$destination]//?(\[)%\{album\}?(\])/$replace}"
		replace=$(sanitizeFile "$albumartist" dir)
		destdir="${destdir//?(\[)%\{albumartist\}?(\])/$replace}"
		replace=$(sanitizeFile "$releasecountry" dir)
		destdir="${destdir//?(\[)%\{releasecountry\}?(\])/$releasecountry}"
		replace=$(sanitizeFile "$artist" dir)
		destdir="${destdir//?(\[)%\{artist\}?(\])/$replace}"
		replace=$(sanitizeFile "$genre" dir)
		destdir="${destdir//?(\[)%\{genre\}?(\])/$replace}"
		replace=$(sanitizeFile "$title" dir)
		destdir="${destdir//?(\[)%\{title\}?(\])/$replace}"
		tracknumber="${track%/*}"
		replace=$(sanitizeFile "$tracknumber" dir)
		destdir="${destdir//?(\[)%\{track\}?(\])/$replace}"
		replace=$(sanitizeFile "$year" dir)
		destdir="${destdir//?(\[)%\{year\}?(\])/$replace}"
		replace=$(sanitizeFile "$disc" dir)
		destdir="${destdir//?(\[)%\{disc\}?(\])/$replace}"
	else
		destdir=$(sanitizeFile "${filename%%/*}" dir)
		part=${filename#*/}
		while [[ $part =~ / ]]
		do
			thispart="${part%%/*}"
			thispart=${thispart//$'\n'/::AtOM:NewLine:SQL:Inline::}
			if (( ${destinationascii["$destination"]} ))
			then
				echo "$thispart" >&${toascii[1]}
				read -r -u${toascii[0]} thispart
			fi
			sanitized="$(sanitizeFile "$thispart" dir)"
			destdir+="${sanitized:+/}$sanitized"
			part=${part#*/}
		done
	fi
	if ! [ -d "${destinationpath[$destination]}/$destdir" ]
	then
		mkdir -p "${destinationpath[$destination]}/${destdir//::AtOM:NewLine:SQL:Inline::/$'\n'}"
	fi
	destdir="${destdir//+(\/)//}"
}
