#!/bin/bash

[ -n "$GIT_DIR" ] || export GIT_DIR="$(git rev-parse --show-toplevel 2>/dev/null)/.git"
. "$GIT_DIR"/hooks/hook_lib

VERSION=$1
debug "Detected VERSION as '$VERSION'"
STRIP_VERSION=$(echo "$VERSION" | tr -c -d '0-9.')
debug "Detected STRIP_VERSION as '$STRIP_VERSION'"

LAST_VERSION=get_last_version

step_name "Adding new release subheading in CHANGELOG.md"

# Automatically update the CHANGELOG to mark the new release.
# Unlike our post-flow-release-start script, there is no'Unreleased' tag
# so we just insert the new version number immediately after the header
sed -i "/^=========$/ a\
\\\n## [$VERSION] - $(date +%Y-%m-%d)\nTODO
" "$REPO_DIR/CHANGELOG.md"

add_changelog_diff_link "$LAST_VERSION" "$STRIP_VERSION"

set_gem_version "$STRIP_VERSION"
