using Pkg
Pkg.update(update_registry=false)

using Artifacts
using NixSourcerer
using Downloads
using CxxWrap.CxxWrapCore: libcxxwrap_julia_jll

function main()
    cd(@__DIR__) do
        meta = artifact_meta("libcxxwrap_julia", find_artifacts_toml(pkgdir(libcxxwrap_julia_jll)))
        url = nothing
        for dl in meta["download"]
            try
                # Test URL 
                Downloads.download(dl["url"])
                url = dl["url"]
                break
            catch e
                @error e
            end
        end
        if url !== nothing
            p = NixSourcerer.read_project(joinpath(@__DIR__, "NixProject.toml"))
            p.specs["JlCxx"] = Dict("type" => "archive", "url" => url, "extraArgs" => Dict("stripRoot" => false))
            NixSourcerer.write_project(p, joinpath(@__DIR__, "NixProject.toml"))
            NixSourcerer.update_package()
        else
            error("Update failed")
        end
    end
end

main()
