#!/bin/bash
set -euox pipefail

RUST_VERSION=$(dpkg-parsechangelog -SVersion | sed -re 's/([^.]+)\.([^.]+)\..*/\1.\2/')

# Set RUSTC so that `cargo` knows which rustc to use
export RUSTC=/usr/lib/rust-${RUST_VERSION}/bin/rustc
CARGO=/usr/lib/rust-${RUST_VERSION}/bin/cargo

tmpdir=$(mktemp -d)
cd "$tmpdir"

${CARGO} new hello
cd hello

cat <<EOF >src/main.rs
use anyhow::Result;

fn main() -> Result<()> {
    println!("Hello, World!");
    Ok(())
}
EOF

${CARGO} add 'anyhow@^1'
${CARGO} vendor

mkdir -p .cargo
cat <<EOF >.cargo/config.toml
[source.crates-io]
replace-with = "vendored-sources"

[source.vendored-sources]
directory = "vendor"
EOF

# Build both wasi targets
rc=0
for target in wasm32-wasip1 wasm32-wasip2; do
    if ! ${CARGO} build --target "$target"; then
        echo "$target build failed" >&2
        rc=1
    fi
done
exit $rc
