#!/bin/bash

# Build and publish script for the "pytem1d" package
# Usage: ./pypi_build_script

set -e  # Exit on error

MODULE_NAME="pytem1d"
VERSION=$(grep "^version" pyproject.toml | sed -E 's/.*"([0-9]+\.[0-9]+\.[0-9]+)".*/\1/')

echo "=== Building $MODULE_NAME v$VERSION ==="
echo "Note: This package includes:"
echo "      - Pre-compiled binaries for Windows (.dll)"
echo "      - Auto-build support for Linux/macOS (requires gfortran + make)"
echo "      See INSTALL.md for details"

# Check for compiled libraries
echo -e "\n=== Checking for compiled libraries ==="
LIB_DIR="src/pytem1d/lib"
FOUND_LIBS=0

if [ -f "$LIB_DIR/libtem1d.so" ]; then
    echo "✓ Found Linux library: libtem1d.so"
    FOUND_LIBS=$((FOUND_LIBS + 1))
fi

if [ -f "$LIB_DIR/libtem1d.dylib" ]; then
    echo "✓ Found macOS library: libtem1d.dylib"
    FOUND_LIBS=$((FOUND_LIBS + 1))
fi

if [ -f "$LIB_DIR/libtem1d.dll" ]; then
    echo "✓ Found Windows library: libtem1d.dll"
    FOUND_LIBS=$((FOUND_LIBS + 1))
fi

if [ $FOUND_LIBS -eq 0 ]; then
    echo "⚠ WARNING: No compiled libraries found in $LIB_DIR"
    echo "  Run 'make' to build the library before packaging"
    read -p "Continue anyway? [y/N] " continue_build
    if [[ ! $continue_build =~ ^[Yy]$ ]]; then
        echo "Aborted. Please build the library first with 'make'"
        exit 1
    fi
else
    echo "Found $FOUND_LIBS library file(s)"
fi

# Clean up old builds
echo -e "\n=== Cleaning old build artifacts ==="
rm -rf dist/ build/ *.egg-info/

# Make sure you have the required tools
echo "Updating build tools..."
pip install --upgrade pip
pip install --upgrade build setuptools wheel twine

# Build distribution packages using build module (PEP 517)
echo -e "\n=== Building distribution packages ==="
python -m build

echo -e "\n=== Build complete ==="
echo "Distribution files created:"
ls -lh dist/

# Verify libraries are included in the wheel
echo -e "\n=== Verifying library files in wheel ==="
WHEEL_FILE=$(ls dist/*.whl 2>/dev/null | head -1)
if [ -f "$WHEEL_FILE" ]; then
    echo "Inspecting: $(basename $WHEEL_FILE)"
    unzip -l "$WHEEL_FILE" | grep -E "\.so|\.dll|\.dylib" || echo "⚠ WARNING: No library files found in wheel!"
else
    echo "⚠ No wheel file found"
fi

# Upload to TestPyPI first
echo -e "\n=== TestPyPI Upload ==="
read -p "Upload to TestPyPI for verification? [y/N] " test_upload
if [[ $test_upload =~ ^[Yy]$ ]]; then
    python -m twine upload --repository testpypi dist/*
    echo -e "\nTestPyPI upload complete!"
    echo "You can install and test with:"
    echo "pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ $MODULE_NAME==$VERSION"
    echo "This uses TestPyPI for $MODULE_NAME but regular PyPI for dependencies."
fi

# Upload to PyPI when ready
echo -e "\n=== PyPI Upload ==="
read -p "Upload to PyPI for production release? [y/N] " prod_upload
if [[ $prod_upload =~ ^[Yy]$ ]]; then
    python -m twine upload dist/*
    echo -e "\nPyPI upload complete!"
    echo "Your package is now available via:"
    echo "pip install $MODULE_NAME"
else
    echo -e "\nSkipped PyPI upload. When ready for the real release, run:"
    echo "python -m twine upload dist/*"
fi
