#!/bin/sh

set -e

fail() {
    printf '%s\n' "$1" >&2  ## Send message to stderr. Exclude >&2 if you don't want it that way.
    exit "${2-1}"  ## Return a code specified by $2 or 1 by default.
}

diff() {
    git diff --no-ext-diff --quiet
}

# Uncommitted changes
diff || fail "Uncommitted changes"

# Unformatted rust code
cargo fmt && diff || fail "Unformatted rust code"
cargo fmt --manifest-path contrib/screen-13-egui/Cargo.toml && diff || fail "Unformatted rust code (screen-13-egui)"
cargo fmt --manifest-path contrib/screen-13-fx/Cargo.toml && diff || fail "Unformatted rust code (screen-13-fx)"
cargo fmt --manifest-path contrib/screen-13-hot/Cargo.toml && diff || fail "Unformatted rust code (screen-13-hot)"
cargo fmt --manifest-path contrib/screen-13-imgui/Cargo.toml && diff || fail "Unformatted rust code (screen-13-imgui)"
cargo fmt --manifest-path examples/shader-toy/Cargo.toml && diff || fail "Unformatted rust code (shader-toy)"
cargo fmt --manifest-path examples/skeletal-anim/Cargo.toml && diff || fail "Unformatted rust code (skeletal-anim)"
cargo fmt --manifest-path examples/vr/Cargo.toml && diff || fail "Unformatted rust code (vr)"

# Rust code errors
echo "Checking screen-13"
cargo check --all-targets
echo "Checking screen-13 (w/ parking_lot)"
cargo check --all-targets --features parking_lot
echo "Checking contrib/screen-13-egui"
cargo check --manifest-path contrib/screen-13-egui/Cargo.toml --all-targets --all-features
echo "Checking contrib/screen-13-fx"
cargo check --manifest-path contrib/screen-13-fx/Cargo.toml --all-targets --all-features
echo "Checking contrib/screen-13-hot"
cargo check --manifest-path contrib/screen-13-hot/Cargo.toml --all-targets --all-features
#echo "Checking contrib/screen-13-imgui"
#cargo check --manifest-path contrib/screen-13-imgui/Cargo.toml --all-targets --all-features
echo "Checking contrib/screen-13-window"
cargo check --manifest-path contrib/screen-13-window/Cargo.toml --all-targets --all-features
echo "Checking examples/shader-toy"
cargo check --manifest-path examples/shader-toy/Cargo.toml --all-targets --all-features
echo "Checking examples/skeletal-anim"
cargo check --manifest-path examples/skeletal-anim/Cargo.toml --all-targets --all-features
echo "Checking examples/vr"
cargo check --manifest-path examples/vr/Cargo.toml --all-targets --all-features

# Rust code lints
cargo clippy --all-targets
cargo clippy --all-targets --features parking_lot
cargo clippy --manifest-path contrib/screen-13-egui/Cargo.toml --all-targets --all-features
cargo clippy --manifest-path contrib/screen-13-fx/Cargo.toml --all-targets --all-features
cargo clippy --manifest-path contrib/screen-13-hot/Cargo.toml --all-targets --all-features
#cargo clippy --manifest-path contrib/screen-13-imgui/Cargo.toml --all-targets --all-features
cargo clippy --manifest-path examples/shader-toy/Cargo.toml --all-targets --all-features
cargo clippy --manifest-path examples/skeletal-anim/Cargo.toml --all-targets --all-features
cargo clippy --manifest-path examples/vr/Cargo.toml --all-targets --all-features

# Rust code tests
cargo test

# Check for semver breaking changes: if this fails you must update the crate version or fix the code
cargo semver-checks check-release --default-features

echo "OK"
