#!/bin/bash
set -e

SOLUTION="$(dirname "$0")/../Libplanet.sln"

if ! [[ -f "$SOLUTION" ]]; then
  echo "No such solution file: $SOLUTION" > /dev/stderr
  exit 1
fi

list-projects() {
  dotnet sln "$SOLUTION" list | grep -E $'\\...proj\r?$' | sed 's/[/][^/]*$//'
}

# shellcheck source=.github/bin/constants.sh
. "$(dirname "$0")/../.github/bin/constants.sh"
projects_index=" ${projects[*]} "

check-github-actions() {
  # shellcheck disable=SC2076
  if [[ ! "$projects_index" =~ " $1 " ]]; then
    echo "The project $1 is missing from the projects constant in the " \
      ".github/bin/constants.sh file." > /dev/stderr
    exit 1
  fi
}

check-contributing() {
  name=$(echo "$1" | sed -re 's/(src|tools|test)\///')
  if ! grep -E "^ -  \*$name\*: " CONTRIBUTING.md > /dev/null; then
    echo "The project $1 is not documented in the CONTRIBUTING.md file." \
      > /dev/stderr
    exit 1
  fi
}

check-docfx() {
  if ! grep "$1" Docs/docfx.json > /dev/null; then
    echo "The project $1 is not listed in the Docs/docfx.json file." \
      > /dev/stderr
    exit 1
  fi
}

for project in $(list-projects); do
  if ! [[ "$project" = *.Tests || "$project" = *.UnitTests \
       || "$project" = *.Benchmarks ]]; then
    check-github-actions "$project" || exit 1
    if [[ "$project" != *.Executable ]]; then
      check-docfx "$project" || exit 1
    fi
  fi
  check-contributing "$project" || exit 1
done

# vim: set filetype=sh ts=2 sw=2 et:
