#!/usr/bin/env bash
set -e;

# Run a build with a given toolchain.
TC=${TC?Supply a toolchain in this environment variable}

echo "Rust toolchain: ${TC}"

REPO_DIR=$( cd $(dirname ${BASH_SOURCE[0]})/..; pwd )
export CARGO_TARGET_DIR="${REPO_DIR}/target/build_local/${TC}"

set -x;

rustup self update;
rustup toolchain update ${TC};

cd ${REPO_DIR}/framed;

export RUSTFLAGS="--deny warnings";

# use_std build
cargo +${TC} test -p framed --verbose --lib;

if [[ "${TC}" == "nightly" ]]; then
    # no_std build
    cargo +${TC} test -p framed --verbose --lib \
          --no-default-features --features=use_nightly;

    rustup target add --toolchain "${TC}" thumbv7em-none-eabihf

    cargo +${TC} build --verbose --lib \
        --target thumbv7em-none-eabihf \
        --no-default-features --features=use_nightly;

    # doc tests require nightly
    cargo +${TC} test -p framed --verbose --doc \
          --features=use_nightly;
fi

cargo +${TC} test -p framed --verbose --lib --features=trace;
cargo +${TC} doc  -p framed --verbose --no-deps;

if [[ "${TC}" == "stable" ]]; then
    # Only run decode_typed_test on stable because currently the Debug format
    # is different between stable and nightly and stable is the default for decode_typed.
    TC=${TC} ${REPO_DIR}/bin/decode_typed_test;
fi
