using BinaryProvider # requires BinaryProvider 0.3.0 or later

# Parse some basic command-line arguments
const verbose = "--verbose" in ARGS
const prefix = Prefix(get([a for a in ARGS if a != "--verbose"], 1, joinpath(@__DIR__, "usr")))
products = [
    LibraryProduct(prefix, String["libsuitesparseconfig"], :suitesparseconfig),
    LibraryProduct(prefix, String["libamd"], :amd),
    LibraryProduct(prefix, String["libbtf"], :btf),
    LibraryProduct(prefix, String["libcamd"], :camd),
    LibraryProduct(prefix, String["libccolamd"], :ccolamd),
    LibraryProduct(prefix, String["libcolamd"], :colamd),
    LibraryProduct(prefix, String["libcholmod"], :cholmod),
    LibraryProduct(prefix, String["libldl"], :ldl),
    LibraryProduct(prefix, String["libklu"], :klu),
    LibraryProduct(prefix, String["libumfpack"], :umfpack),
    LibraryProduct(prefix, String["librbio"], :rbio),
    LibraryProduct(prefix, String["libspqr"], :spqr),
]

# Download binaries from hosted location
bin_prefix = "https://github.com/JuliaLinearAlgebra/SuiteSparseBuilder/releases/download/v5.2.0-0.2.20"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    Linux(:aarch64, :glibc) => ("$bin_prefix/SuiteSparse.aarch64-linux-gnu.tar.gz", "f4b08b4b1dee6e6e7929ea2059309eefdc395554ee7d771a62e98779929aad1d"),
    Linux(:armv7l, :glibc, :eabihf) => ("$bin_prefix/SuiteSparse.arm-linux-gnueabihf.tar.gz", "d80c1718d8ce2835877907a2366a2b412f7920c8b79d0eaa9b4f5f4f3c464e2f"),
    Linux(:i686, :glibc) => ("$bin_prefix/SuiteSparse.i686-linux-gnu.tar.gz", "0eaa7a6eff01ce3c9b4a2d08350fba875f5559d349e018d3a839fcfe10964533"),
    Windows(:i686) => ("$bin_prefix/SuiteSparse.i686-w64-mingw32.tar.gz", "7284c271c9ba7731bb88ab12d912173479c568093f09ffc1e8b352b68b54b8c4"),
    MacOS(:x86_64) => ("$bin_prefix/SuiteSparse.x86_64-apple-darwin14.tar.gz", "6e7e4a5590ac7458026b2dafb49db6c5adaae3a30e98e8b627b8b97185d3abe1"),
    Linux(:x86_64, :glibc) => ("$bin_prefix/SuiteSparse.x86_64-linux-gnu.tar.gz", "e1a0ce686e696be79d62ad10160137911510c7618612a5f3019d002ef6903945"),
    Windows(:x86_64) => ("$bin_prefix/SuiteSparse.x86_64-w64-mingw32.tar.gz", "4e6e104ba32dfff14eb7a68bc7557c9fc9d4434c935c7ca6cfbc7c243dd5d25a"),
)

# Install unsatisfied or updated dependencies:
unsatisfied = any(!satisfied(p; verbose=verbose) for p in products)
if haskey(download_info, platform_key())
    url, tarball_hash = download_info[platform_key()]
    if unsatisfied || !isinstalled(url, tarball_hash; prefix=prefix)
        # Download and install binaries
        install(url, tarball_hash; prefix=prefix, force=true, verbose=verbose)
    end
elseif unsatisfied
    # If we don't have a BinaryProvider-compatible .tar.gz to download, complain.
    # Alternatively, you could attempt to install from a separate provider,
    # build from source or something even more ambitious here.
    error("Your platform $(triplet(platform_key())) is not supported by this package!")
end

# Write out a deps.jl file that will contain mappings for our products
write_deps_file(joinpath(@__DIR__, "deps.jl"), products)
