#!/bin/bash

set -eu -o pipefail

changelog=$(cat ChangeLog.md)

regex='
([0-9]+\.[0-9]+\.[0-9]+) \(([0-9]{4}-[0-9]{2}-[0-9]{2})\)
-*

((.|
)*)
'

if [[ ! $changelog =~ $regex ]]; then
      echo "Could not find date line in change log!"
      exit 1
fi

version="${BASH_REMATCH[1]}"
date="${BASH_REMATCH[2]}"
notes="$(echo "${BASH_REMATCH[3]}" | sed -n -e '/^[0-9]\+\.[0-9]\+\.[0-9]\+/,$!p')"
dist="geoipupdate-$version.tar.gz"

if [[ "$date" !=  $(date +"%Y-%m-%d") ]]; then
    echo "$date is not today!"
    exit 1
fi

tag="v$version"

if [ -n "$(git status --porcelain)" ]; then
    echo ". is not clean." >&2
    exit 1
fi

git pull

perl -pi -e "s/(?<=^AC_INIT\(\[geoipupdate\], \[).+?(?=\])/$version/g" configure.ac

./bootstrap
./configure
make clean
make dist

echo "Diff:"

git diff

echo "Release notes:"
echo "$notes"

read -e -r -p "Push to origin? " SHOULD_PUSH

if [ "$SHOULD_PUSH" != "y" ]; then
    echo "Aborting"
    exit 1
fi

git commit -m "Bump version to $version" -a

git push

message="$version

$notes"

hub release create -a "$dist" -m "$message" "$tag"

echo "Release to PPA and Homebrew"
