#!/usr/bin/env bash
# Package the public crate, then prove the published source still builds, tests,
# and documents independently of this checkout. Cargo's own verification only
# builds the library target, so it cannot substitute for these extracted-source
# checks.
set -euo pipefail

repo_root="$(git rev-parse --show-toplevel)"
manifest="$repo_root/Cargo.toml"
version="$(sed -n 's/^version = "\([^"]*\)"/\1/p' "$manifest" | head -n 1)"

if [[ -z "$version" ]]; then
    echo "could not determine package version from $manifest" >&2
    exit 1
fi

cd "$repo_root"
cargo package --locked

archive="$repo_root/target/package/unsigned-float-$version.crate"
if [[ ! -f "$archive" ]]; then
    echo "cargo package did not produce $archive" >&2
    exit 1
fi

package_root="$(mktemp -d)"
trap 'rm -rf "$package_root"' EXIT
tar -xzf "$archive" -C "$package_root"
cd "$package_root/unsigned-float-$version"

cargo test --lib
cargo test --doc
RUSTDOCFLAGS="-D warnings" cargo doc --no-deps
