#!/bin/sh
# Offline contracts by default; FPKGS_TEST_REAL_APK=1 exercises real signed APKs.
set -eu
root=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
(
	. "$root/scripts/functions.sh"
	unset FPKGS_FORMAT
	case "$(canonical_os)" in
	Linux)
		[ "$(package_format)" = apk ]
		case "$(default_target apk)" in *-alpine-linux-musl) ;; *) exit 1 ;; esac ;;
	*) [ "$(package_format)" = tar.gz ] ;;
	esac
	FPKGS_FORMAT=tar.gz
	[ "$(package_format)" = tar.gz ]
	[ "$(default_target tar.gz)" = "$(guess_triplet)" ]
)
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
trap 'exit 1' HUP INT TERM
mkdir -p "$work/repo/noarch/fixture" "$work/repo/scripts" "$work/bin"
cp "$root/scripts/"*.sh "$work/repo/scripts/"
chmod +x "$work/repo/scripts/"*.sh
cat >"$work/repo/noarch/fixture/APKBUILD" <<'RECIPE'
pkgname=fixture
pkgver=1.0
pkgrel=0
pkgdesc="Disposable APK backend test"
url="https://example.invalid"
arch="noarch"
license="MIT"
subpackages="$pkgname-data"
source=""
builddir="$srcdir"
build() {
	if cross_compiling; then echo cross >"$srcdir/checked"; fi
}
check() { echo checked >"$srcdir/checked"; }
package() {
	mkdir -p "$pkgdir/usr/share/fixture"
	cp "$srcdir/checked" "$pkgdir/usr/share/fixture/checked"
}
data() {
	mkdir -p "$subpkgdir/usr/share/fixture-data"
	echo data >"$subpkgdir/usr/share/fixture-data/content"
}
RECIPE
if [ "${FPKGS_TEST_REAL_APK:-0}" != 1 ]; then
	cat >"$work/bin/abuild" <<'STUB'
#!/bin/sh
set -eu
[ "$1" != -V ] || { echo 'abuild contract-test stub'; exit; }
[ "$#" -eq 1 ] && [ "$1" = -d ]
[ -n "$CBUILD" ] && [ "$CHOST" = x86_64-alpine-linux-musl ]
[ -z "${FPKGS_TEST_EXPECT_CBUILD:-}" ] || [ "$CBUILD" = "$FPKGS_TEST_EXPECT_CBUILD" ]
[ -n "$SRCDEST" ]
for name in srcdir pkgdir subpkgdir tmpdir startdir builddir pkgbasedir workdir destdir; do
	eval value=\${$name-}
	[ -z "$value" ] || { echo "Inherited CI staging variable: $name" >&2; exit 1; }
done
touch generated-only-in-copy
out=$REPODEST/noarch/x86_64
mkdir -p "$out"
printf main >"$out/fixture-1.0-r0.apk"
printf split >"$out/fixture-data-1.0-r0.apk"
printf index >"$out/APKINDEX.tar.gz"
STUB
	chmod +x "$work/bin/abuild"
	export FPKGS_ABUILD=$work/bin/abuild
	# Native musl must not be mistaken for cross compilation just due to vendor.
	mkdir -p "$work/platform-bin"
	printf '#!/bin/sh\ncase "$1" in -s) echo Linux ;; -m) echo x86_64 ;; esac\n' >"$work/platform-bin/uname"
	printf '#!/bin/sh\necho musl\n' >"$work/platform-bin/ldd"
	chmod +x "$work/platform-bin/"*
	(unset FPKGS_FORMAT; PATH="$work/platform-bin:$PATH" FPKGS_TEST_EXPECT_CBUILD=x86_64-alpine-linux-musl sh "$root/scripts/build.sh" -o "$work/musl-dist" "$work/repo/noarch/fixture/APKBUILD") >"$work/musl.log" 2>&1 || { cat "$work/musl.log"; exit 1; }
fi
export FPKGS_ROOT=$work/repo FPKGS_WORK_ROOT=$work/cache FPKGS_DL_ROOT=$work/published
export FPKGS_DL_BASE_URL=https://artifacts.invalid
export FPKGS_FORMAT=apk FPKGS_TARGETS=x86_64-alpine-linux-musl
if [ "${FPKGS_TEST_REAL_APK:-0}" = 1 ]; then
	# Upstream intentionally skips execution checks for cross builds.
	FPKGS_TARGETS=$(. "$root/scripts/functions.sh"; guess_triplet)
