#!/bin/bash

set -e

echo "OpenCrates Publishing Script v3.0.0"
echo "=================================="

# Ensure we're in the right directory
cd "$(dirname "$0")/.."

echo "Cleaning previous builds..."
cargo clean

echo "Running comprehensive tests..."
cargo test --all-features

echo "Checking code formatting..."
cargo fmt --check

echo "Running linters..."
cargo clippy --all-features -- -D warnings

echo "Building documentation..."
cargo doc --no-deps --all-features

echo "Checking for security vulnerabilities..."
if command -v cargo-audit &> /dev/null; then
    cargo audit
else
    echo "WARNING: cargo-audit not found, skipping security check"
fi

echo "Verifying package contents..."
cargo package --dry-run

echo "Running dry-run publish..."
if cargo publish --dry-run; then
    echo ""
    echo "All checks passed!"
    echo ""
    read -p "Ready to publish to crates.io? (y/N): " -n 1 -r
    echo ""
    
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        echo "Publishing to crates.io..."
        cargo publish
        echo "Successfully published OpenCrates v3.0.0!"
        echo "View at: https://crates.io/crates/opencrates"
    else
        echo "Publishing cancelled."
    fi
else
    echo "Dry-run failed. Please fix issues before publishing."
    exit 1
fi 