#!/bin/bash

# Usage function to display help
usage() {
    echo "Usage: $0 [OPTIONS]"
    echo ""
    echo "Options:"
    echo "  -f, --final       Use the last 'final' tag as the start point."
    echo "  -n, --newest      Display commits from newest to oldest."
    echo "  -h, --help        Display this help and exit."
    echo ""
    echo "This script generates release notes from git commits based on tags."
    echo "By default, it lists commits from the last tag to HEAD, sorted from oldest to newest."
}

# Check for command line arguments
for arg in "$@"; do
    case "$arg" in
        -f|--final)
            finalFlag=true
            ;;
        -n|--newest)
            newestFirst=true
            ;;
        -h|--help)
            usage
            exit 0
            ;;
        *)
            echo "Unknown option: $arg"
            usage
            exit 1
            ;;
    esac
done

# Adjust sort order based on the flag
if [ "${newestFirst}" = true ]; then
    sortOrder="cat"
else
    sortOrder="tail -r"
fi

# Get tags sorted by creation date
tags=$(git tag --sort=creatordate)

# Convert tags into an array
if [[ $(echo "$BASH_VERSION" | cut -d. -f1) -ge 4 ]]; then
    readarray -t tagArray <<< "$tags"
else
    IFS=$'\n' read -d '' -ra tagArray <<< "$tags"
fi

# Determine the number of tags
len=${#tagArray[@]}

# Find the last tag or the last "final" tag based on the flag
if [ "${finalFlag}" = true ]; then
    for (( i=len-1; i>=0; i-- )); do
        if [[ "${tagArray[i]}" == *"final"* ]]; then
            latestTag="${tagArray[i]}"
            break
        fi
    done
    if [ -z "$latestTag" ]; then
        echo "No 'final' tag found."
        exit 1
    fi
else
    if [ "$len" -gt 0 ]; then
        latestTag="${tagArray[$len-1]}"
    else
        echo "No tags found."
        exit 1
    fi
fi

# Print release notes
echo "## Release Notes from $latestTag to this release"
echo ""

# Fetch commit logs from the latest tag to HEAD and categorize them
newfeatures=$(git log "$latestTag"..HEAD --pretty=format:"%s" | grep 'new:' | sed 's/new:/- /g' | sort | uniq | $sortOrder)
updatedfeatures=$(git log "$latestTag"..HEAD --pretty=format:"%s" | grep 'update:' | sed 's/update:/- /g' | sort | uniq | $sortOrder)
fixedfeatures=$(git log "$latestTag"..HEAD --pretty=format:"%s" | grep 'fix:' | sed 's/fix:/- /g' | sort | uniq | $sortOrder)
deletedfeatures=$(git log "$latestTag"..HEAD --pretty=format:"%s" | grep 'delete:' | sed 's/delete:/- /g' | sort | uniq | $sortOrder)

# Output formatted commit lists
echo "New Features:"
echo ""
if [ -z "$newfeatures" ]; then
    echo "- No new features."
else
    echo "$newfeatures"
fi
echo ""

echo "Updated Features:"
echo ""
if [ -z "$updatedfeatures" ]; then
    echo "- No updated features."
else
    echo "$updatedfeatures"
fi
echo ""

echo "Fixed Features:"
echo ""
if [ -z "$fixedfeatures" ]; then
    echo "- No fixed features."
else
    echo "$fixedfeatures"
fi
echo ""

echo "Deleted Features:"
echo ""
if [ -z "$deletedfeatures" ]; then
    echo "- No deleted features."
else
    echo "$deletedfeatures"
fi
echo ""
