#!/bin/sh
# Functional check of the installed foremost: embed a known HTML payload
# between non-HTML padding, then confirm foremost carves it back byte-for-byte.
# The fixture is generated here as plain text, so there is no opaque binary
# test data in the package -- the script is the source of the data.
set -e

cd "${AUTOPKGTEST_TMP:?AUTOPKGTEST_TMP is not set}"

payload='<html><body>carve bait</body></html>'

# Padding on both sides so a successful recovery proves foremost *located* the
# file (header "<html" to footer "</html>") rather than copying the whole input.
{
	echo "leading junk padding"
	printf '%s' "$payload"
	echo
	echo "trailing junk padding"
} >container.raw

# foremost refuses to write into a pre-existing output directory.
rm -rf out
foremost -t htm -o out -i container.raw

# Exactly one HTML file should have been carved, identical to the payload.
set -- out/htm/*.htm
[ "$#" -eq 1 ] && [ -f "$1" ] || {
	echo "expected exactly one carved .htm file, got: $*" >&2
	exit 1
}
printf '%s' "$payload" | cmp - "$1"

echo "PASS: foremost recovered the embedded HTML payload"
