#!/bin/bash
# Automates /docs/RELEASE.md steps until the point the RC is uploaded to svn

set -e

export RELEASE_VERSION=0.8.0
export RELEASE_CANDIDATE=rc2
UPSTREAM_REMOTE=$(git remote -v|grep ' (push)'| grep 'git@github.com:apache/otava.git '|cut -f1)


echo Preparing relese candidate $RELEASE_VERSION-incubating-$RELEASE_CANDIDATE for testing and voting
echo You should be at the top level directory of the master branch checked out

echo First create the git tag marking the source of this release
git tag -a -f $RELEASE_VERSION-incubating-$RELEASE_CANDIDATE -m "$RELEASE_VERSION-incubating-$RELEASE_CANDIDATE"
git push -f $UPSTREAM_REMOTE $RELEASE_VERSION-incubating-$RELEASE_CANDIDATE

echo Download sources for the tagged release from Github

mkdir -p release/pypi/
wget -O release/apache-otava-$RELEASE_VERSION-incubating-$RELEASE_CANDIDATE-src.tar.gz https://github.com/apache/otava/archive/refs/tags/$RELEASE_VERSION-incubating-$RELEASE_CANDIDATE.tar.gz

echo Build PyPi artifacts
uv build --no-create-gitignore
mv dist/* release/pypi/

echo Create and verify signatures and checksums
cd release
gpg --armor --output apache-otava-$RELEASE_VERSION-incubating-$RELEASE_CANDIDATE-src.tar.gz.asc --detach-sig apache-otava-$RELEASE_VERSION-incubating-$RELEASE_CANDIDATE-src.tar.gz
gpg --verify apache-otava-$RELEASE_VERSION-incubating-$RELEASE_CANDIDATE-src.tar.gz.asc apache-otava-$RELEASE_VERSION-incubating-$RELEASE_CANDIDATE-src.tar.gz


sha512sum apache-otava-$RELEASE_VERSION-incubating-$RELEASE_CANDIDATE-src.tar.gz.asc > apache_otava-$RELEASE_VERSION.tar.gz.sha512
sha512sum --check apache_otava-$RELEASE_VERSION.tar.gz.sha512

echo
echo Done with the official source release. Now do sign the pypi versions
echo
cd pypi
gpg --armor --output apache_otava-$RELEASE_VERSION-py3-none-any.whl.asc --detach-sig apache_otava-$RELEASE_VERSION-py3-none-any.whl
gpg --armor --output apache_otava-$RELEASE_VERSION.tar.gz.asc --detach-sig apache_otava-$RELEASE_VERSION.tar.gz

gpg --verify apache_otava-$RELEASE_VERSION-py3-none-any.whl.asc apache_otava-$RELEASE_VERSION-py3-none-any.whl
gpg --verify apache_otava-$RELEASE_VERSION.tar.gz.asc apache_otava-$RELEASE_VERSION.tar.gz


sha512sum apache_otava-$RELEASE_VERSION-py3-none-any.whl > apache_otava-$RELEASE_VERSION-py3-none-any.whl.sha512
sha512sum apache_otava-$RELEASE_VERSION.tar.gz > apache_otava-$RELEASE_VERSION.tar.gz.sha512
sha512sum --check apache_otava-$RELEASE_VERSION.tar.gz.sha512
sha512sum --check apache_otava-$RELEASE_VERSION-py3-none-any.whl.sha512

echo
echo Release artefacts are now ready. The last steps, uploading the the ASF svn repository, for teting, are up to you.
echo Here are the commands
echo
echo cp -r release ../otava-dist-release-repo/$RELEASE_VERSION-incubating-$RELEASE_CANDIDATE
echo cd ../otava-dist-release-repo
echo svn add $RELEASE_VERSION-incubating-$RELEASE_CANDIDATE/
echo svn ci -m "[otava] Add $RELEASE_VERSION-incubating-$RELEASE_CANDIDATE"
echo
echo Now go to https://github.com/apache/otava/blob/master/docs/RELEASE.md#vote-on-the-release-candidate and copy paste
echo the template for an email starting the voting process.
echo
