#!/bin/bash

set -e

isPoetryInstalled=$(which poetry > /dev/null && echo $?)

if [[ $isPoetryInstalled -eq 1 ]]; then
  echo "poetry is not installed, cannot run python build"
  exit 1
fi

echo "Current math3d version $(poetry version)"

if [[ $# -eq 1 && "$1" == "patch_ver" ]]; then
    echo "Upgrading patch version"
    poetry version patch
fi

if [[ $# -eq 1 && "$1" == "minor_ver" ]]; then
    echo "Upgrading minor version"
    poetry version minor
fi

if [[ $# -eq 1 && "$1" == "major_ver" ]]; then
    echo "Upgrading major version"
    poetry version major
fi

rm -rf dist/*
touch py/math3d/py.typed
poetry run pybind11-stubgen math3d -o py
CMAKE_BUILD_PARALLEL_LEVEL="$(sysctl -n hw.ncpu)" \
CMAKE_ARGS="-DenableTesting=OFF" poetry run python -m build
poetry run twine upload dist/*
