#!/bin/bash -eu

set -o pipefail

readonly VERSION=${1:-$(date +%Y.%m.%-d.0)}


cd "$(git rev-parse --show-toplevel)"

# update requirements
ydl_version=$(LANG=C pacman -Qi yt-dlp | grep ^Version | cut -d ':' -f 2 | cut -d '-' -f 1)
grep -v yt-dlp requirements.txt > requirements.txt.tmp
echo "yt-dlp>=$ydl_version" >> requirements.txt.tmp
sort -u -o requirements.txt.tmp requirements.txt.tmp
mv requirements.txt{.tmp,}
git add requirements.txt

# update version
sed -i "s/^\(__version__ = \"\).*\(\"\)/\1$VERSION\2/w /dev/stdout" amg/__init__.py
git add amg/__init__.py

# commit
git commit -m "Version ${VERSION}"

# tag
git tag -m "Version ${VERSION}" "${VERSION}"

# release
rm -Rf dist
python setup.py sdist
twine upload dist/*.tar.gz
