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

# Download binaries from hosted location
bin_prefix = "https://github.com/kbarbary/DierckxBuilder/releases/download/v1.0.0-1"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    Linux(:armv7l, libc=:glibc, call_abi=:eabihf, compiler_abi=CompilerABI(:gcc4)) => ("$bin_prefix/libddierckx.v1.0.0.arm-linux-gnueabihf-gcc4.tar.gz", "c83224796f4a8fdbf3d74ba91f2a430680611be3a1e7c8ef1517377dad9bf606"),
    Linux(:armv7l, libc=:glibc, call_abi=:eabihf, compiler_abi=CompilerABI(:gcc7)) => ("$bin_prefix/libddierckx.v1.0.0.arm-linux-gnueabihf-gcc7.tar.gz", "2246e492f55df8759bf957e8ffa562bfd241206470fe1d7c78c67d060e86e69a"),
    Linux(:armv7l, libc=:glibc, call_abi=:eabihf, compiler_abi=CompilerABI(:gcc8)) => ("$bin_prefix/libddierckx.v1.0.0.arm-linux-gnueabihf-gcc8.tar.gz", "f096dbacfd47aec9033fac72be9e4a3922d53a275c051c42c053c3e385c03c28"),
    Linux(:i686, libc=:glibc, compiler_abi=CompilerABI(:gcc4)) => ("$bin_prefix/libddierckx.v1.0.0.i686-linux-gnu-gcc4.tar.gz", "2a52576cd189121dd584d7d9ff3f00b1a7ed469081217033e4b793e4f29ae85b"),
    Linux(:i686, libc=:glibc, compiler_abi=CompilerABI(:gcc7)) => ("$bin_prefix/libddierckx.v1.0.0.i686-linux-gnu-gcc7.tar.gz", "a1859e7cbd1c4d7b6fab82cd583dba8ac0f3562cb049c427c55d96c4d36d6eed"),
    Linux(:i686, libc=:glibc, compiler_abi=CompilerABI(:gcc8)) => ("$bin_prefix/libddierckx.v1.0.0.i686-linux-gnu-gcc8.tar.gz", "dfa3de97899cff88dba969c9951fce210f01fb34da89a64d0fd78887e563628a"),
    Windows(:i686, compiler_abi=CompilerABI(:gcc4)) => ("$bin_prefix/libddierckx.v1.0.0.i686-w64-mingw32-gcc4.tar.gz", "919eceff111d9cbc6431f239bf6f23097656e93937e66ffdcab67a47c172e941"),
    Windows(:i686, compiler_abi=CompilerABI(:gcc7)) => ("$bin_prefix/libddierckx.v1.0.0.i686-w64-mingw32-gcc7.tar.gz", "38d1dc9f63ad9a8465c6b49ea4336d93d9d92487b84809d77d78717022406e44"),
    Windows(:i686, compiler_abi=CompilerABI(:gcc8)) => ("$bin_prefix/libddierckx.v1.0.0.i686-w64-mingw32-gcc8.tar.gz", "e0e7fa9534449f6c397efd195de2af54a9e0b010c603adb22119ec6f1f49feef"),
    MacOS(:x86_64, compiler_abi=CompilerABI(:gcc7)) => ("$bin_prefix/libddierckx.v1.0.0.x86_64-apple-darwin14-gcc7.tar.gz", "ff0e49b74a05012cfc1198da460b0e9d36dca90adf53c3964836f86a2ad7275c"),
    MacOS(:x86_64, compiler_abi=CompilerABI(:gcc8)) => ("$bin_prefix/libddierckx.v1.0.0.x86_64-apple-darwin14-gcc8.tar.gz", "809a205cd7a8df3d3723e9c013b855c9cb034ff881d3eda820f26809d1efad8a"),
    Linux(:x86_64, libc=:glibc, compiler_abi=CompilerABI(:gcc4)) => ("$bin_prefix/libddierckx.v1.0.0.x86_64-linux-gnu-gcc4.tar.gz", "2b4061d85a1f5b40f37880e27be00b1b3dc7c628d88e1a024c64492aed067339"),
    Linux(:x86_64, libc=:glibc, compiler_abi=CompilerABI(:gcc7)) => ("$bin_prefix/libddierckx.v1.0.0.x86_64-linux-gnu-gcc7.tar.gz", "5b6c3b511ca64e59054a3c196c7e840d3b8d2d4ad04beefb2bd246573f67e936"),
    Linux(:x86_64, libc=:glibc, compiler_abi=CompilerABI(:gcc8)) => ("$bin_prefix/libddierckx.v1.0.0.x86_64-linux-gnu-gcc8.tar.gz", "f75ae37c668c276922254ef6320e2ff0824bae1a88e4f554396a873df7885f29"),
    FreeBSD(:x86_64, compiler_abi=CompilerABI(:gcc4)) => ("$bin_prefix/libddierckx.v1.0.0.x86_64-unknown-freebsd11.1-gcc4.tar.gz", "dcbd52553fe730852ee31c1fc0f2b0f6b5aa6db5f820505aa9ba77b73ebcc4d7"),
    FreeBSD(:x86_64, compiler_abi=CompilerABI(:gcc7)) => ("$bin_prefix/libddierckx.v1.0.0.x86_64-unknown-freebsd11.1-gcc7.tar.gz", "22ed4639567896a469c00dd7e5cd4ac97038f4763d055698c1073a0932c13b32"),
    FreeBSD(:x86_64, compiler_abi=CompilerABI(:gcc8)) => ("$bin_prefix/libddierckx.v1.0.0.x86_64-unknown-freebsd11.1-gcc8.tar.gz", "f550f78201b27c82e41103b6d5db482c266281eb01968a33b98b82b9fb40b171"),
    Windows(:x86_64, compiler_abi=CompilerABI(:gcc4)) => ("$bin_prefix/libddierckx.v1.0.0.x86_64-w64-mingw32-gcc4.tar.gz", "d67dcd3521c973e3deb0efecdf192871ba5c22c994426394d474fa6c7ab7a9a7"),
    Windows(:x86_64, compiler_abi=CompilerABI(:gcc7)) => ("$bin_prefix/libddierckx.v1.0.0.x86_64-w64-mingw32-gcc7.tar.gz", "98ebdea7468d0fcd0c5ec266766c0cdb4e9f9c5cb89dbd53f5102f646a533385"),
    Windows(:x86_64, compiler_abi=CompilerABI(:gcc8)) => ("$bin_prefix/libddierckx.v1.0.0.x86_64-w64-mingw32-gcc8.tar.gz", "eb54c673b95cae7b0743858a789aa770422b823f97bfde2efca16b13800c3818"),
)

# 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)
