#!/bin/bash

## Script to generate the docstring coverage
NAME_CURRENT_FILE="`realpath \"$0\"`"
DIRNAME_CURRENT_FILE=$(dirname $NAME_CURRENT_FILE)
TESTS_DIR="$DIRNAME_CURRENT_FILE/binarycpython/tests"
REPORTS_DIR="$DIRNAME_CURRENT_FILE/reports"
BADGE_DIR="$DIRNAME_CURRENT_FILE/badges"

#
# echo "$NAME_CURRENT_FILE"
# echo "$DIRNAME_CURRENT_FILE"
# echo "$TESTS_DIR"
# echo "$REPORTS_DIR"

# Create main reports directory
mkdir -p "$REPORTS_DIR"

## Docstring coverage:
command -v docstr-coverage >/dev/null 2>&1 || { echo >&2 "docstr-coverage is not installed.  Aborting."; exit 1; }

#
echo "Generating docstring report"
DOCSTRING_COV_DIR="$REPORTS_DIR/docstring_coverage"
mkdir -p "$DOCSTRING_COV_DIR/"
docstr-coverage binarycpython --exclude="$TESTS_DIR/*" -v 3 --badge "$DOCSTRING_COV_DIR/docstring_coverage.svg" > "$DOCSTRING_COV_DIR/docstring_coverage.txt" 2>&1
cp "$DOCSTRING_COV_DIR/docstring_coverage.svg" "$BADGE_DIR/docstring_coverage.svg"
echo "Done"

## test coverage
command -v coverage >/dev/null 2>&1 || { echo >&2 "coverage is not installed. Aborting."; exit 1; }
command -v coverage-badge >/dev/null 2>&1 || { echo >&2 "coverage-badge is not installed. Aborting."; exit 1; }

echo "Generating test coverage html report"
TEST_COV_DIR="$REPORTS_DIR/test_coverage"
mkdir -p "$TEST_COV_DIR/"
cd $TEST_COV_DIR
coverage run --source=binarycpython "$TESTS_DIR/main.py"
coverage html
coverage-badge > "$TEST_COV_DIR/test_coverage.svg"
cd $DIRNAME_CURRENT_FILE
cp "$TEST_COV_DIR/test_coverage.svg" "$BADGE_DIR/test_coverage.svg"
echo "Done"

# # Go down to the dir
# cd reports/test_coverage && coverage run --source=binarycpython ../../binary_c-python/binarycpython/tests/main.py && coverage-badge > test_coverage.svg
# cd ../../

# # Copy docstring coverage
# cp reports/docstring_coverage/docstring_coverage.svg binary_c-python/docs/source/badges/docstring_coverage.svg

# # Copy test coverage
# cp reports/test_coverage/coverage.svg binary_c-python/badges/test_coverage.svg
# cp reports/test_coverage/coverage.svg binary_c-python/docs/source/badges/test_coverage.svg
