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, String["libtestu01"], :libtestu01),
    LibraryProduct(prefix, String["libprobdist"], :libprobdist),
    LibraryProduct(prefix, String["libtestu01extractors"], :libtestu01extractors),
]

# Download binaries from hosted location
bin_prefix = "https://github.com/andreasnoack/TestU01Builder/releases/download/v1.2.3-0"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    Linux(:aarch64, :glibc) => ("$bin_prefix/TestU01.aarch64-linux-gnu.tar.gz", "205e8212a2da9e4d2d9cc9a400ffa7766062ec2dc245a076c01e2c9e51184228"),
    Linux(:aarch64, :musl) => ("$bin_prefix/TestU01.aarch64-linux-musl.tar.gz", "842ca0cc5f5b3edc40da6d84ff12689c0554012251a99edd96892ab953cd191d"),
    Linux(:armv7l, :glibc, :eabihf) => ("$bin_prefix/TestU01.arm-linux-gnueabihf.tar.gz", "069dbc418562b385125dd05289c807443869c6dc5fa731bbb3dd7f57d302b2ca"),
    Linux(:armv7l, :musl, :eabihf) => ("$bin_prefix/TestU01.arm-linux-musleabihf.tar.gz", "2f0ff74623bcd5fbff1f2fa4833376f42a235a407da2ac1192b71a0f1eeff4f9"),
    Linux(:i686, :glibc) => ("$bin_prefix/TestU01.i686-linux-gnu.tar.gz", "616971bc49f3f7066fa77b7890f94750ba1666c10130c3e5b93b2dea9c6cf3b3"),
    Linux(:i686, :musl) => ("$bin_prefix/TestU01.i686-linux-musl.tar.gz", "db381b8e8193f90c3485e514dc5d20682b3941ddebbb0fdfb8aa6fc246866e19"),
    Windows(:i686) => ("$bin_prefix/TestU01.i686-w64-mingw32.tar.gz", "1500b9b1115cdbd07c1c28b744bf5ec3cfc815caff84dacfc816d99b93432c64"),
    Linux(:powerpc64le, :glibc) => ("$bin_prefix/TestU01.powerpc64le-linux-gnu.tar.gz", "64ba48e9fad7c26c267ed338bb47edf9a1020c854cf80f7e31a238d51689e80a"),
    MacOS(:x86_64) => ("$bin_prefix/TestU01.x86_64-apple-darwin14.tar.gz", "95b73e9dd271db69f14401474363e45ec401a34a608207276c4e03d2e4c5320b"),
    Linux(:x86_64, :glibc) => ("$bin_prefix/TestU01.x86_64-linux-gnu.tar.gz", "187f70dbc7293d47b5a9bc586eec0ebd99475205155a94045a15ded1b48ce40f"),
    Linux(:x86_64, :musl) => ("$bin_prefix/TestU01.x86_64-linux-musl.tar.gz", "dc1e796ad5dce302d61507fe896f911aa3e17148739f955e195b84fe6709271a"),
    FreeBSD(:x86_64) => ("$bin_prefix/TestU01.x86_64-unknown-freebsd11.1.tar.gz", "65d0ff7a7e5e30781aa5bcc4fd16e8b51eddec10fa2555ce0eb2b759b1333827"),
    Windows(:x86_64) => ("$bin_prefix/TestU01.x86_64-w64-mingw32.tar.gz", "8609cb84cafbb4866cb9c80c5c654a0188e986b750376c761bf153dc8fdd63d4"),
)

# Install unsatisfied or updated dependencies:
unsatisfied = any(!satisfied(p; verbose=verbose) for p in products)
if haskey(download_info, platform_key())
    url, tarball_hash = download_info[platform_key()]
    if unsatisfied || !isinstalled(url, tarball_hash; prefix=prefix)
        # Download and install binaries
        install(url, tarball_hash; prefix=prefix, force=true, verbose=verbose)
    end
elseif unsatisfied
    # If we don't have a BinaryProvider-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 $(triplet(platform_key())) is not supported by this package!")
end

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