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, ["libchemfiles"], :libchemfiles),
]

# Download binaries from hosted location
bin_prefix = "https://github.com/JuliaBinaryWrappers/Chemfiles_jll.jl/releases/download/Chemfiles-v0.10.1+0"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    Linux(:aarch64, libc=:glibc, compiler_abi=CompilerABI(:gcc_any, :cxx03)) => ("$bin_prefix/Chemfiles.v0.10.1.aarch64-linux-gnu-cxx03.tar.gz", "bf07bc28429f9205b926ce976ada2553eeb66ae3cd17955487e81d77ffcdb4cd"),
    Linux(:aarch64, libc=:glibc, compiler_abi=CompilerABI(:gcc_any, :cxx11)) => ("$bin_prefix/Chemfiles.v0.10.1.aarch64-linux-gnu-cxx11.tar.gz", "b279cc3c23df3b6bed5faeb5c3ad81916108b4e2965b90d0c7b8eb6883ab359b"),
    Linux(:aarch64, libc=:musl, compiler_abi=CompilerABI(:gcc_any, :cxx03)) => ("$bin_prefix/Chemfiles.v0.10.1.aarch64-linux-musl-cxx03.tar.gz", "312e2c029d48ce1f703cf8a170f414cfcbbf0ab302f1a900d5869e0bd1968209"),
    Linux(:aarch64, libc=:musl, compiler_abi=CompilerABI(:gcc_any, :cxx11)) => ("$bin_prefix/Chemfiles.v0.10.1.aarch64-linux-musl-cxx11.tar.gz", "ec8eac86c5437bf46bf7e7703440221c5e34764ab41dda163de57c22a4ca6524"),
    Linux(:armv7l, libc=:glibc, call_abi=:eabihf, compiler_abi=CompilerABI(:gcc_any, :cxx03)) => ("$bin_prefix/Chemfiles.v0.10.1.armv7l-linux-gnueabihf-cxx03.tar.gz", "f321ddeb5ca83f4bafbd817f5554a3e22add3265ae9a55704c6186eadf2f176b"),
    Linux(:armv7l, libc=:glibc, call_abi=:eabihf, compiler_abi=CompilerABI(:gcc_any, :cxx11)) => ("$bin_prefix/Chemfiles.v0.10.1.armv7l-linux-gnueabihf-cxx11.tar.gz", "3717a6201775b4c940f41db7087a0a9d7c8b9a84adce1a9a58b0a837d01f36e8"),
    Linux(:armv7l, libc=:musl, call_abi=:eabihf, compiler_abi=CompilerABI(:gcc_any, :cxx03)) => ("$bin_prefix/Chemfiles.v0.10.1.armv7l-linux-musleabihf-cxx03.tar.gz", "4bdcae96493b417b65a5cab139121ec3bd9316777f12584b848ac6a1703bf076"),
    Linux(:armv7l, libc=:musl, call_abi=:eabihf, compiler_abi=CompilerABI(:gcc_any, :cxx11)) => ("$bin_prefix/Chemfiles.v0.10.1.armv7l-linux-musleabihf-cxx11.tar.gz", "18f275a78b0f0880f7bd3e41d6a2cdff8fc90b52381a2e3327fbd55d6f165c82"),
    Linux(:i686, libc=:glibc, compiler_abi=CompilerABI(:gcc_any, :cxx03)) => ("$bin_prefix/Chemfiles.v0.10.1.i686-linux-gnu-cxx03.tar.gz", "2e294ae0fd49b91886c35fdff8ec1b21b86ce911fedbc60f437d930049941ae6"),
    Linux(:i686, libc=:glibc, compiler_abi=CompilerABI(:gcc_any, :cxx11)) => ("$bin_prefix/Chemfiles.v0.10.1.i686-linux-gnu-cxx11.tar.gz", "b567565bd871c3ea767724e17d098447015d813e19f8f25a6f52777533716b5f"),
    Linux(:i686, libc=:musl, compiler_abi=CompilerABI(:gcc_any, :cxx03)) => ("$bin_prefix/Chemfiles.v0.10.1.i686-linux-musl-cxx03.tar.gz", "178e6161b66b3c5651d8698a73276c95244ecfe3e91cf2fad311e1349cd31cb8"),
    Linux(:i686, libc=:musl, compiler_abi=CompilerABI(:gcc_any, :cxx11)) => ("$bin_prefix/Chemfiles.v0.10.1.i686-linux-musl-cxx11.tar.gz", "e3554b10699475de0423d6939a3f9cd1057dcb839f5e3b6e39f70473a68cf705"),
    Windows(:i686, compiler_abi=CompilerABI(:gcc_any, :cxx03)) => ("$bin_prefix/Chemfiles.v0.10.1.i686-w64-mingw32-cxx03.tar.gz", "518e931c326c28c03c8d2d7019c117a4efad1c740f38146388ac7dc77ee86043"),
    Windows(:i686, compiler_abi=CompilerABI(:gcc_any, :cxx11)) => ("$bin_prefix/Chemfiles.v0.10.1.i686-w64-mingw32-cxx11.tar.gz", "5880800642560147a445ad99c065bb2f327acebfb78d99b4fd875b32da1a3d8d"),
    Linux(:powerpc64le, libc=:glibc, compiler_abi=CompilerABI(:gcc_any, :cxx03)) => ("$bin_prefix/Chemfiles.v0.10.1.powerpc64le-linux-gnu-cxx03.tar.gz", "3a23f4f901b59c726b84f7a1b4171d7817b0e0fdd847444e10cafa77a276db59"),
    Linux(:powerpc64le, libc=:glibc, compiler_abi=CompilerABI(:gcc_any, :cxx11)) => ("$bin_prefix/Chemfiles.v0.10.1.powerpc64le-linux-gnu-cxx11.tar.gz", "19d4d8aa3882a12908f1bea53f5e10e780244c6118537bd5fe7a6056d10f6c73"),
    MacOS(:x86_64) => ("$bin_prefix/Chemfiles.v0.10.1.x86_64-apple-darwin.tar.gz", "beb206c18e58e890c0ccf33b822f353cb93d71facbb5dbe5253b08bb5b6dd804"),
    Linux(:x86_64, libc=:glibc, compiler_abi=CompilerABI(:gcc_any, :cxx03)) => ("$bin_prefix/Chemfiles.v0.10.1.x86_64-linux-gnu-cxx03.tar.gz", "90523b3554e36145ae9a62e82687e10dc7fc845ed232b507bc695f06ce2ebfbf"),
    Linux(:x86_64, libc=:glibc, compiler_abi=CompilerABI(:gcc_any, :cxx11)) => ("$bin_prefix/Chemfiles.v0.10.1.x86_64-linux-gnu-cxx11.tar.gz", "d74dcf89b846cdf7a8136746c22779391a71a5d820322af1df1106e516dd0a78"),
    Linux(:x86_64, libc=:musl, compiler_abi=CompilerABI(:gcc_any, :cxx03)) => ("$bin_prefix/Chemfiles.v0.10.1.x86_64-linux-musl-cxx03.tar.gz", "bab29d8f9077c0027f3a0008e64124cc3b6557524d361fbc81c84fccc1eb3dcb"),
    Linux(:x86_64, libc=:musl, compiler_abi=CompilerABI(:gcc_any, :cxx11)) => ("$bin_prefix/Chemfiles.v0.10.1.x86_64-linux-musl-cxx11.tar.gz", "239b200e35bc54cc1a8d6eba8ab34be99bc520f0eb8740ceabda7995a00008a6"),
    FreeBSD(:x86_64) => ("$bin_prefix/Chemfiles.v0.10.1.x86_64-unknown-freebsd.tar.gz", "e80b45cdaca230861f55e824ce7645ea0ec81d40d82e7ec35097deaa80b5a18a"),
    Windows(:x86_64, compiler_abi=CompilerABI(:gcc_any, :cxx03)) => ("$bin_prefix/Chemfiles.v0.10.1.x86_64-w64-mingw32-cxx03.tar.gz", "a3da44fb48207ca84180e6a8ae57d7d760f2a5d775ba9b34293faef38b5f9ea9"),
    Windows(:x86_64, compiler_abi=CompilerABI(:gcc_any, :cxx11)) => ("$bin_prefix/Chemfiles.v0.10.1.x86_64-w64-mingw32-cxx11.tar.gz", "665b072a5701ad7ad418b1d1b4fc2f2df8ba7d4f19b70f5b32c90e2553be512b"),
)

# Install unsatisfied or updated dependencies:
unsatisfied = any(!satisfied(p; verbose=verbose) for p in products)
dl_info = choose_download(download_info, platform_key_abi())
if dl_info === nothing && unsatisfied
    # If we don't have a 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 (\"$(Sys.MACHINE)\", parsed as \"$(triplet(platform_key_abi()))\") is not supported by this package!")
end

# If we have a download, and we are unsatisfied (or the version we're
# trying to install is not itself installed) then load it up!
if unsatisfied || !isinstalled(dl_info...; prefix=prefix)
    # Download and install binaries
    install(dl_info...; prefix=prefix, force=true, verbose=verbose)
end

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