fi
mkdir -p "$FPKGS_DL_ROOT"
cd "$work/repo"
git init -q
git add .
git -c user.name=Test -c user.email=test@example.invalid commit -qm fixture
rev=$(git rev-parse HEAD)
cli=$root/scripts/fpkgs-ci.sh
apk_key=$(sh "$cli" key noarch/fixture/APKBUILD "$FPKGS_TARGETS" "$rev")
tar_key=$(FPKGS_FORMAT=tar.gz sh "$cli" key noarch/fixture/APKBUILD "$FPKGS_TARGETS" "$rev")
[ "$apk_key" != "$tar_key" ]
systemd_key=$(FPKGS_INIT_SYSTEM=systemd sh "$cli" key noarch/fixture/APKBUILD "$FPKGS_TARGETS" "$rev")
openrc_key=$(FPKGS_INIT_SYSTEM=openrc sh "$cli" key noarch/fixture/APKBUILD "$FPKGS_TARGETS" "$rev")
[ "$systemd_key" != "$openrc_key" ]
FPKGS_PACKAGES=not-selected sh "$cli" plan "$rev" >"$work/filter"
[ ! -s "$work/filter" ]
FPKGS_PACKAGES=fixture sh "$cli" plan "$rev" >"$work/filter"
[ "$(wc -l <"$work/filter" | tr -d ' ')" -eq 1 ]
mkdir -p "$work/minci-source" "$work/minci-pkg" "$work/minci-work"
printf sentinel >"$work/minci-source/keep"
srcdir="$work/minci-source" pkgdir="$work/minci-pkg" workdir="$work/minci-work" \
	sh "$cli" run "$rev" >"$work/run.log" 2>&1 || {
	cat "$work/run.log"
	find "$FPKGS_WORK_ROOT/build" -name build.log -exec tail -n 100 {} \;
	exit 1
}
[ ! -f noarch/fixture/generated-only-in-copy ]
[ "$(cat "$work/minci-source/keep")" = sentinel ]
sh "$cli" status fixture "$FPKGS_TARGETS" "$rev" >"$work/status"
grep -q '^format=apk$' "$work/status"
grep -q '/noarch/x86_64/fixture-1.0-r0.apk$' "$work/status"
archive=$(awk -F= '$1=="artifact_path" {print $2}' "$work/status")
out=${archive%/noarch/x86_64/*}
[ -f "$out/noarch/x86_64/fixture-data-1.0-r0.apk" ]
[ -f "$out/noarch/x86_64/APKINDEX.tar.gz" ]
if [ "${FPKGS_TEST_REAL_APK:-0}" = 1 ]; then
	${APK:-apk} verify "$archive" "$out/noarch/x86_64/fixture-data-1.0-r0.apk"
	tar -xOzf "$archive" usr/share/fixture/checked | grep -q '^checked$'
	# Also exercise the actual Linux default, including upstream cross semantics.
	(unset FPKGS_FORMAT; sh scripts/build.sh -o "$work/default-dist" noarch/fixture) >"$work/default.log" 2>&1 || { cat "$work/default.log"; exit 1; }
	default_archive=$(find "$work/default-dist" -name fixture-1.0-r0.apk)
	case "$default_archive" in */x86_64-alpine-linux-musl/*) ;; *) exit 1 ;; esac
	${APK:-apk} verify "$default_archive"
	tar -xOzf "$default_archive" usr/share/fixture/checked | grep -q '^cross$'
fi
sh "$cli" plan "$rev" >"$work/plan"
grep -q '^skip ' "$work/plan"
printf corruption >>"$out/noarch/x86_64/fixture-data-1.0-r0.apk"
code=0
sh "$cli" status fixture "$FPKGS_TARGETS" "$rev" >"$work/status" || code=$?
[ "$code" -eq 2 ]
sh "$cli" plan "$rev" >"$work/plan"
grep -q '^build ' "$work/plan"
sh "$cli" run "$rev" >"$work/rebuild.log" 2>&1 || { cat "$work/rebuild.log"; exit 1; }
sh "$cli" status fixture "$FPKGS_TARGETS" "$rev" >/dev/null
printf corruption >>"$out/noarch/x86_64/APKINDEX.tar.gz"
code=0
sh "$cli" status fixture "$FPKGS_TARGETS" "$rev" >"$work/status" || code=$?
[ "$code" -eq 2 ]
if sh scripts/build.sh -f apk -s "$work/unused" noarch/fixture >"$work/rejected" 2>&1; then
	echo 'Legacy dependency staging unexpectedly accepted for APK' >&2; exit 1
fi
if sh scripts/build.sh -f invalid noarch/fixture >"$work/rejected" 2>&1; then
	echo 'Invalid package format unexpectedly accepted' >&2; exit 1
fi
if FPKGS_FORMAT=invalid sh "$cli" plan "$rev" >"$work/rejected" 2>&1; then
	echo 'Invalid CI package format unexpectedly accepted' >&2; exit 1
fi
# A missing tool must fail without creating a successful publication.
before=$(find "$FPKGS_DL_ROOT" -name manifest | wc -l)
if FPKGS_ABUILD="$work/absent" sh "$cli" run "$rev" >"$work/failed.log" 2>&1; then
	echo 'Missing abuild unexpectedly succeeded' >&2; exit 1
fi
[ "$before" = "$(find "$FPKGS_DL_ROOT" -name manifest | wc -l)" ]
# Keep the legacy backend and its flat publication layout working on Linux too.
cat >noarch/fixture/APKBUILD <<'LEGACY'
pkgname=fixture
pkgver=1.0
pkgrel=0
arch=noarch
source=""
build() { :; }
package() {
	mkdir -p "$pkgdir/usr/share/fixture"
	echo legacy >"$pkgdir/usr/share/fixture/content"
}
LEGACY
git add noarch/fixture/APKBUILD
git -c user.name=Test -c user.email=test@example.invalid commit -qm legacy
rev=$(git rev-parse HEAD)
export FPKGS_FORMAT=tar.gz FPKGS_TARGETS=x86_64-unknown-linux-gnu
sh "$cli" run "$rev" >"$work/legacy.log" 2>&1 || { cat "$work/legacy.log"; exit 1; }
sh "$cli" status fixture "$FPKGS_TARGETS" "$rev" >"$work/status"
grep -q '^format=tar.gz$' "$work/status"
archive=$(awk -F= '$1=="artifact_path" {print $2}' "$work/status")
[ -f "$archive" ]
[ -f "${archive%/*}/manifest" ]
echo 'PASS: APK/tar dispatch, split/index publication, format-sensitive keys, verified cache, corruption/rebuild, isolation, tool failures'
