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

# Download binaries from hosted location
bin_prefix = "https://github.com/jpsamaroo/x264Builder/releases/download/v2018.2.12-noyasm"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    Linux(:aarch64, :glibc) => ("$bin_prefix/x264Builder.v2018.2.12-pre-noyasm.aarch64-linux-gnu.tar.gz", "2a0ab4f4b1ae142abf2b8cadb3acb1c176e1d29921ee52749f01c46e6c5051d8"),
    Linux(:aarch64, :musl) => ("$bin_prefix/x264Builder.v2018.2.12-pre-noyasm.aarch64-linux-musl.tar.gz", "85e9a923faa455dedb6b4d06829799f49b6942f8eb8c8d5957c79b9e06cc33ae"),
    Linux(:armv7l, :glibc, :eabihf) => ("$bin_prefix/x264Builder.v2018.2.12-pre-noyasm.arm-linux-gnueabihf.tar.gz", "e1173cb209ce8e9b0f4c9f623044b0a9fec03ec76d43e5e39e0824ef3070eb66"),
    Linux(:armv7l, :musl, :eabihf) => ("$bin_prefix/x264Builder.v2018.2.12-pre-noyasm.arm-linux-musleabihf.tar.gz", "610cf15612ca054537cc8a0845f55304562163662c75eb8a5c57d41c4f44196c"),
    Linux(:i686, :glibc) => ("$bin_prefix/x264Builder.v2018.2.12-pre-noyasm.i686-linux-gnu.tar.gz", "e4ac5eba78397f85e3968664651d897f68c4ba9f0a84677569158eb652295ca1"),
    Linux(:i686, :musl) => ("$bin_prefix/x264Builder.v2018.2.12-pre-noyasm.i686-linux-musl.tar.gz", "a731e80af58cf3f3203a598331b41bb1dc8f4d0f16696609ff29bf85f24426d4"),
    Windows(:i686) => ("$bin_prefix/x264Builder.v2018.2.12-pre-noyasm.i686-w64-mingw32.tar.gz", "f449c7e177a1a45be63417487996166131d4d857b0d26e4ab0527b19a6ca3ff9"),
    Linux(:powerpc64le, :glibc) => ("$bin_prefix/x264Builder.v2018.2.12-pre-noyasm.powerpc64le-linux-gnu.tar.gz", "d95e0eaa336b0977071d4a5212def6c354f02219c2c699c9917c47b238497f78"),
    MacOS(:x86_64) => ("$bin_prefix/x264Builder.v2018.2.12-pre-noyasm.x86_64-apple-darwin14.tar.gz", "c51834f3bc6f95601a4e8434b24d809f61683133feccaf61670e6974c7c4ed82"),
    Linux(:x86_64, :glibc) => ("$bin_prefix/x264Builder.v2018.2.12-pre-noyasm.x86_64-linux-gnu.tar.gz", "65daed583973ae54787bf528f48ca6ce23375aee19a428a6045d6ea7436b1f3a"),
    Linux(:x86_64, :musl) => ("$bin_prefix/x264Builder.v2018.2.12-pre-noyasm.x86_64-linux-musl.tar.gz", "99c67163be9810476f39478ef3b64d82bf5d237592557eeac3690ab8aeb3a692"),
    FreeBSD(:x86_64) => ("$bin_prefix/x264Builder.v2018.2.12-pre-noyasm.x86_64-unknown-freebsd11.1.tar.gz", "a437b7ada02d263470da92a273cdc34e447bfa56cae7d79764c9b6120692e6dd"),
    Windows(:x86_64) => ("$bin_prefix/x264Builder.v2018.2.12-pre-noyasm.x86_64-w64-mingw32.tar.gz", "70b2e53a82cca36e4aa9774a2be39443b690696da6b15534c49338bd78928972"),
)

# 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, ignore_platform = true)
    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_codec.jl"), products)
