#!/usr/bin/env bash
set -euo pipefail

cargo fmt --check
cargo test
cargo test --features alloc
cargo test --features std
cargo test --features memory-lock
cargo test --features asm-compare
cargo test --features cache-flush
cargo test --features guard-pages
cargo test --features multi-pass-clear
cargo test --features unsafe-wipe
cargo test --features unsafe-wipe,alloc
cargo test --all-features
cargo check --examples
cargo check --examples --features alloc
cargo check --examples --features std
cargo check --examples --features memory-lock
cargo check --examples --features asm-compare
cargo check --examples --features cache-flush
cargo check --examples --features guard-pages
cargo check --examples --features multi-pass-clear
cargo check --examples --features unsafe-wipe
cargo check --examples --all-features
cargo clippy --all-targets --no-default-features -- -D warnings
cargo clippy --all-targets --all-features -- -D warnings

target_installed() {
    rustup target list --installed | grep -Fxq "$1"
}

check_installed_target() {
    target="$1"
    shift

    if target_installed "$target"; then
        cargo check --target "$target" "$@"
    else
        printf 'skipping target check for %s; target is not installed\n' "$target"
    fi
}

check_installed_target x86_64-unknown-linux-gnu --all-features --lib
check_installed_target aarch64-unknown-linux-gnu --features memory-lock,guard-pages,multi-pass-clear --lib
check_installed_target x86_64-apple-darwin --all-features --lib
check_installed_target aarch64-apple-darwin --all-features --lib
check_installed_target aarch64-apple-ios --all-features --lib
check_installed_target x86_64-apple-ios --all-features --lib
check_installed_target x86_64-pc-windows-gnu --all-features --lib
check_installed_target aarch64-linux-android --all-features --lib
check_installed_target x86_64-linux-android --all-features --lib
check_installed_target x86_64-unknown-freebsd --features memory-lock,guard-pages,multi-pass-clear --lib
check_installed_target wasm32-unknown-unknown --no-default-features --lib
check_installed_target wasm32-unknown-unknown --features alloc,memory-lock,random-canary,multi-pass-clear --lib
check_installed_target wasm32-unknown-unknown --features random-canary --lib
check_installed_target wasm32-wasip1 --features alloc,random-canary,multi-pass-clear --lib
check_installed_target wasm32-wasip2 --features random-canary --lib
check_installed_target thumbv7em-none-eabihf --no-default-features --lib

if target_installed wasm32-unknown-unknown; then
    if cargo check --target wasm32-unknown-unknown --features canary-check --lib >/tmp/sanitization-wasm-canary-check.log 2>&1; then
        printf 'wasm32 canary-check without random-canary unexpectedly compiled\n'
        exit 1
    fi

    if cargo check --target wasm32-unknown-unknown --features guard-pages --lib >/tmp/sanitization-wasm-guard-pages.log 2>&1; then
        printf 'wasm32 guard-pages unexpectedly compiled\n'
        exit 1
    fi
fi

scripts/verify-codegen.sh
scripts/verify-kani.sh
RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features
cargo package --allow-dirty --list >/dev/null
