#!/bin/sh -ex

FEATURES="serde"

# Pin dependencies as required if we are using MSRV toolchain.
if cargo --version | grep "1\.56"; then
    cargo update -p tempfile --precise 3.6.0
    cargo update -p once_cell --precise 1.13.1
    cargo update -p which --precise 4.4.0
    cargo update -p byteorder --precise 1.4.3
    cargo update -p cc --precise 1.0.94
    cargo update -p libc --precise 0.2.163
    cargo update -p serde_json --precise 1.0.98
    cargo update -p serde --precise 1.0.156
    cargo update -p ppv-lite86 --precise 0.2.8
    cargo update -p bitcoin --precise 0.32.2
    cargo update -p bitcoin-units --precise 0.1.2
    cargo update -p bitcoin-io --precise 0.1.2
    cargo update -p bitcoin_hashes --precise 0.14.2
    cargo update -p base58ck --precise 0.1.0
    cargo update -p itoa --precise 1.0.6
    cargo update -p ryu --precise 1.0.9
    cargo update -p getrandom --precise 0.2.3
    cargo update -p quote --precise 1.0.35
    cargo update -p proc-macro2 --precise 1.0.80
    cargo update -p unicode-ident --precise 1.0.6
fi

if [ "$DO_FEATURE_MATRIX" = true ]
then
    # Test without any features first
    cargo test --verbose --no-default-features
    # Then test with the default features
    cargo test --verbose
    # Then test with the default features
    cargo test --all-features --verbose

    # Also build and run each example to catch regressions
    cargo build --examples
    # run all examples
    run-parts ./target/debug/examples

    # Test each feature
    for feature in ${FEATURES}
    do
        cargo test --verbose --features="$feature"
    done
fi

if [ "$DO_LINT" = true ]
then
    cargo clippy --all-features --all-targets
fi

# Build the docs if told to (this only works with the nightly toolchain)
if [ "$DO_DOCSRS" = true ]; then
    RUSTDOCFLAGS="--cfg docsrs -D rustdoc::broken-intra-doc-links" cargo +nightly doc --no-deps --all-features
fi

# Build the docs with a stable toolchain, in unison with the DO_DOCSRS command
# above this checks that we feature guarded docs imports correctly.
if [ "$DO_DOCS" = true ]; then
    cargo +stable doc --all-features
fi


# Fuzz if told to
if [ "$DO_FUZZ" = true ]
then
    (
        cd fuzz
        if cargo --version | grep "1\.63"; then
            cargo update -p cc --precise 1.0.94
            cargo update -p bitcoin --precise 0.32.2
            cargo update -p bitcoin-units --precise 0.1.2
            cargo update -p bitcoin-io --precise 0.1.2
            cargo update -p bitcoin_hashes --precise 0.14.2
            cargo update -p base58ck --precise 0.1.0
            cargo update -p itoa --precise 1.0.6
            cargo update -p unicode-ident --precise 1.0.6
            cargo update -p serde_json --precise 1.0.98
            cargo update -p serde --precise 1.0.156
            cargo update -p ppv-lite86 --precise 0.2.8
            cargo update -p quote --precise 1.0.28
            cargo update -p proc-macro2 --precise 1.0.66
            cargo update -p libc --precise 0.2.163
            cargo update -p ryu --precise 1.0.9
            cargo update -p honggfuzz --precise 0.5.54
        fi
        cargo test --verbose
        ./travis-fuzz.sh
    )
fi

# Do integration test if told to
if [ "$DO_INTEGRATION" = true ]
then
    (
        BITCOIND_EXE_DEFAULT="$(git rev-parse --show-toplevel)/elementsd-tests/bin/bitcoind"
        ELEMENTSD_EXE_DEFAULT="$(git rev-parse --show-toplevel)/elementsd-tests/bin/elementsd"

        cd elementsd-tests
        BITCOIND_EXE=${BITCOIND_EXE:=${BITCOIND_EXE_DEFAULT}} \
        ELEMENTSD_EXE=${ELEMENTSD_EXE:=${ELEMENTSD_EXE_DEFAULT}} \
        cargo test
        cd ..
    )
fi
