#!/bin/sh -eu

set -o pipefail

had_errors=

export RUSTFLAGS="-C target-feature=-crt-static"

echo "== Copying sources" >&2
mkdir -p /build
cp -r /src/Cargo.* /src/src /build
cd /build

echo "== Install dependencies" >&2
rustup component add clippy rustfmt
apk add musl-dev openssl-dev

echo "== Running cargo test" >&2
if ! cargo test; then
  had_errors=1
fi

echo "== Running clippy" >&2
if ! cargo clippy; then
  had_errors=1
fi

echo "== Running rustfmt" >&2
if ! find src -type f -name '*.rs' | xargs rustfmt --edition 2021 --check; then
  had_errors=1
fi

if [ "$had_errors" ]; then
  echo "Failing checks" >&2
  exit 1
fi
