#!/bin/sh

set -eu

python_lines=$(find -name '*.py' | xargs grep '\S' | wc -l)
python_comments=$(find -name '*.py' | xargs grep '^\s*#' | wc -l)
python_tests=$(grep --binary-files=without-match -r 'def\s*test' test/ | wc -l)
python_runtime_dependencies=$(grep ^[^-#] requirements.txt | wc -l)
python_development_dependencies=$(grep ^[^-#] requirements-dev.txt | wc -l)
python_files=$(find -name '*.py' | wc -l)

git_commits=$(git log --oneline | wc -l)
git_authors=$(git log --format=%aN | sort -u | wc -l)

doc_files=$(find -name '*.md' -or -name '*.rst' | wc -l)
doc_lines=$(find -name '*.md' -or -name '*.rst' | xargs grep '\S'  | wc -l)

if [ -f .tests ]; then
  tests_per_sec=$(sed -e '/^Ran [0-9]\+ tests/ !d; s/Ran \([0-9]\+\) tests in \([0-9.]\+\)s/scale=2\n\1.0 \/ \2/' .tests  | bc)
else
  tests_per_sec=0
fi

echo "
{
  \"python/lines\": $python_lines,
  \"python/comments\": $python_comments,
  \"python/tests\": $python_tests,
  \"python/runtime_dependencies\": $python_runtime_dependencies,
  \"python/development_dependencies\": $python_development_dependencies,
  \"python/files\": $python_files,

  \"tests_per_sec\": $tests_per_sec,

  \"doc/files\": $doc_files,
  \"doc/lines\": $doc_lines,

  \"git/commits\": $git_commits,
  \"git/authors\": $git_authors
}" | jq ''
