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, "hybrid-Lambda", :hybridlambda),
]

# Download binaries from hosted location
bin_prefix = "https://github.com/cecileane/HybridLambdaBuilder/releases/download/v0.6.2-beta"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    Linux(:aarch64, libc=:glibc) => ("$bin_prefix/HybridLambda.v0.6.2-beta.aarch64-linux-gnu.tar.gz", "cb9723304dfc7a4d67ba305d6a7c3041c665c8db53b170fde320d9533f7cb367"),
    Linux(:aarch64, libc=:musl) => ("$bin_prefix/HybridLambda.v0.6.2-beta.aarch64-linux-musl.tar.gz", "6291d48b2e6b569dbc2bc011f5d768eb44e5611aa1943faae720cb1ce281b5d3"),
    Linux(:armv7l, libc=:glibc, call_abi=:eabihf) => ("$bin_prefix/HybridLambda.v0.6.2-beta.arm-linux-gnueabihf.tar.gz", "807aafa70fb1d022b0b796abc9e2a89a030ef79357cc3880e1a8dcae3823a667"),
    Linux(:armv7l, libc=:musl, call_abi=:eabihf) => ("$bin_prefix/HybridLambda.v0.6.2-beta.arm-linux-musleabihf.tar.gz", "381f40c0d6892bd7ceafdf2cce0d35b06617388ff9d090dcc6a7fa93bc2bc006"),
    Linux(:i686, libc=:glibc) => ("$bin_prefix/HybridLambda.v0.6.2-beta.i686-linux-gnu.tar.gz", "c49e49736aa7cc5b82f9d40fdffd7effba2f2aaaf77a8090dee5c3c064c0895d"),
    Linux(:powerpc64le, libc=:glibc) => ("$bin_prefix/HybridLambda.v0.6.2-beta.powerpc64le-linux-gnu.tar.gz", "8643c0708d1cce0080984e08168a68f1361a2ae1876cf95da8ab39a0cd1a2423"),
    MacOS(:x86_64) => ("$bin_prefix/HybridLambda.v0.6.2-beta.x86_64-apple-darwin14.tar.gz", "4ed16d6775ddcef45e63c1a769d9bdf731fc9eeeb5d44572a486548f53c27f71"),
    Linux(:x86_64, libc=:glibc) => ("$bin_prefix/HybridLambda.v0.6.2-beta.x86_64-linux-gnu.tar.gz", "cf27dbf2ced3b6d167112a8ca6c7c2aa818cc622494bfc5bca8ede03c5da6ea8"),
)

# 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.
    @warn "Your platform (\"$(Sys.MACHINE)\", parsed as \"$(triplet(platform_key_abi()))\") is not supported by this package!"
end

if dl_info !== nothing
    # 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
end

if dl_info !== nothing || !unsatisfied
    # Write out deps.jl that will contain mappings for our products:
    #   import Libdl
    #   const hybridlambda = joinpath(dirname(@__FILE__), "usr/bin/hybrid-Lambda")
    #   definition of function check_deps()
    write_deps_file(joinpath(@__DIR__, "deps.jl"), products, verbose=verbose)
end
