#!/bin/bash


set -eu
set -o pipefail

help() {
    >&2 echo "
    Update version number, changelog and trigger new release in GitHub Actions
    "
}

repo="$(git remote get-url public | sed -nre 's/git@github\.com:(.+)\.git/\1/p')"
upstream_branch="main"

if [ "${1:-}" = "-h" -o "${1:-}" = "--help" ]; then
    help
    exit 1
fi

if ! git remote | grep -q "public"; then
    echo
    echo "No public remote found"
    echo
    exit 1
fi

if [ "$(uname)" != "Darwin" ]; then
    echo "Sorry, this script only works on macOS for now."
    exit 1
fi

if [ "$(git rev-parse --abbrev-ref HEAD)" != "$upstream_branch" ]; then
    echo
    echo "Not on the $upstream_branch branch"
    echo
    exit 2
fi

if [ "$(git status . --porcelain)" != "" ]; then
    echo
    echo "Dirty git. Commit changes"
    echo
    exit 1
fi

>&2 echo "Syncing public and origin remotes"
>&2 echo

git fetch public
git push public HEAD:$upstream_branch
git push origin HEAD:$upstream_branch

if [ "$(git rev-parse HEAD)" != "$(git rev-parse origin/$upstream_branch)" ]; then
    echo
    echo "Repository not up to date with origin/$upstream_branch. Do your git push/pull/rebase dance"
    echo
    exit 1
fi

if [ "$(git rev-parse HEAD)" != "$(git rev-parse public/$upstream_branch)" ]; then
    echo
    echo "Repository not up to date with public/$upstream_branch. Do your git push/pull/rebase dance"
    echo
    exit 1
fi

version_file=plugin.php

current_version="$(cat $version_file | sed -En 's/.*Version: ([^ ]*)/\1/p')"

echo
echo "Current version is: $current_version"

read -p "New version> " new_version

if [ "$new_version" = "" ]; then
    echo "Bad version"
    exit 1
fi


prev_tag="$(git describe --abbrev=0 --match "v*" || true)"
next_tag="v$new_version"

tmp_changelog=changelog_entry.md

echo "## v$new_version" > "$tmp_changelog"
echo >> "$tmp_changelog"
date +'%Y-%m-%d' >> "$tmp_changelog"
echo >> "$tmp_changelog"
git log --format="-   %s [%h](https://github.com/${repo}/commit/%h) - %an" HEAD...$prev_tag . >> "$tmp_changelog"
echo >> "$tmp_changelog"
echo "All changes https://github.com/${repo}/compare/${prev_tag}...${next_tag}" >> "$tmp_changelog"
echo >> "$tmp_changelog"

if [ -f CHANGELOG.md ]; then
    cat CHANGELOG.md >> $tmp_changelog
fi

mv $tmp_changelog CHANGELOG.md

while true; do
    echo
    echo
    echo "----------------------------------------------------------------------"
    git diff
    echo "----------------------------------------------------------------------"
    echo
    echo
    echo "👆 The CHANGELOG.md file was updated automatically."
    echo
    echo "Type 'e' to edit in vscode and 'c' to commit and continue"
    read -p "> " changelog_mode

    if [ "$changelog_mode" = "e" ]; then
        code CHANGELOG.md
    fi

    if [ "$changelog_mode" = "c" ]; then
        break
    fi
done

if [ "$(git status . --porcelain)" != "" ]; then
    git add CHANGELOG.md
    git commit -m "Update changelog for v$new_version"
    git push origin HEAD:$upstream_branch
    git push public HEAD:$upstream_branch
    git push public HEAD:release/$new_version

    >&2 echo ""
    >&2 echo "Release triggered in Github Actions."
    >&2 echo "Pull in the changes once it is ready and push to origin too"
    >&2 echo "Or use this script:"
    >&2 echo ""
    >&2 echo "     ./tools/sync-remotes"
fi
