# This build includes components from LLDBuilder and WABTBuilder

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["libLLVM"], :libLLVM),
    ExecutableProduct(prefix, "llc", :llc),
    ExecutableProduct(prefix, "lld", :lld),
    ExecutableProduct(prefix, "wast2wasm", :wast2wasm),
    ExecutableProduct(prefix, "wasm2wast", :wasm2wast),
]

# Download binaries from hosted location
bin_prefix = "https://github.com/tshort/LLDBuilder/releases/download/v6.0.0-3"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    Linux(:aarch64, :glibc) => ("$bin_prefix/lld.aarch64-linux-gnu.tar.gz", "539650c04e9acb9e78568f7753da63cdee6692ae796196eb9e409d733de721c1"),
    Linux(:armv7l, :glibc, :eabihf) => ("$bin_prefix/lld.arm-linux-gnueabihf.tar.gz", "d4ef33968c21e3987a08642c93cca020ce60745b7d99cd280cf5045006b4a6e0"),
    Linux(:i686, :glibc) => ("$bin_prefix/lld.i686-linux-gnu.tar.gz", "11d835bcdca411d457a35e3018801ed338492947f44d3af964b22f13646a5ada"),
    Windows(:i686) => ("$bin_prefix/lld.i686-w64-mingw32.tar.gz", "36f4d6cf50118238746f440d22da3fcc9e63635a10453138b5ed5688c55142bb"),
    MacOS(:x86_64) => ("$bin_prefix/lld.x86_64-apple-darwin14.tar.gz", "d34f798a2a70295a1be115ffc1d19db9f1d947492137a274461b7dee49710e29"),
    Linux(:x86_64, :glibc) => ("$bin_prefix/lld.x86_64-linux-gnu.tar.gz", "9036170580a90654740bc0058e88f5d7a8e4ed353c477beeb3301dcfee6a5d70"),
    Windows(:x86_64) => ("$bin_prefix/lld.x86_64-w64-mingw32.tar.gz", "1e3c17bde002a072013e5e14fad9597f1155456aa8b79459c5dfc3ddc5ba21b0"),
)
bin_prefix = "https://github.com/tshort/WABTBuilder/releases/download/wabt1.0.0-0"
download_info2 = Dict(
    BinaryProvider.Linux(:aarch64, :glibc, :blank_abi) => ("$bin_prefix/WABT.aarch64-linux-gnu.tar.gz", "d979e0b71799da6fe64dff2e31743ae1ce37aca4a474f6cbd6d1e258dfc69966"),
    BinaryProvider.Linux(:aarch64, :musl, :blank_abi) => ("$bin_prefix/WABT.aarch64-linux-musl.tar.gz", "447d13319b4e869f07e68ac225176e859cc748a0f8f2d977c189809b0571ae99"),
    BinaryProvider.Linux(:armv7l, :glibc, :eabihf) => ("$bin_prefix/WABT.arm-linux-gnueabihf.tar.gz", "d4fb9d6c6f63b69b352fab1a750feec51a38f6d76787af29e5887a92b2278a9f"),
    BinaryProvider.Linux(:armv7l, :musl, :eabihf) => ("$bin_prefix/WABT.arm-linux-musleabihf.tar.gz", "c6fac837327a2ac64f578b9d43163019058f45caf3181cc7307325b19e7147e7"),
    BinaryProvider.Linux(:i686, :glibc, :blank_abi) => ("$bin_prefix/WABT.i686-linux-gnu.tar.gz", "06f246047e4ac8a534d515d6deeb0f84141a5e09ad16ad238fb0312cd9559e61"),
    BinaryProvider.Linux(:i686, :musl, :blank_abi) => ("$bin_prefix/WABT.i686-linux-musl.tar.gz", "319e87b6ed1386f13f5d14e7c6cd5e1ea1cc1f5f926a43b3502578736e80a0bc"),
    BinaryProvider.Windows(:i686, :blank_libc, :blank_abi) => ("$bin_prefix/WABT.i686-w64-mingw32.tar.gz", "b10d7b7abad86da5222da5e46d0b0d868423d883820bd7b9e1bd0b01c574eb7e"),
    BinaryProvider.Linux(:powerpc64le, :glibc, :blank_abi) => ("$bin_prefix/WABT.powerpc64le-linux-gnu.tar.gz", "70e5b51b41d85e041379ea28259e12e45d129c36bb490ce5b007eda866ff13af"),
    BinaryProvider.MacOS(:x86_64, :blank_libc, :blank_abi) => ("$bin_prefix/WABT.x86_64-apple-darwin14.tar.gz", "9f437ff428c777f24687f2408f5ca5faea18da8ef0955637164b691f0e44be5f"),
    BinaryProvider.Linux(:x86_64, :glibc, :blank_abi) => ("$bin_prefix/WABT.x86_64-linux-gnu.tar.gz", "ee24ce9f5f9b732fcba5e8ab3311a318579846ebd3f5961a0c0812c0432a5f7a"),
    BinaryProvider.Linux(:x86_64, :musl, :blank_abi) => ("$bin_prefix/WABT.x86_64-linux-musl.tar.gz", "0d9ef975f70abc997d98785b067940bcf7d483b2705298513adcba844c6f63aa"),
    BinaryProvider.Windows(:x86_64, :blank_libc, :blank_abi) => ("$bin_prefix/WABT.x86_64-w64-mingw32.tar.gz", "0bc40a171839d4f73f85bdfdc526f8e5f41262b58c65094ad499466a0f313cd8"),
)


# 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
    url, tarball_hash = download_info2[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)
