#!/bin/bash

# python setup.py --command-packages=stdeb.command sdist_dsc --with-python2=True --with-python3=True -x .stdeb.cfg bdist_deb
# replaced with debianize:
# python setup.py --command-packages=stdeb.command debianize --with-python2=True --with-python3=False

sudo apt install -yq python-stdeb git-buildpackage

set -e
set -x

# sanity checks
test -e ~/.pypirc

# can I upload a deb package?
#ssh debian1.c1 true

# Can I sign a package?
# Requires importing GPG keys to local keyring. For keybase:
# gpg --allow-secret-key-import --import privkey-gwillem
echo "Checking for $EMAIL in gpg keys .."
gpg --list-secret-keys | grep -F "<$EMAIL>"

export VERSION=$(date "+%Y%m%d.%H%M%S")

echo "Updating setup.py with version $VERSION"
perl -pi -e 's/^VERSION="[^"]*"/VERSION=\"$ENV{"VERSION"}\"/g;' setup.py

echo "Adding setup.py to git"
git add setup.py
git commit setup.py -m "Update version in setup.py to $VERSION"

echo "Generating changelog changelog"
gbp dch --debian-tag="%(version)s" --new-version=$VERSION --ignore-branch  --release --commit --git-author

echo "Building package"
gbp buildpackage --git-ignore-branch --git-no-sign-tags

echo "Creating tag $VERSION"
git tag $VERSION

echo "Pushing tags"
git po
git push --tags

# scp ../python-mwscan_${VERSION}_all.deb debian1.c1:/srv/ubuntu/tmp

# ssh debian1.c1 "cd /srv/ubuntu; 
# reprepro -C hypernode includedeb precise tmp/python-mwscan_${VERSION}_all.deb; 
# reprepro -C hypernode list precise python-mwscan;
# "

# rm -f ../{python-mwscan,mwscan}_*.{build,changes,dsc,gz,deb}

# http://peterdowns.com/posts/first-time-with-pypi.html
echo "Uploading to pypi"
python setup.py sdist upload -r pypi
