#!/usr/bin/env bash
#  vim:ts=4:sts=4:sw=4:et
#
#  Author: Hari Sekhon
#  Date: 2024-10-03 04:55:02 +0300 (Thu, 03 Oct 2024)
#
#  https///github.com/HariSekhon/DevOps-Bash-tools
#
#  License: see accompanying Hari Sekhon LICENSE file
#
#  If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish
#
#  https://www.linkedin.com/in/HariSekhon
#

set -euo pipefail
[ -n "${DEBUG:-}" ] && set -x
srcdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# shellcheck disable=SC1090,SC1091
. "$srcdir/lib/git.sh"

code_month="git_commits_per_month.gnuplot"
code_year="git_commits_per_year.gnuplot"
data_month="data/git_commits_per_month.dat"
data_year="data/git_commits_per_year.dat"
image_year="images/git_commits_per_year.png"
image_month="images/git_commits_per_month.png"

# shellcheck disable=SC2034,SC2154
usage_description="
Generates GNUplot graphs of Git commits per year and per month for the entire history of the local Git repo checkout

Generates the following files:

    $code_month - Code
    $code_year  - Code

    $data_month - Data
    $data_year  - Data

    $image_month - Image
    $image_year  - Image

A MermaidJS version of this script is adjacent at:

    git_graph_commit_history_mermaidjs.sh

Requires Git and GNUplot to be installed to generate the graphs
"

# used by usage() in lib/utils.sh
# shellcheck disable=SC2034
usage_args="[<git_log_paths_to_check_only>]"

help_usage "$@"

if ! is_in_git_repo; then
    die "Error: Not inside a git repository!"
fi

for x in $code_month  \
         $code_year   \
         $data_month  \
         $data_year   \
         $image_month \
         $image_year; do
    mkdir -p -v "$(dirname "$x")"
done

# output git commits as simple YYYY-MM and then just sort and count them by month
timestamp "Calculating commit counts per month from the Git log"
git log --date=format:'%Y-%m' --pretty=format:'%ad' "$@" |
sort |
uniq -c |
awk '{print $2, $1}' > "$data_month"
timestamp "Wrote data: $data_month"
echo

timestamp "Calculating commit counts per year from the Git log"
git log --date=format:'%Y' --pretty=format:'%ad' "$@" |
sort |
uniq -c |
awk '{print $2, $1}' > "$data_year"
timestamp "Wrote data: $data_year"
echo

gnuplot_common_settings="
#
# Generated by ${0##*/}
#
# from https://github.com/HariSekhon/DevOps-Bash-tools
#
set terminal pngcairo size 1280,720 enhanced font 'Arial,14'
set ylabel 'Number of Commits'
set grid
set xtics rotate by -45
set boxwidth 0.5 relative
set style fill solid
set datafile separator ' '
"
#set xtics auto  # cannot find a way to make this show every year

timestamp "Generating GNUplot code for Commits per Month"
sed '/^[[:space:]]*$/d' > "$code_month" <<EOF
$gnuplot_common_settings
set title "Git Commits per Month"
set xlabel "Month-Year"
set format x "%b %Y"
set xdata time
set timefmt "%Y-%m"
set output "$image_month"
plot "$data_month" using 1:2 with boxes title 'Commits'
EOF
timestamp "Generated GNUplot code: $code_month"
echo

timestamp "Generating GNUplot code for Commits per Year"
sed '/^[[:space:]]*$/d' > "$code_year" <<EOF
$gnuplot_common_settings
set title "Git Commits per Year"
set xlabel "Year"
# results in X axis labels every 2 years
#set xdata time
#set timefmt "%Y"
#set format x "%Y"
# trick to get X axis labels for every year
stats "$data_year" using 1 nooutput
set xrange [STATS_min:STATS_max]
set xtics 1
set output "$image_year"
plot "$data_year" using 1:2 with boxes title 'Commits'
EOF
timestamp "Generated GNUplot code: $code_year"

echo

timestamp "Generating bar chart for Commits per Month"
gnuplot "$code_month"
timestamp "Generated bar chart image: $image_month"
echo

timestamp "Generating bar chart for Commits per Year"
gnuplot "$code_year"
timestamp "Generated bar chart image: $image_year"
echo

if is_CI; then
    exit 0
fi

timestamp "Opening: $image_month"
"$srcdir/../media/imageopen.sh" "$image_month"

timestamp "Opening: $image_year"
"$srcdir/../media/imageopen.sh" "$image_year"
