#!/bin/sh
set -eu
# minci uses a restrictive umask; Debian control/payload defaults must stay public.
umask 027
root=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
if [ "$(uname -s)" != Linux ] || ! command -v dpkg-deb >/dev/null 2>&1; then
 echo 'SKIP: real Debian backend checks require Linux and dpkg-deb'; exit 0
fi
work=$(mktemp -d)
cleanup() {
 result=$?
 if [ "$result" != 0 ]; then
  for log in "$work"/*.log; do [ ! -f "$log" ] || tail -60 "$log" >&2; done
 fi
 rm -rf "$work"
}
trap cleanup 0
mkdir -p "$work/recipe"
cat > "$work/recipe/APKBUILD" <<'RECIPE'
pkgname=deb-fixture
pkgver=1.0
pkgrel=2
deb_pkgver=1.0
pkgdesc="Debian backend fixture"
url="https://example.invalid"
source=""
build() { :; }
package() {
 mkdir -p "$pkgdir/usr/bin"
 printf '#!/bin/sh\necho fixture\n' > "$pkgdir/usr/bin/deb-fixture"
 chmod 755 "$pkgdir/usr/bin/deb-fixture"
 mkdir -p "$pkgdir/usr/share/deb-fixture"
 for name in a b c; do printf '%s\n' "$name" > "$pkgdir/usr/share/deb-fixture/$name"; done
}
RECIPE
"$root/scripts/build.sh" -f deb -t x86_64-unknown-linux-gnu -o "$work/out" "$work/recipe/APKBUILD" > "$work/build.log" 2>&1
file=$work/out/x86_64-unknown-linux-gnu/deb-fixture_1.0-2_amd64.deb
[ "$(dpkg-deb -f "$file" Package)" = deb-fixture ]
[ "$(dpkg-deb -f "$file" Version)" = 1.0-2 ]
[ "$(dpkg-deb -f "$file" Architecture)" = amd64 ]
dpkg-deb -c "$file" > "$work/contents"
grep -q 'root/root.*usr/bin/deb-fixture' "$work/contents"
[ "$(sha256sum "$file" | awk '{print $1}')" = "$(awk 'NR==1{print $1}' "$file.sha256")" ]
dpkg-deb -e "$file" "$work/control"
LC_ALL=C sort -c "$work/control/md5sums"
sleep 2
"$root/scripts/build.sh" -f deb -t x86_64-unknown-linux-gnu -o "$work/repeated" "$work/recipe/APKBUILD" > "$work/repeated.log" 2>&1
cmp "$file" "$work/repeated/x86_64-unknown-linux-gnu/deb-fixture_1.0-2_amd64.deb"
if "$root/scripts/build.sh" -f deb -t aarch64-unknown-linux-gnu -o "$work/rejected" "$work/recipe/APKBUILD" >/dev/null 2>&1; then exit 1; fi
awk '!/^deb_pkgver=/' "$work/recipe/APKBUILD" > "$work/recipe/no-opt-in"
if "$root/scripts/build.sh" -f deb -t x86_64-unknown-linux-gnu -o "$work/rejected" "$work/recipe/no-opt-in" >/dev/null 2>&1; then exit 1; fi
# Exercise the same publication/status interface CI uses, not just dpkg-deb.
mkdir -p "$work/repo/Unix/deb-fixture"
cp -R "$root/scripts" "$work/repo/"
cp "$work/recipe/APKBUILD" "$work/repo/Unix/deb-fixture/APKBUILD"
(cd "$work/repo" && git init -q && git add . && git -c user.name=Test -c user.email=test@example.invalid commit -qm fixture)
rev=$(git -C "$work/repo" rev-parse HEAD)
export FPKGS_ROOT=$work/repo FPKGS_WORK_ROOT=$work/ci FPKGS_DL_ROOT=$work/published
export FPKGS_FORMAT=deb FPKGS_PACKAGES=deb-fixture FPKGS_TARGETS=x86_64-unknown-linux-gnu
"$root/scripts/fpkgs-ci.sh" run "$rev" > "$work/ci.log" 2>&1
"$root/scripts/fpkgs-ci.sh" status deb-fixture x86_64-unknown-linux-gnu "$rev" > "$work/status"
grep -q '^format=deb$' "$work/status"
artifact=$(awk -F= '$1=="artifact_path" {print substr($0,index($0,"=")+1)}' "$work/status")
printf 'corrupt' >> "$artifact"
result=0
"$root/scripts/fpkgs-ci.sh" status deb-fixture x86_64-unknown-linux-gnu "$rev" > "$work/corrupt" || result=$?
[ "$result" = 2 ]
"$root/scripts/fpkgs-ci.sh" run "$rev" > "$work/rebuild.log" 2>&1
"$root/scripts/fpkgs-ci.sh" status deb-fixture x86_64-unknown-linux-gnu "$rev" > /dev/null
# Architecture-independent recipes use the legacy noarch staging directory,
# but retain the requested Debian publication target and Architecture: all.
printf '\narch="noarch"\n' >> "$work/recipe/APKBUILD"
"$root/scripts/build.sh" -f deb -t x86_64-unknown-linux-gnu -o "$work/noarch" "$work/recipe/APKBUILD" > "$work/noarch.log" 2>&1
all=$work/noarch/x86_64-unknown-linux-gnu/deb-fixture_1.0-2_all.deb
[ "$(dpkg-deb -f "$all" Architecture)" = all ]
dpkg-deb -c "$all" > "$work/all-contents"
grep -q 'root/root.*usr/bin/deb-fixture' "$work/all-contents"
echo 'PASS: rootless Debian metadata/ownership, noarch staging, checksums, target/opt-in guards, publication, status and corrupt-cache rebuild'
