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, ["libLLVM"], :libLLVM),
    LibraryProduct(prefix, ["libLTO"], :libLTO),
]

# Download binaries from hosted location
bin_prefix = "https://github.com/staticfloat/LLVMBuilder/releases/download/v6.0.1-5+nowasm"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    Linux(:aarch64, libc=:glibc, compiler_abi=CompilerABI(:gcc4)) => ("$bin_prefix/LLVM.v6.0.1.aarch64-linux-gnu-gcc4.tar.gz", "529483759f97253e676948048e8a3ba067b2cd359b0956116f426c2dcae6fc20"),
    Linux(:aarch64, libc=:glibc, compiler_abi=CompilerABI(:gcc7)) => ("$bin_prefix/LLVM.v6.0.1.aarch64-linux-gnu-gcc7.tar.gz", "da3e8e8fcfe83fb98e0936784ff11246fe4a134fa27f65e35d166215c17bcc2f"),
    Linux(:aarch64, libc=:glibc, compiler_abi=CompilerABI(:gcc8)) => ("$bin_prefix/LLVM.v6.0.1.aarch64-linux-gnu-gcc8.tar.gz", "c04bd4a12a7edd888dd11fe9b12cb699f276aa7da8a6593a18ce08d98b4016aa"),
    Linux(:armv7l, libc=:glibc, call_abi=:eabihf, compiler_abi=CompilerABI(:gcc4)) => ("$bin_prefix/LLVM.v6.0.1.arm-linux-gnueabihf-gcc4.tar.gz", "80f95d9f2333ba59e17c7bfc6190b3d52736feb1f9baf0dc3f5e3662025c2fd5"),
    Linux(:armv7l, libc=:glibc, call_abi=:eabihf, compiler_abi=CompilerABI(:gcc7)) => ("$bin_prefix/LLVM.v6.0.1.arm-linux-gnueabihf-gcc7.tar.gz", "f84b89484ff5c17d68aeba79a6278aafd76134766e2c8452f5fb693d9aa7e107"),
    Linux(:armv7l, libc=:glibc, call_abi=:eabihf, compiler_abi=CompilerABI(:gcc8)) => ("$bin_prefix/LLVM.v6.0.1.arm-linux-gnueabihf-gcc8.tar.gz", "8f09debd429919ec946802d4e49584cf934193aa3fed7d46d691e1e436a17ec8"),
    Linux(:i686, libc=:glibc, compiler_abi=CompilerABI(:gcc4)) => ("$bin_prefix/LLVM.v6.0.1.i686-linux-gnu-gcc4.tar.gz", "0e631d89d8d7afac8d0c41a10ec33b69e6fdf376828c7e6fde63fe807fd94aca"),
    Linux(:i686, libc=:glibc, compiler_abi=CompilerABI(:gcc7)) => ("$bin_prefix/LLVM.v6.0.1.i686-linux-gnu-gcc7.tar.gz", "177d35b2ef323cc3e46e2f890e15e1c143965ac0201a182c32b64f3b1077d50f"),
    Linux(:i686, libc=:glibc, compiler_abi=CompilerABI(:gcc8)) => ("$bin_prefix/LLVM.v6.0.1.i686-linux-gnu-gcc8.tar.gz", "602dc54bebb49c1571f34979f09fd3d29efd2957878eab7f192201fc093897ce"),
    Windows(:i686, compiler_abi=CompilerABI(:gcc4)) => ("$bin_prefix/LLVM.v6.0.1.i686-w64-mingw32-gcc4.tar.gz", "8749ddb21111ea921c384904720ab02d1f35524d0a40b7b3cbb1c8e18c3c42a6"),
    Windows(:i686, compiler_abi=CompilerABI(:gcc7)) => ("$bin_prefix/LLVM.v6.0.1.i686-w64-mingw32-gcc7.tar.gz", "be708ffeca9a896da1e2a7fc55cdd69697606d7cdd19ebe0b5907d02abe04609"),
    Windows(:i686, compiler_abi=CompilerABI(:gcc8)) => ("$bin_prefix/LLVM.v6.0.1.i686-w64-mingw32-gcc8.tar.gz", "86ebb8926e57821431a3d2ccd6ff76f47e6a4d621ceefecf353df0598363c1bd"),
    Linux(:powerpc64le, libc=:glibc, compiler_abi=CompilerABI(:gcc4)) => ("$bin_prefix/LLVM.v6.0.1.powerpc64le-linux-gnu-gcc4.tar.gz", "bb2a00ddc00febd345ab87e64d2e071f409727148b6bf9f77129360e695de25f"),
    Linux(:powerpc64le, libc=:glibc, compiler_abi=CompilerABI(:gcc7)) => ("$bin_prefix/LLVM.v6.0.1.powerpc64le-linux-gnu-gcc7.tar.gz", "43301b92a54aabf5b21a8a00c6904ecb8e9d18d7275051a3af383e20dfc1b5cc"),
    Linux(:powerpc64le, libc=:glibc, compiler_abi=CompilerABI(:gcc8)) => ("$bin_prefix/LLVM.v6.0.1.powerpc64le-linux-gnu-gcc8.tar.gz", "6f0533b16bbdc4bfc661eb27b404f361422de42cc41c0477b53433c3b219464c"),
    MacOS(:x86_64, compiler_abi=CompilerABI(:gcc4)) => ("$bin_prefix/LLVM.v6.0.1.x86_64-apple-darwin14-gcc4.tar.gz", "55c6d47113a1d8c5b0ac155fc3381382e93fcff97fc4aa4e58dc866d232c9cd8"),
    MacOS(:x86_64, compiler_abi=CompilerABI(:gcc7)) => ("$bin_prefix/LLVM.v6.0.1.x86_64-apple-darwin14-gcc7.tar.gz", "12ea772128b9f9306188fd9933ad5c26a247e598578fffd6487d6dc4eb417ec8"),
    MacOS(:x86_64, compiler_abi=CompilerABI(:gcc8)) => ("$bin_prefix/LLVM.v6.0.1.x86_64-apple-darwin14-gcc8.tar.gz", "59ceff90627b0a7d930d1039b4746141d784f45d5c012d9de5947642107ef842"),
    Linux(:x86_64, libc=:glibc, compiler_abi=CompilerABI(:gcc4)) => ("$bin_prefix/LLVM.v6.0.1.x86_64-linux-gnu-gcc4.tar.gz", "c9ba503269fbf4ac2a29ff18e2ecfde6a2e348478270cd931939e6c193b9ae05"),
    Linux(:x86_64, libc=:glibc, compiler_abi=CompilerABI(:gcc7)) => ("$bin_prefix/LLVM.v6.0.1.x86_64-linux-gnu-gcc7.tar.gz", "6fca2fedc5ae4ead8b2fd62d863e2a556075d4d7de8b8c66cc8feeea6dc33851"),
    Linux(:x86_64, libc=:glibc, compiler_abi=CompilerABI(:gcc8)) => ("$bin_prefix/LLVM.v6.0.1.x86_64-linux-gnu-gcc8.tar.gz", "10a5caffb642b181f5064f6b123d293234fcea86f218e71bbf03482a6f78406b"),
    Linux(:x86_64, libc=:musl) => ("$bin_prefix/tblgen.v6.0.1.x86_64-linux-musl.tar.gz", "ddb48961af831b13226f3363c45b5d01f8d9d0df8d59bafc500b584cd97f0fe8"),
    Linux(:x86_64, libc=:musl, compiler_abi=CompilerABI(:gcc4)) => ("$bin_prefix/LLVM.v6.0.1.x86_64-linux-musl-gcc4.tar.gz", "78a68bd503cda3ae3441eb7c52e1132351c8f329d0394f718055bebee80e9b67"),
    Linux(:x86_64, libc=:musl, compiler_abi=CompilerABI(:gcc7)) => ("$bin_prefix/LLVM.v6.0.1.x86_64-linux-musl-gcc7.tar.gz", "bc760df43c69d7b337f0cb02f5a69b3ce57e13403f92625772fee13f32b3dbd4"),
    Linux(:x86_64, libc=:musl, compiler_abi=CompilerABI(:gcc8)) => ("$bin_prefix/LLVM.v6.0.1.x86_64-linux-musl-gcc8.tar.gz", "5554db7556001124fc450f09f51dec472e8cd1499e688876c0a23cd3c62a87d9"),
    FreeBSD(:x86_64, compiler_abi=CompilerABI(:gcc4)) => ("$bin_prefix/LLVM.v6.0.1.x86_64-unknown-freebsd11.1-gcc4.tar.gz", "8bbc4db3cf4435bf3ea5e5558659ee8796840d1c1e5b738ada17b273a0349047"),
    FreeBSD(:x86_64, compiler_abi=CompilerABI(:gcc7)) => ("$bin_prefix/LLVM.v6.0.1.x86_64-unknown-freebsd11.1-gcc7.tar.gz", "91bffae12a1727b4218c8c698de6bc7ef2ee6270224046d3a69aa284708f15f2"),
    FreeBSD(:x86_64, compiler_abi=CompilerABI(:gcc8)) => ("$bin_prefix/LLVM.v6.0.1.x86_64-unknown-freebsd11.1-gcc8.tar.gz", "313171f8959e78ecb197163b19c9e0b97b5fec48062d3c5ded9b16ca9c303d56"),
    Windows(:x86_64, compiler_abi=CompilerABI(:gcc4)) => ("$bin_prefix/LLVM.v6.0.1.x86_64-w64-mingw32-gcc4.tar.gz", "661bd6e7ac6a977d00822053c28b0d903973f27028ed8685c843aa7cdbfc01e4"),
    Windows(:x86_64, compiler_abi=CompilerABI(:gcc7)) => ("$bin_prefix/LLVM.v6.0.1.x86_64-w64-mingw32-gcc7.tar.gz", "998a1932884121f15d7d5b2e75fb977695a4d448dd3888c10a18aafa83faf8c9"),
    Windows(:x86_64, compiler_abi=CompilerABI(:gcc8)) => ("$bin_prefix/LLVM.v6.0.1.x86_64-w64-mingw32-gcc8.tar.gz", "9218695814f413b9423228d6fbef2eeba5812a1c99023f9c500bdf6fa55ce972"),
)

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