#!/bin/bash

# GCC 15.1 + CMake 4.1 + import std; Setup Validator
# Run this script to verify your environment supports import std;

echo "🔍 Checking GCC 15.1 + CMake 4.1 + import std; Setup..."
echo "=================================================="

# Check GCC version
echo "📋 Checking GCC version..."
GCC_VERSION=$(g++ --version | head -n1)
echo "   $GCC_VERSION"

if g++ --version | grep -q "15\."; then
    echo "   ✅ GCC 15.x detected"
else
    echo "   ❌ GCC 15.1+ required for import std;"
    echo "   💡 Install GCC 15.1 or later"
fi

# Check CMake version
echo ""
echo "📋 Checking CMake version..."
CMAKE_VERSION=$(cmake --version | head -n1)
echo "   $CMAKE_VERSION"

if cmake --version | grep -q "version [4-9]\."; then
    echo "   ✅ CMake 4.x+ detected"
else
    echo "   ❌ CMake 4.1+ required for CMAKE_EXPERIMENTAL_CXX_IMPORT_STD"
    echo "   💡 Upgrade to CMake 4.1 or later"
fi

# Check if modules compilation works
echo ""
echo "📋 Testing basic module compilation..."
cat > /tmp/test_modules.cpp << 'EOF'
export module test;
export int test_function() { return 42; }
EOF

if g++ -fmodules-ts -std=c++23 -c /tmp/test_modules.cpp -o /tmp/test_modules.o 2>/dev/null; then
    echo "   ✅ GCC modules compilation works"
    rm -f /tmp/test_modules.cpp /tmp/test_modules.o /tmp/test_modules.gcm
else
    echo "   ❌ GCC modules compilation failed"
    echo "   💡 Check GCC installation and -fmodules-ts support"
fi

# Create test project
echo ""
echo "📋 Creating test project for import std;..."
mkdir -p /tmp/import_std_test
cd /tmp/import_std_test

# Create CMakeLists.txt
cat > CMakeLists.txt << 'EOF'
cmake_minimum_required(VERSION 4.1)

# Enable experimental import std support
set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD "d0edc3af-4c50-42ea-a356-e2862fe7a444")

project(ImportStdTest)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_SCAN_FOR_MODULES ON)

add_executable(test_app main.cpp)

set_target_properties(test_app PROPERTIES
    CXX_STANDARD 23
    CXX_STANDARD_REQUIRED ON
    CXX_MODULE_STD 1
)

target_compile_options(test_app PRIVATE -fmodules-ts)
EOF

# Create test source
cat > main.cpp << 'EOF'
import std;

int main() {
    std::println("🎉 import std; is working!");
    
    std::vector<int> numbers = {1, 2, 3, 4, 5};
    std::println("Vector size: {}", numbers.size());
    
    auto message = std::format("Formatted number: {}", 42);
    std::println(message);
    
    return 0;
}
EOF

# Test configuration
echo "📋 Testing CMake configuration..."
if cmake -S . -B build -G Ninja 2>/dev/null; then
    echo "   ✅ CMake configuration successful"
    
    echo ""
    echo "📋 Testing build with import std;..."
    if cmake --build build 2>/dev/null; then
        echo "   ✅ Build successful!"
        
        echo ""
        echo "📋 Testing execution..."
        if ./build/test_app; then
            echo ""
            echo "🎉 SUCCESS! Your environment fully supports import std;"
            echo ""
            echo "📝 Configuration summary:"
            echo "   ✅ GCC 15.1+ with modules support"
            echo "   ✅ CMake 4.1+ with experimental import std"
            echo "   ✅ C++23 standard library modules"
            echo "   ✅ Proper CMake configuration"
            echo ""
            echo "🚀 You can now use import std; in your projects!"
        else
            echo "   ❌ Execution failed"
        fi
    else
        echo "   ❌ Build failed with import std;"
        echo "   💡 Check compiler logs above for details"
    fi
else
    echo "   ❌ CMake configuration failed"
    echo "   💡 Check CMake version and experimental feature support"
fi

# Cleanup
cd /
rm -rf /tmp/import_std_test

echo ""
echo "=================================================="
echo "📚 For complete setup guide, see:"
echo "   https://github.com/your-repo/cpp23-import-std-guide"
echo ""
echo "🔧 Quick reference configuration:"
echo "   cmake_minimum_required(VERSION 4.1)"
echo "   set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD \"d0edc3af-4c50-42ea-a356-e2862fe7a444\")"
echo "   set_target_properties(target PROPERTIES CXX_MODULE_STD 1)"