#!/bin/sh
# SPDX-License-Identifier: 0BSD

trap 'umount "${tmpd}m" 2>/dev/null; rm -rf "$tmpd"' EXIT INT
tmpd="$(mktemp -d)/"


ar r "${tmpd}ar" archivemount.cpp archivemount.1.in 2>/dev/null
mkdir "${tmpd}m"
./archivemount -f "${tmpd}ar" "${tmpd}m" & sleep 0.1
for f in archivemount.cpp archivemount.1.in; do
	cmp "$f" "${tmpd}m/$f" || exit
done
umount "${tmpd}m"
wait $! || exit


tar -cf "${tmpd}tar" archivemount.cpp archivemount.1.in
./archivemount -f "${tmpd}tar" "${tmpd}m" & sleep 0.1
for f in archivemount.cpp archivemount.1.in; do
	cmp "$f" "${tmpd}m/$f" || exit
done
umount "${tmpd}m"
wait $! || exit


./archivemount -f "${tmpd}tar" "${tmpd}m" & sleep 0.1
echo gaming > "${tmpd}m/archivemount.cpp"
echo gaming > "${tmpd}m/newfile"
checktrunc() {
	for f in archivemount.cpp newfile; do
		echo gaming | cmp - "${1}$f" || exit
	done
	cmp "archivemount.1.in" "${1}archivemount.1.in" || exit
}
checktrunc "${tmpd}m/"
umount "${tmpd}m"
wait $! || exit
mkdir "${tmpd}tar.unp"
(cd "${tmpd}tar.unp" && exec tar -xf -) < "${tmpd}tar"
checktrunc "${tmpd}tar.unp/"


(
	cp archivemount.cpp "${tmpd}"
	cd "${tmpd}"
	ln archivemount.cpp архивмаунт.цпп
	exec tar -cf tar archivemount.cpp архивмаунт.цпп
)
LC_ALL=C.UTF-8 ./archivemount -f "${tmpd}tar" "${tmpd}m" & sleep 0.1
cmp "${tmpd}archivemount.cpp" "${tmpd}архивмаунт.цпп" || exit
umount "${tmpd}m"
wait $! || exit
