#!/bin/bash

## Runs code statistics, checks for outdated dependencies, then runs cargo-audit and clippy.
## Requires cargo components, see `bin/bootstrap-rust.txt` for a list

set -e
dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
project_dir=${dir}/..
cd $project_dir

f () {
  echo "=== $1 counts ==="
  cargo clean -p "dbui-$1"
  cd "crates/$1"
  cargo count --separator , --unsafe-statistics src
  echo "=== $1 dependencies ==="
  cargo outdated --depth=1
  cd ../..
}

bin/format

f "core"
f "client"
f "assets"
f "database"
f "controllers"
f "service"
f "templates"

echo "=== dbui counts ==="
cargo clean -p dbui
cd "src"
cargo count --separator , --unsafe-statistics
echo "=== dbui dependencies ==="
cargo outdated --depth=1
cd ..

echo "=== (total) ==="
cargo count --separator , --unsafe-statistics -a --exclude=$(cat .gitignore) --exclude=target --exclude=project --exclude=crates/assets/embed --exclude=crates/assets/stylesheets/uikit --exclude=crates/client/pkg --exclude=crates/client/www
cargo deps --depth 1 --include-versions --dev-deps | dot -Tpng > "target/dependencies.png"

echo "=== Advisories ==="
cargo audit

echo "=== Linting ==="
cargo clippy
echo "=== Linting Client ==="
cd crates/client
cargo clippy
