#!/bin/sh

# Скрипт генерирует changelog от последнего релиза.

CHANGELOG=CHANGELOG.md

printf "\033[0;32m~~~ %s generation ~~~\033[0m\n" "$CHANGELOG";

if ! git --version > /dev/null 2>&1; then
	printf "\n\033[0;31mGIT is not found!\033[0m\n"; exit 1;
fi

if ! git status > /dev/null 2>&1; then
	printf "\n\033[0;31mThis is not a git repo!\033[0m\n"; exit 1;
fi

# Get all tags that points to the HEAD
CURRENT_TAG=$(git tag --points-at HEAD)
echo "Current tag: $CURRENT_TAG"

LAST_TAG=$(git describe --tags --abbrev=0 HEAD^ 2> /dev/null)
echo "Last tag: $LAST_TAG"

if [ -n "$CURRENT_TAG" ]; then
	printf "<details><summary>changelog:</summary>\n\n" > $CHANGELOG
else
	printf "Current commit does not have a release tag.\n\n" > $CHANGELOG
fi

if [ -n "$LAST_TAG" ]; then
	git log --pretty="- %h %s" "$LAST_TAG..HEAD" >> $CHANGELOG
else
	git log --pretty="- %h %s" >> $CHANGELOG
fi

if [ -n "$CURRENT_TAG" ]; then
	printf "</details>" >> $CHANGELOG
fi