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 = [
    ExecutableProduct(prefix, "thrift", :thrift),
]

# Download binaries from hosted location
bin_prefix = "https://github.com/tanmaykm/JuliaThriftBuilder/releases/download/julia1.0-thrift0.11.0"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    Linux(:aarch64, libc=:glibc) => ("$bin_prefix/JuliaThriftBuilder.v0.2.0.aarch64-linux-gnu.tar.gz", "562d24f55dd289e4e23817a11d2aaffd2b8fc5cfa721c94654e8904558064ecf"),
    Linux(:aarch64, libc=:musl) => ("$bin_prefix/JuliaThriftBuilder.v0.2.0.aarch64-linux-musl.tar.gz", "bb8af72515ed83dc014a1a042d3854cf7474d487f7040c7be893e2e15d56bf06"),
    Linux(:armv7l, libc=:glibc, call_abi=:eabihf) => ("$bin_prefix/JuliaThriftBuilder.v0.2.0.arm-linux-gnueabihf.tar.gz", "eb6d31434f540b2bb9ce2b21313af1461fa622c29c4084c7afb0fdfa6248e0ef"),
    Linux(:armv7l, libc=:musl, call_abi=:eabihf) => ("$bin_prefix/JuliaThriftBuilder.v0.2.0.arm-linux-musleabihf.tar.gz", "d3649e99233bc850dd2c8c7cfb3e000bc751a62ca7a053789fbf169c66d76d13"),
    Linux(:i686, libc=:glibc) => ("$bin_prefix/JuliaThriftBuilder.v0.2.0.i686-linux-gnu.tar.gz", "f56ae0cf7ab4fd0b8b079b179db48425298b6ae6a9c76f68382bc6d7503cde18"),
    Linux(:i686, libc=:musl) => ("$bin_prefix/JuliaThriftBuilder.v0.2.0.i686-linux-musl.tar.gz", "0a28f98554677ee045fcbbce414302b1fe8d8ffff9ac9baf481827b72f484d86"),
    Linux(:powerpc64le, libc=:glibc) => ("$bin_prefix/JuliaThriftBuilder.v0.2.0.powerpc64le-linux-gnu.tar.gz", "0598b6a1dbaf8ea3a6ae09b193d8e9ee64ba2d7963007d35950cc2ff3e040983"),
    MacOS(:x86_64) => ("$bin_prefix/JuliaThriftBuilder.v0.2.0.x86_64-apple-darwin14.tar.gz", "47bd16f2ec8e8d68c97461404fb684baf30aca4441bd2627ec4a43c2c284f01e"),
    Linux(:x86_64, libc=:glibc) => ("$bin_prefix/JuliaThriftBuilder.v0.2.0.x86_64-linux-gnu.tar.gz", "a57ab1119e0dfa28a2831e3f779c5d3cc34a65d819e3a6b1f1d0662bd34d4da5"),
    Linux(:x86_64, libc=:musl) => ("$bin_prefix/JuliaThriftBuilder.v0.2.0.x86_64-linux-musl.tar.gz", "db2e9638092bc1be7c8249028514e9464927c407b2841082d1acfc4b7ef6e446"),
    FreeBSD(:x86_64) => ("$bin_prefix/JuliaThriftBuilder.v0.2.0.x86_64-unknown-freebsd11.1.tar.gz", "2c250ea91ee70641b0cb0c0205b35219e33b3b3762d130f4f25a1328ed9d81ad"),
)

if Sys.iswindows()
    @info "No pre-built Julia Thrift compiler found for your platform $(triplet(platform_key_abi())). Not required unless you want to compile new Thrift IDLs. Follow package instructions to build on your own if needed."
else
    # 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)
end
