#!/bin/sh
#
# Verify that the acustic fingerprint of a given audio file can be calculated.
#
# TODO: maybe switch to something like "sharness" for simplified test definitions
#

set -eu


EXAMPLE_FILE="/usr/share/sounds/freedesktop/stereo/alarm-clock-elapsed.oga"
EXAMPLE_MD5="5e5b9522a7cf44101f66154d3b043bd4"
EXAMPLE_ACOUSTID_FINGERPRINT="AQAAHImSSEqSZFGiCLjxw9bxw8V9fMeJ-zhs4YeL48eJzzi8CSfM4DieAfcB5ZQ0QkgBjRLOCIegIeY5c5QA"

# verify that the example has not changed since the test was defined
if ! echo "$EXAMPLE_MD5  $EXAMPLE_FILE" | md5sum --check; then
	echo >&2 "The MD5 hash sum of the example file ($EXAMPLE_FILE) seems to have changed."
	echo >&2 "Probably the test needs to be updated with the current hash and acoustic fingerprint."
	exit 1
fi


PYTHON_SNIPPET_FINGERPRINT="import acoustid; print(acoustid.fingerprint_file('$EXAMPLE_FILE')[1].decode())"
calculated_python3_fingerprint=$(python3 -c "$PYTHON_SNIPPET_FINGERPRINT")
if [ "$calculated_python3_fingerprint" != "$EXAMPLE_ACOUSTID_FINGERPRINT" ]; then
	echo >&2 "Not all generated fingerprints match the expected fingerprint for example file ($EXAMPLE_FILE):"
	echo >&2 "  expected fingerprint  : $EXAMPLE_ACOUSTID_FINGERPRINT"
	echo >&2 "  calculated via python3: $calculated_python3_fingerprint"
	exit 1
fi
