watch_file uv.lock
export VCPKG_DISABLE_METRICS=TRUE
uv sync --all-extras
export VIRTUAL_ENV="$PWD/.venv"
PATH_add "$VIRTUAL_ENV/bin"

# Build configuration for miepy
# Sets compiler paths and CMake arguments for building C++/Fortran extensions

# Detect architecture
ARCH=$(uname -m)

if [[ "$OSTYPE" == "darwin"* ]]; then
    # macOS-specific configuration

    # Get Homebrew paths
    if command -v brew &> /dev/null; then
        LIBOMP_PREFIX=$(brew --prefix libomp 2>/dev/null || echo "")
        GCC_PREFIX=$(brew --prefix gcc 2>/dev/null || echo "")

        if [ -n "$GCC_PREFIX" ] && [ -d "$GCC_PREFIX" ]; then
            # Find GCC version
            GCC_VERSION=$(ls ${GCC_PREFIX}/bin/gcc-* 2>/dev/null | grep -oE '[0-9]+$' | head -n1)

            if [ -n "$GCC_VERSION" ]; then
                # Set compiler paths
                export CC="${GCC_PREFIX}/bin/gcc-${GCC_VERSION}"
                export CXX="${GCC_PREFIX}/bin/g++-${GCC_VERSION}"
                export FC="${GCC_PREFIX}/bin/gfortran-${GCC_VERSION}"
            fi
        fi

        # Set OpenMP paths if available
        if [ -n "$LIBOMP_PREFIX" ] && [ -d "$LIBOMP_PREFIX" ]; then
            export OpenMP_ROOT="${LIBOMP_PREFIX}"
        fi
    fi

    # Determine vcpkg triplet based on architecture
    if [ "$ARCH" = "arm64" ]; then
        VCPKG_TRIPLET="arm64-osx"
    else
        VCPKG_TRIPLET="x64-osx"
    fi

    # Set vcpkg overlay triplets path
    export VCPKG_OVERLAY_TRIPLETS="${PWD}/vcpkg/triplets"

    # Build CMAKE_ARGS for scikit-build-core
    if [ -n "$CC" ] && [ -n "$CXX" ] && [ -n "$FC" ]; then
        export CMAKE_ARGS="-DCMAKE_C_COMPILER=${CC} -DCMAKE_CXX_COMPILER=${CXX} -DCMAKE_Fortran_COMPILER=${FC} -DVCPKG_TARGET_TRIPLET=${VCPKG_TRIPLET}"
    fi
fi
