#!/bin/bash
# From https://github.com/MetaCubeX/mihomo/blob/82517e6ba8059339287911af899ffdffca6a4044/.github/genReleaseNote.sh

while getopts "v:" opt; do
  case $opt in
    v)
      version_range=$OPTARG
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
  esac
done

if [ -z "$version_range" ]; then
  echo "Please provide the version range using -v option. Example: chmod +x genReleaseNote.sh && ./genReleaseNote.sh -v 1.0.0..HEAD" >&2
  exit 1
fi

repo_url=$(git config --get remote.origin.url)
if [ -z "$repo_url" ]; then
  echo "Could not determine the repository URL. Please ensure you are in a git repository." >&2
  exit 1
fi

repo_url=${repo_url%.git}

new_commits=$(git log --reverse --pretty=format:"* %h %s by @%an" --grep="^feat" -i $version_range)
if [ -n "$new_commits" ]; then
  echo "## New" >> release.md
  echo "$new_commits" >> release.md
  echo "" >> release.md
fi

fix_commits=$(git log --reverse --pretty=format:"* %h %s by @%an" --grep="^fix" -i $version_range)
if [ -n "$fix_commits" ]; then
  echo "## Fix" >> release.md
  echo "$fix_commits" >> release.md
  echo "" >> release.md
fi

maint_commits=$(git log --reverse --pretty=format:"* %h %s by @%an" --grep="^chore\|^docs\|^refactor\|^perf" -i $version_range)
if [ -n "$maint_commits" ]; then
  echo "## Maintenance" >> release.md
  echo "$maint_commits" >> release.md
  echo "" >> release.md
fi

echo "**Full Changelog**: $repo_url/compare/$version_range" >> release.md
