# -*- shell-script -*-

FUNCTIONS_DIR="debian/tests/functions.d"

error_exit () {
	echo "ERROR: $1"
	exit 1
}

line_count () {
	wc -l $1 | cut -d' ' -f1
}

match_or_exit () {
	file_to_match="$1"
	pattern_file="$2"

	if [ $(line_count "${file_to_match}") -ne $(line_count "${pattern_file}") ]; then
		error_exit "line count of ${file_to_match} ($(line_count "${file_to_match}")) and pattern ($(line_count "${pattern_file}")) do not match"
	fi
	while read line_to_match <&3 && read pattern_line <&4 ; do
		if [ "${line_to_match##$pattern_line}" ]; then
			echo '!!! MISMATCH !!!' >&2
			echo "Line:    ${line_to_match}" >&2
			echo "Pattern: ${pattern_line}" >&2
			exit 1
		fi;
	done 3<"${file_to_match}" 4<"${pattern_file}"
}

check_cdrdao_version_string () {
	echo ~~~ Checking cdrdao version string ...
	cdrdao version 2>&1 | tee version.out
	match_or_exit version.out $FUNCTIONS_DIR/cdrdao-version.patterns
}

check_cdrdao_help_page () {
	echo ~~~ Checking cdrdao help page ...
	cdrdao 2>&1 | tee help.out
	match_or_exit help.out $FUNCTIONS_DIR/cdrdao-help.patterns
}

check_toc2cue_version_string () {
	echo ~~~ Checking toc2cue version string ...
	toc2cue -V 2>&1 | tee version.out
	match_or_exit version.out $FUNCTIONS_DIR/toc2cue-version.patterns
}

check_toc2cddb_version_string () {
	echo ~~~ Checking toc2cddb version string ...
	toc2cddb -V 2>&1 | tee version.out
	match_or_exit version.out $FUNCTIONS_DIR/toc2cddb-version.patterns
}

is_drive_available () {
	echo ~~~ Reading Drive Info ...
	cdrdao drive-info 2>&1
	return $?
}

get_disk_info () {
	echo ~~~ Reading Disk Info ...
	cdrdao disk-info 2>&1
}
