#!/usr/bin/env bash

sanityCheck() {
	if [ ! -d "$tempdir" ] && ! mkdir -p "$tempdir"
	then
		echo "[FATAL] Could not create temp directory $tempdir" >&2
		(( sanityfail++ ))
	fi
	if [ ! -f "$database" ] && [ ! -d "${database%/*}" ]	\
	&& ! mkdir -p "${database%/*}"
	then
		echo "[FATAL] Directory holding database file does not exist"\
		"and could not be created" >&2
		(( sanityfail++ ))
	fi
	if [ ! -d "$sourcepath" ]
	then
		echo "[FATAL] Source path $sourcepath does not exist or is"\
		"not a directory" >&2
		(( sanityfail++ ))
	fi
	if ! which sed >/dev/null
	then
		echo "[FATAL] Required tool sed is not installed or not in PATH
		I never thought this would actually hit someone..." >&2
		(( sanityfail++ ))
	fi
	if ! which sox >/dev/null
	then
		echo "[FATAL] Required tool sox is not installed or not in"\
		"PATH" >&2
		(( sanityfail++ ))
	fi
	if ! which ogginfo >/dev/null
	then
		echo "[WARNING] Tool ogginfo (from vorbis-tools) is not"\
		"installed or not in PATH
		WebM metadata disabled" >&2
		disableogginfo=1
		(( sanitywarn++ ))
	fi
	if ! which soxi >/dev/null
	then
		echo "[WARNING] Tool soxi (from sox) is not"	\
		"installed or not in PATH
		Vorbis metadata disabled" >&2
		disablesoxi=1
		(( sanitywarn++ ))
	fi
	if (( oggencneeded )) && ! which oggenc >/dev/null
	then
		echo "[WARNING] Tool oggenc (from vorbis-tools) is not"	\
		"installed or not in PATH
		Vorbis targets disabled" >&2
		disableoggenc=1
		(( sanitywarn++ ))
	fi
	if ! which opusinfo >/dev/null
	then
		echo "[WARNING] Tool opusinfo (from opus-tools) is not"	\
		"installed or not in PATH
		Opus metadata disabled" >&2
		disableopusinfo=1
		(( sanitywarn++ ))
	fi
	if (( opusencneeded )) && ! which opusenc >/dev/null
	then
		echo "[WARNING] Tool opusenc (from opus-tools) is not"	\
		"installed or not in PATH
		Opus targets disabled" >&2
		disableopusenc=1
		(( sanitywarn++ ))
	fi
	if ! which opusdec >/dev/null
	then
		echo "[WARNING] Tool opusdec (from opus-tools) is not"	\
		"installed or not in PATH
		Opus support disabled" >&2
		disableopusdec=1
		(( sanitywarn++ ))
	fi
	if (( lameneeded )) && ! which lame >/dev/null
	then
		echo "[WARNING] Tool lame is not installed or not in PATH
		MP3 targets disabled" >&2
		disablelame=1
		(( sanitywarn++ ))
	fi
	if ! which metaflac >/dev/null
	then
		echo "[WARNING] Tool metaflac (from FLAC) is not installed"\
		"or not in PATH
		FLAC metadata disabled" >&2
		disableflac=1
		(( sanitywarn++ ))
	fi
	if ! which mpcdec >/dev/null
	then
		echo "[WARNING] Tool mpcdec (from Musepack) is not"	\
		"installed or not in PATH
		Musepack support disabled" >&2
		disablempcdec=1
		(( sanitywarn++ ))
	fi
	if ! which mkvextract >/dev/null
	then
		echo "[WARNING] Tool mkvextract (from MKVToolNix) is not"\
		"installed or not in PATH
		WebM metadata disabled
		WebM support disabled" >&2
		disablemkvextract=1
		(( sanitywarn++ ))
	fi
	if ! which ffprobe >/dev/null
	then
		echo "[WARNING] Tool ffprobe (from FFmpeg) is not installed"\
		"or not in PATH
		Video metadata disabled
		MPEG metadata disabled
		MusePack metadata disabled
		Unknown format metadata disabled" >&2
		disableffprobe=1
		(( sanitywarn++ ))
	fi
	if ! which ffmpeg >/dev/null
	then
		echo "[WARNING] Tool ffmpeg is not installed or not in PATH
		Video support disabled" >&2
		disablevideo=1
		(( sanitywarn++ ))
	fi
	if (( textunidecodeneeded )) && ! perl -MText::Unidecode -e 'exit;' 2>/dev/null
	then
		echo "[WARNING] Perl module Text::Unidecode is not available
		Renaming to ASCII-only disabled" >&2
		unset destinationascii
		destinationascii=0
		textunidecodeneeded=0
		(( sanitywarn++ ))
	fi
	if (( sanityfail ))
	then
		echo "
Sanity checks raised ${sanitywarn:-0} warnings, $sanityfail failures. Dying now." >&2
		exit $ESANITY
	elif (( sanitywarn ))
	then
		echo "
Sanity checks raised $sanitywarn warnings... Hit Control-C to abort." >&2
		if ! (( cron ))
		then
			timeout=$(( sanitywarn * 10 ))
			echo -n "Starting in $(printf %3i $timeout)"	\
				$'seconds...\b\b\b\b\b\b\b\b\b\b\b' >&2
			while (( timeout ))
			do
				echo -n $'\b\b\b'"$(printf %3i $timeout)" >&2
				sleep 1
				(( timeout-- ))
			done
			echo -en "\r\033[K"
		fi
	fi
}

# vim:set ts=8 sw=8:
