#!/usr/bin/env bash

INSTALL_ONLY="false"

while getopts "i" options; do
    case "${options}" in
	i)
	    INSTALL_ONLY="true"
	    ;;
    esac
done

set -e

VERSION="$(python setup.py --version)"

TARBALL="dist/parsemon2-${VERSION}.tar.gz"

function build_tarball() {
    nix-shell -p python3 --command 'python setup.py sdist'
}

function upload_check() {
    nix-shell -p python3Packages.twine --command \
	  "twine upload -r test-pypi ${TARBALL}"
}

function do_import_check() {
    python -c 'import parsemon' || (echo $1 && exit 1)
}

function remove_parsemon() {
    python -m pip uninstall parsemon2 -y
}

function install_check() {
    rm -rf testenv
    mkdir -p "testenv"
    nix-shell \
	-p python3Packages.virtualenv \
	--command "virtualenv testenv"
    source testenv/bin/activate

    # install dependencies from regular pypi
    python -m pip install -e .
    remove_parsemon

    # install parsemon2 from generated dist file
    python -m pip install "${TARBALL}"
    do_import_check "Installation from tarball failed"
    remove_parsemon

    # install parsemon2 from pypi test
    python -m pip install \
		    --no-cache-dir \
		    --index-url https://test.pypi.org/simple/ \
		    parsemon2==${VERSION}
    do_import_check "Installation from PyPI failed"
    deactivate
}

build_tarball
if [ ${INSTALL_ONLY} = "false" ]; then
    upload_check
fi
install_check
