#!/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 tmp/queries.json ]; then
  cat tmp/queries.json | jq '' | sed -e 's/}/,/; s#/#_#g; s#:_#/_#; s#^\(\s\+"\)#\1queries/#;'
else
  echo "{"
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,

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

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