#!/bin/bash

# Script to release a new version of the application
# Usage: ./release <new_version>

set -e  # Exit on any error

if [ $# -ne 1 ]; then
    echo "Usage: $0 <new_version>"
    echo "Example: $0 1.0.0"
    exit 1
fi

NEW_VERSION="$1"
WORKSPACE_CARGO="Cargo.toml"
CRATE_CARGO="desktop/Cargo.toml"

# Check if both Cargo.toml files exist
if [ ! -f "$WORKSPACE_CARGO" ]; then
    echo "Error: $WORKSPACE_CARGO not found in current directory"
    exit 1
fi

if [ ! -f "$CRATE_CARGO" ]; then
    echo "Error: $CRATE_CARGO not found"
    exit 1
fi

# Function to update version in a Cargo.toml file
update_version() {
    local file="$1"
    local backup="${file}.bak"

    # Backup the original file
    cp "$file" "$backup"

    # More flexible regex that handles various version formats and whitespace.
    # Note: -i.bak works on both GNU and BSD sed.
    if sed -i.bak -E "s/^[[:space:]]*version[[:space:]]*=[[:space:]]*\"[^\"]+\"/version = \"$NEW_VERSION\"/" "$file"; then
        echo "✓ Updated version to $NEW_VERSION in $file"
    else
        echo "Error: Failed to update version in $file"
        # Restore original backup
        mv "$backup" "$file"
        exit 1
    fi

    # Remove the backup file
    rm -f "$backup"
}

# Update both Cargo.toml files
echo "Updating versions..."
update_version "$WORKSPACE_CARGO"
update_version "$CRATE_CARGO"

COMMIT_MSG="chore: release version $NEW_VERSION"

# When the requested version is already set there is nothing to bump or commit,
# so the current commit is tagged as-is.
if git diff --quiet -- "$WORKSPACE_CARGO" "$CRATE_CARGO"; then
    echo "Version is already $NEW_VERSION, tagging the current commit"
else
    # Check git status before committing
    echo "Checking git status..."
    # The version files are always modified at this point, so only ask about other changes.
    if [ -n "$(git status --porcelain -- . ":(exclude,top)$WORKSPACE_CARGO" ":(exclude,top)$CRATE_CARGO")" ]; then
        echo "Current uncommitted changes:"
        git status --short

        # Ask user if they want to commit all changes or just version files
        echo ""
        echo "Do you want to:"
        echo "1) Commit all current changes (including the version updates)"
        echo "2) Commit only the version file changes"
        echo "3) Abort the release"
        read -p "Enter choice (1/2/3): " choice

        case $choice in
            1)
                echo "Committing all changes..."
                git add .
                ;;
            2)
                echo "Committing only version file changes..."
                git add "$WORKSPACE_CARGO" "$CRATE_CARGO"
                ;;
            3)
                echo "Release aborted by user"
                exit 0
                ;;
            *)
                echo "Invalid choice. Release aborted."
                exit 1
                ;;
        esac
    else
        # Only version files were modified, add them specifically
        echo "Only version files were modified, adding them for commit..."
        git add "$WORKSPACE_CARGO" "$CRATE_CARGO"
    fi

    # Commit the changes
    if git commit -m "$COMMIT_MSG"; then
        echo "✓ Committed version changes"
    else
        echo "Error: Failed to commit version changes"
        exit 1
    fi

    # Push version changes to origin
    echo "Pushing version changes to origin..."
    if git push origin master; then
        echo "✓ Successfully pushed version changes to origin"
    else
        echo "Error: Failed to push version changes to origin"
        exit 1
    fi
fi

# Create git tag
TAG_NAME="v$NEW_VERSION"

if git rev-parse -q --verify "refs/tags/$TAG_NAME" >/dev/null; then
    echo "Error: tag $TAG_NAME already exists"
    exit 1
fi

if git tag -a "$TAG_NAME" -m "$COMMIT_MSG"; then
    echo "✓ Created git tag: $TAG_NAME"
else
    echo "Error: Failed to create git tag"
    exit 1
fi

# Push tag to origin
echo "Pushing tag to origin..."
if git push origin "$TAG_NAME"; then
    echo "✓ Successfully pushed tag to origin"
    echo "✓ Release $NEW_VERSION completed successfully!"
else
    echo "Error: Failed to push tag to origin"
    exit 1
fi
