#!/bin/sh
# Build the complete unsigned APK twice and compare it byte for byte.
set -eu

root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
a=$(mktemp -d)
b=$(mktemp -d)
trap 'rm -rf "$a" "$b"' 0

[ ! -f "$root/keystore.properties" ] || {
	echo "verify-repro: keystore.properties must be absent" >&2
	exit 1
}

make -C "$root" app
cp "$root/app/build/outputs/apk/release/app-release-unsigned.apk" "$a/app.apk"

rm -rf "$root/app/build" "$root/app/src/main/jniLibs" "$root/rsync/work" "$root/out"
make -C "$root" app
cp "$root/app/build/outputs/apk/release/app-release-unsigned.apk" "$b/app.apk"

if cmp "$a/app.apk" "$b/app.apk"; then
	echo "verify-repro: APKs are bit-identical"
else
	echo "verify-repro: NONDETERMINISM detected" >&2
	exit 1
fi
