using BinaryProvider

# This is where all binaries will get installed
const prefix = Prefix(!isempty(ARGS) ? ARGS[1] : joinpath(@__DIR__,"usr"))

libsundials_nvecserial       = LibraryProduct(prefix, String["libsundials_nvecserial"], :libsundials_nvecserial)
libsundials_kinsol           = LibraryProduct(prefix, String["libsundials_kinsol"], :libsundials_kinsol)
libsundials_cvodes           = LibraryProduct(prefix, String["libsundials_cvodes"], :libsundials_cvodes)
libsundials_idas             = LibraryProduct(prefix, String["libsundials_idas"], :libsundials_idas)
libsundials_sunlinsolspfgmr  = LibraryProduct(prefix, String["libsundials_sunlinsolspfgmr"], :libsundials_sunlinsolspfgmr)
libsundials_sunmatrixdense   = LibraryProduct(prefix, String["libsundials_sunmatrixdense"], :libsundials_sunmatrixdense)
libsundials_sunlinsolspbcgs  = LibraryProduct(prefix, String["libsundials_sunlinsolspbcgs"], :libsundials_sunlinsolspbcgs)
libsundials_sunlinsoldense   = LibraryProduct(prefix, String["libsundials_sunlinsoldense"], :libsundials_sunlinsoldense)
libsundials_sunlinsolspgmr   = LibraryProduct(prefix, String["libsundials_sunlinsolspgmr"], :libsundials_sunlinsolspgmr)
libsundials_sunlinsolpcg     = LibraryProduct(prefix, String["libsundials_sunlinsolpcg"], :libsundials_sunlinsolpcg)
libsundials_sunlinsolsptfqmr = LibraryProduct(prefix, String["libsundials_sunlinsolsptfqmr"], :libsundials_sunlinsolsptfqmr)
libsundials_sunmatrixsparse  = LibraryProduct(prefix, String["libsundials_sunmatrixsparse"], :libsundials_sunmatrixsparse)
libsundials_sunlinsolband    = LibraryProduct(prefix, String["libsundials_sunlinsolband"], :libsundials_sunlinsolband)
libsundials_sunmatrixband    = LibraryProduct(prefix, String["libsundials_sunmatrixband"], :libsundials_sunmatrixband)
libsundials_arkode           = LibraryProduct(prefix, String["libsundials_arkode"], :libsundials_arkode)
libsundials_sunlinsolklu     = LibraryProduct(prefix, String["libsundials_sunlinsolklu"], :libsundials_sunlinsolklu)

products = [
    libsundials_nvecserial,
    libsundials_kinsol,
    libsundials_cvodes,
    libsundials_idas,
    libsundials_sunlinsolspfgmr,
    libsundials_sunmatrixdense,
    libsundials_sunlinsolspbcgs,
    libsundials_sunlinsoldense,
    libsundials_sunlinsolspgmr,
    libsundials_sunlinsolpcg,
    libsundials_sunlinsolsptfqmr,
    libsundials_sunmatrixsparse,
    libsundials_sunlinsolband,
    libsundials_sunmatrixband,
    libsundials_arkode,
    libsundials_sunlinsolklu,
]

# Download binaries from hosted location
bin_prefix = "https://github.com/JuliaDiffEq/SundialsBuilder/releases/download/sundials-3.1.0-3"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    BinaryProvider.Linux(:aarch64, :glibc) => ("$bin_prefix/Sundials.aarch64-linux-gnu.tar.gz", "ded75e83b80710015bd5e06b950e47bf5e58cf13c8c7203fbfdc6d20652c8610"),
    BinaryProvider.Linux(:armv7l, :glibc) => ("$bin_prefix/Sundials.arm-linux-gnueabihf.tar.gz", "989f8535dc03940964bbef79918056ddf3922e490c905cf7482752bc8c99d604"),
    BinaryProvider.Linux(:i686, :glibc) => ("$bin_prefix/Sundials.i686-linux-gnu.tar.gz", "6fbc1769dba5534f3c53b15164ef64132027bb44f7a8b33ea06a1a863d7c3503"),
    BinaryProvider.Windows(:i686) => ("$bin_prefix/Sundials.i686-w64-mingw32.tar.gz", "939106914a1bd438ec6cedab3644f09560ab192fa5cabfe3fde9aca04fb26049"),
    BinaryProvider.Linux(:powerpc64le, :glibc) => ("$bin_prefix/Sundials.powerpc64le-linux-gnu.tar.gz", "1e94174b63a6edb052fe2b5374a07a76937833fbcb0236d4b319f19871de267d"),
    BinaryProvider.MacOS() => ("$bin_prefix/Sundials.x86_64-apple-darwin14.tar.gz", "7c9b0a73c334c9e7cd3b7b43a6df8c9275feb7fd02c7240e3804ea29a50dbedb"),
    BinaryProvider.Linux(:x86_64, :glibc) => ("$bin_prefix/Sundials.x86_64-linux-gnu.tar.gz", "10cf64bc564a45459359b07a5a9708b73b0808daff47ef6fc86a530ad1e49b27"),
    BinaryProvider.Windows(:x86_64) => ("$bin_prefix/Sundials.x86_64-w64-mingw32.tar.gz", "4e3e1b1e60b0ec59922b64bab70a2eac66f287aa2f0ac0826481953cd3f81664"),
)
if platform_key() in keys(download_info)
    # First, check to see if we're all satisfied
    if any(!satisfied(p; verbose=true) for p in products)
        # Download and install binaries
        url, tarball_hash = download_info[platform_key()]
        install(url, tarball_hash; prefix=prefix, force=true, verbose=true)
    end

    # Finally write out a deps.jl file
    write_deps_file(joinpath(@__DIR__, "deps.jl"), products)
else
    error("Your platform $(triplet(platform_key())) is not supported by this package!")
end
