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

# Install BinaryBuilder dependencies
dependencies = [
    "https://github.com/bicycle1885/ZlibBuilder/releases/download/v1.0.2/build_Zlib.v1.2.11.jl",
    "https://github.com/JuliaWeb/MbedTLSBuilder/releases/download/v0.11/build_MbedTLS.v1.0.0.jl"
]

for url in dependencies
    build_file = joinpath(@__DIR__, basename(url))
    if !isfile(build_file)
        download(url, build_file)
    end
end

# Execute the build scripts for the dependencies in an isolated module to avoid overwriting
# any variables/constants here
for url in dependencies
    build_file = joinpath(@__DIR__, basename(url))
    m = @eval module $(gensym()); include($build_file); end
    append!(products, m.products)
end

# Download binaries from hosted location
bin_prefix = "https://github.com/JuliaWeb/LibCURLBuilder/releases/download/v0.3.1"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    Linux(:aarch64, :glibc) => ("$bin_prefix/LibCURL.v7.61.0.aarch64-linux-gnu.tar.gz", "69e02dcc8f2290551593ed68eac37615daf1d1bf185b940c105a93f5d95d6546"),
    Linux(:aarch64, :musl) => ("$bin_prefix/LibCURL.v7.61.0.aarch64-linux-musl.tar.gz", "eb562b8ebd0ef7ab3ddb5726049c12fd7666a40e93d76ef66bb53bbd5ab3b051"),
    Linux(:armv7l, :glibc, :eabihf) => ("$bin_prefix/LibCURL.v7.61.0.arm-linux-gnueabihf.tar.gz", "4a08bab5a6d85ee65bf59c05d1fdf3617552a1c0989f3eac4d535ac23a306f62"),
    Linux(:armv7l, :musl, :eabihf) => ("$bin_prefix/LibCURL.v7.61.0.arm-linux-musleabihf.tar.gz", "154285daba1e01de10e38742fcf562b704df0126a369c1b78f829db06f08ab77"),
    Linux(:i686, :glibc) => ("$bin_prefix/LibCURL.v7.61.0.i686-linux-gnu.tar.gz", "7573fb35ff104c9badfa6f928e1c30dfe3c01eda72bd3a28773a87da42297249"),
    Linux(:i686, :musl) => ("$bin_prefix/LibCURL.v7.61.0.i686-linux-musl.tar.gz", "8c5cbadbbe39f9c6fa192edac58cdb4af8a29f1e260a2466a25fd5a7e87ae786"),
    Windows(:i686) => ("$bin_prefix/LibCURL.v7.61.0.i686-w64-mingw32.tar.gz", "fe41ca4f12ef8e720047a079076848b335579a006f6ea7e4a30b758484dcbb4d"),
    Linux(:powerpc64le, :glibc) => ("$bin_prefix/LibCURL.v7.61.0.powerpc64le-linux-gnu.tar.gz", "3288401b57b46e6fc2f307ce6aa1c93b4d449764155ff33fcdbaeeed58b218c9"),
    MacOS(:x86_64) => ("$bin_prefix/LibCURL.v7.61.0.x86_64-apple-darwin14.tar.gz", "c1169390ff0dad5bf104a0645e8638c636b6f694d3a5ce10ad31384b0eeb2c3b"),
    Linux(:x86_64, :glibc) => ("$bin_prefix/LibCURL.v7.61.0.x86_64-linux-gnu.tar.gz", "e1fb2a82877b77363630b5fde1097a26cc84ec7ac497f80908c50df21d3a5e2d"),
    Linux(:x86_64, :musl) => ("$bin_prefix/LibCURL.v7.61.0.x86_64-linux-musl.tar.gz", "7f45b7f955f80b68ae9430c7ae081626f916515b6ccaf25550655149b43c4382"),
    FreeBSD(:x86_64) => ("$bin_prefix/LibCURL.v7.61.0.x86_64-unknown-freebsd11.1.tar.gz", "c3945f2871f621d825203da419e274e490fab0b6e7ed0b1fb34908a173681a42"),
    Windows(:x86_64) => ("$bin_prefix/LibCURL.v7.61.0.x86_64-w64-mingw32.tar.gz", "6a3fb40bea8f2c7987ef46af8a423abfdf60287f69397f0f1df8973ea66e17fa"),
)

# 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, verbose=verbose)
