#!/usr/bin/env bash
set -euo pipefail

# Read all tags, separate them into an array
all_tags=$(git tag -l | wc -l)

if [ "$all_tags" = 0 ]; then
    echo "Repository contains no tags. Please make a tag first. Fetching last 5 commits"

    changelog="$(git log -n 5 --pretty=format:" - %s [%an]")"
elif [ "$all_tags" = 1 ]; then
    echo "Fetching last 5 commits."

    changelog="$(git log -n 5 --pretty=format:" - %s [%an]")"
else 
    echo "Fetching commits since last tag."

    latest_tag=$(git describe --tags --abbrev=0)

    changelog="$(git log --pretty=format:" - %s [%an]" "$latest_tag"..HEAD)"
fi

# Add branch info
branch="$(git branch --contains "${GIT_CLONE_COMMIT_HASH}")"
branch=${branch:2}
NEWLINE=$'\n'
if [ -n "$branch" ]; then
    if [[ "$branch" == *"feature"* ]]; then
        branchinfo="*_WARNING_*: This is a _FEATURE_ build on *${branch}*${NEWLINE}${NEWLINE}" 
        changelog=$branchinfo$changelog
    elif [[ "$branch" == *"hotfix"* ]]; then
        branchinfo="*_WARNING_*: This is a _HOTFIX_ build on *${branch}*${NEWLINE}${NEWLINE}"
        changelog=$branchinfo$changelog
    else
        branchinfo="Built on *${branch}*${NEWLINE}${NEWLINE}"
        changelog=$branchinfo$changelog
    fi
fi

# Output collected information
echo "Committer: $(git log --pretty=format:"%ce" HEAD^..HEAD)"
echo "Latest tag: $latest_tag"
echo "Changelog:"
echo "$changelog"

export CHANGELOG="$changelog"
