#!/usr/bin/env bash

# Immediate exit on failure
set -e

# Check all pyproject.toml files that have changed
for file in $(find . -print | sed 's|^./||' | grep -E "(^services/[^/]+/pyproject.toml$|^core/pyproject.toml$)"); do
    # Extract the current version and build the expected tag
    dirpath=$(dirname $file)
    expected_tag=$(scripts/helper.sh $dirpath --path-version)
    version=$(scripts/helper.sh $dirpath)
    # Check if the tag already exists
    if git rev-parse --verify $expected_tag^{tag} &> /dev/null; then
        echo "Tag '$expected_tag' already exists."
    else
        # Tag doesn't exist. Create tag and build/publish to PyPi
        echo "Tag '$expected_tag' does not exist. Creating new tag to trigger release."
        git tag -a $expected_tag -m "Release $version"
        git push origin tag $expected_tag
        cd $dirpath
        uv build
        uv publish --username="__token__" --password="$PYPI_TOKEN"
        cd $GITHUB_WORKSPACE
    fi              
done
