#!/usr/bin/env bash

set -u
VERSION="$1"
: "$VERSION"

# Version number should be changed in root `CMakeLists.txt`.
sed -i -r "s/(  VERSION )[0-9\.]+/\1$VERSION/g" CMakeLists.txt

# Set `INSTALL_VERSION` to "${PROJECT_VERSION}".
sed -i '/set(INSTALL_VERSION "${PROJECT_VERSION}")/s/^#//g' CMakeLists.txt
sed -i '/set(INSTALL_VERSION "unreleased")/s/^/#/g' CMakeLists.txt

# Update files (and cmake references) suffixed `--unreleased` should be renamed.
rename unreleased "$VERSION" h3/sql/updates/*--unreleased.sql
sed -i "s/unreleased/$VERSION/g" h3/sql/updates/*--$VERSION.sql
sed -i "s/unreleased/$VERSION/g" h3/CMakeLists.txt

rename unreleased "$VERSION" h3_postgis/sql/updates/*--unreleased.sql
sed -i "s/unreleased/$VERSION/g" h3_postgis/sql/updates/*--$VERSION.sql
sed -i "s/unreleased/$VERSION/g" h3_postgis/CMakeLists.txt

git checkout -b "release-$VERSION"

echo "Remember to":
echo "   - Installer \`.sql\` files should have \`@ availability\` comments updated."
echo "   - Update changelog by moving from \`Unreleased\` to a new section"