#!/usr/bin/env bash

# This file is part of eRCaGuy_dotfiles: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles

# GS
# Feb. 2024

# Description:
#
# Generate Doxygen documentation for a C or C++ project, store repo info. into a special readme next
# to the doxygen output, and open the index.html Doxygen output webpage in the Google Chrome web
# browser.

# See: https://stackoverflow.com/a/60157372/4561887
FULL_PATH_TO_SCRIPT="$(realpath "${BASH_SOURCE[-1]}")"
SCRIPT_DIRECTORY="$(dirname "$FULL_PATH_TO_SCRIPT")"

cd "$SCRIPT_DIRECTORY"

DOXYFILE="Doxyfile"  # name/path of the doxygen configuration file
OUTPUT_DIRECTORY="$(grep "^OUTPUT_DIRECTORY" "$DOXYFILE" | cut -d "=" -f 2 | tr -d " ")"

# From my answer: https://stackoverflow.com/a/76856090/4561887
# Get a short commit hash, and see whether `git status` is clean or dirty.
# Example outputs:
# 1. Not in a git repo: `(not a git repo)`
# 2. In a repo which has a "dirty" `git status`: `72361c8-dirty`
#   - Note that "dirty" means there are pending uncommitted changes.
# 3. In a repo which has a "clean" `git status`: `72361c8`
git_get_short_hash() {
    # See: https://stackoverflow.com/a/16925062/4561887
    is_git_repo="$(git rev-parse --is-inside-work-tree 2>/dev/null)"

    if [ "$is_git_repo" != "true" ]; then
        echo "(not a git repo)"
        return $RETURN_CODE_SUCCESS
    fi

    # See my answer here: https://stackoverflow.com/a/76856090/4561887
    test -z "$(git status --porcelain)" \
        && echo "$(git rev-parse --short HEAD)" \
        || echo "$(git rev-parse --short HEAD)-dirty"
}

echo "Generating Doxygen documentation..."
time doxygen "$DOXYFILE"
if [ "$?" -ne 0 ]; then
    echo "Error: doxygen failed!"
    exit 1
fi

# Create a readme in the `OUTPUT_DIRECTORY` with info about the doxygen run, to help orient the 
# user about the repo (on which branch, commit, OS, etc. it ran)

REPO_NAME="$(basename "$(git rev-parse --show-toplevel)")"
GIT_COMMIT="$(git_get_short_hash)"
GIT_BRANCH="$(git branch --show-current)"
DOXYGEN_VERSION="$(doxygen --version)"
# Note: `2>&1` redirects stderr (file descriptor 2) to stdout (file descriptor 1) because the
# version info is otherwise printed to stderr
DOT_VERSION="$(dot -V 2>&1)"  
OPERATING_SYSTEM=""

# See: 
# 1. My answer with this code: https://stackoverflow.com/a/78480875/4561887
# 2. Main answer: https://stackoverflow.com/a/8597411/4561887
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
    OPERATING_SYSTEM="Linux: $(lsb_release -d | cut -f2-)"
elif [[ "$OSTYPE" == "msys" ]]; then 
    OPERATING_SYSTEM="Windows: MSYS2: $(uname -a)"
fi

INFO_STR=$(cat << EOF
_This is an automatically-generated README. Do not edit this file manually._


# Last doxygen run

- **Repository:** \`$REPO_NAME\`
- **Git commit:** \`$GIT_COMMIT\`
- **Git branch:** \`$GIT_BRANCH\`
- **Doxygen (\`doxygen\`) version:** \`$DOXYGEN_VERSION\`
- **Graphviz (\`dot\`) version:** \`$DOT_VERSION\`
- **Operating system:** \`$OPERATING_SYSTEM\`
EOF
)

echo "$INFO_STR" > "$OUTPUT_DIRECTORY/doxygen_info_README.md"


echo ""
echo "Doxygen documentation generated in \"$OUTPUT_DIRECTORY\"." 
echo "Opening the Doxygen documentation in the Google Chrome web browser..."
echo "Running cmd:  google-chrome \"$OUTPUT_DIRECTORY/html/index.html\""
echo ""
google-chrome "$OUTPUT_DIRECTORY/html/index.html"
