#!/bin/sh
# Build and run the upstream test suite against the installed Debian crates,
# the same way debian/rules does at build time.
set -eu

src="$AUTOPKGTEST_TMP/src"
cp -a . "$src"
cd "$src"
rm -f Cargo.lock
rm -rf target debian/cargo_home debian/cargo_registry

eval "$(dpkg-buildflags --export=sh)"
export CARGO=/usr/share/cargo/bin/cargo
export CARGO_HOME="$src/debian/cargo_home"
export CARGO_TARGET_DIR="$src/target"
export DEB_CARGO_CRATE="crumb_$(dpkg-parsechangelog -SVersion | sed 's/-[^-]*$//')"
export CARGO_REGISTRY="$src/debian/cargo_registry"
export DEB_HOST_GNU_TYPE="$(dpkg-architecture -qDEB_HOST_GNU_TYPE)"
export DEB_HOST_RUST_TYPE="$(printf 'include /usr/share/rustc/architecture.mk\nall:\n\t@echo $(DEB_HOST_RUST_TYPE)\n' | make -s -f -)"

mkdir -p "$CARGO_HOME" "$CARGO_REGISTRY"
$CARGO prepare-debian "$CARGO_REGISTRY" --link-from-system
$CARGO test -p crumb-cli --no-default-features --features debian --target "$DEB_HOST_RUST_TYPE"
