using BinaryProvider # requires BinaryProvider 0.3.0 or later
include("compile.jl")

# env var to force compilation from source, for testing purposes
const forcecompile = get(ENV, "FORCE_COMPILE_NLOPT", "no") == "yes"

# Parse some basic command-line arguments
const verbose = ("--verbose" in ARGS) || forcecompile
const prefix = Prefix(get([a for a in ARGS if a != "--verbose"], 1, joinpath(@__DIR__, "usr")))
products = [
    LibraryProduct(prefix, String["libnlopt_cxx"], :libnlopt),
]
verbose && forcecompile && @info("Forcing compilation from source.")

# Download binaries from hosted location
bin_prefix = "https://github.com/stevengj/NLoptBuilder/releases/download/v2.5.0"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    Linux(:aarch64, :glibc) => ("$bin_prefix/NLopt.v2.5.0.aarch64-linux-gnu.tar.gz", "2347e5e240b9b95cfe6717d356e1f1948758e983574bc97378bee0fc90e21936"),
    Linux(:aarch64, :musl) => ("$bin_prefix/NLopt.v2.5.0.aarch64-linux-musl.tar.gz", "0bf5cf5676a8df605cae199cd2a07b541f54a2c6c78f98109a863811d77cc268"),
    Linux(:armv7l, :glibc, :eabihf) => ("$bin_prefix/NLopt.v2.5.0.arm-linux-gnueabihf.tar.gz", "2436981433c7bd7bab8853d26341a01c82a553528232ae950bf390b399678f6f"),
    Linux(:armv7l, :musl, :eabihf) => ("$bin_prefix/NLopt.v2.5.0.arm-linux-musleabihf.tar.gz", "65cba09664827d6f7baf2324594b1e652912ef38dc0cb15adab2f99cfde4f1ac"),
    Linux(:i686, :glibc) => ("$bin_prefix/NLopt.v2.5.0.i686-linux-gnu.tar.gz", "61602ad8e8924badc2836e5be8279b9f85afb8608bfbb6c1bebe13eef755c2f4"),
    Linux(:i686, :musl) => ("$bin_prefix/NLopt.v2.5.0.i686-linux-musl.tar.gz", "cebfc02353c3cc16778789d4654dff193d186cd3c453b42712cc888ca387a0f6"),
    Windows(:i686) => ("$bin_prefix/NLopt.v2.5.0.i686-w64-mingw32.tar.gz", "b1d3f963866f383195bf9a0cf1c280e4817f8d5238911c830b8a6cdda949e817"),
    Linux(:powerpc64le, :glibc) => ("$bin_prefix/NLopt.v2.5.0.powerpc64le-linux-gnu.tar.gz", "a3515af1d464d420a0e78e61dabf6735a1320acfc898f8a5284093c7bb5fa57d"),
    MacOS(:x86_64) => ("$bin_prefix/NLopt.v2.5.0.x86_64-apple-darwin14.tar.gz", "623b8be18dba99021befbebaa50f73f70b9949849e8e916253c657038f014f14"),
    Linux(:x86_64, :glibc) => ("$bin_prefix/NLopt.v2.5.0.x86_64-linux-gnu.tar.gz", "ea0ee00881fa1517f2640a1e05ff9ea88757c1c148fde1c42dd124791684e0b4"),
    Linux(:x86_64, :musl) => ("$bin_prefix/NLopt.v2.5.0.x86_64-linux-musl.tar.gz", "d20ab1cfb9b5561ffaa70da919df4e409ca7ed7fef8e4b049d3ef6e24344a8a8"),
    FreeBSD(:x86_64) => ("$bin_prefix/NLopt.v2.5.0.x86_64-unknown-freebsd11.1.tar.gz", "078e9a08283d471473da5c6f465d8f907f068da8ef15ad36305a231cf776a309"),
    Windows(:x86_64) => ("$bin_prefix/NLopt.v2.5.0.x86_64-w64-mingw32.tar.gz", "776ea5d6777242f86355de2fdd6d5be8203e60b38ea5794c53f45c36551fde46"),
)

# source code tarball and hash for fallback compilation
source_url = "https://github.com/stevengj/nlopt/archive/v2.5.0.tar.gz"
source_hash = "c6dd7a5701fff8ad5ebb45a3dc8e757e61d52658de3918e38bab233e7fd3b4ae"

# Install unsatisfied or updated dependencies:
unsatisfied = any(!satisfied(p; verbose=verbose) for p in products)
if haskey(download_info, platform_key()) && !forcecompile
    url, tarball_hash = download_info[platform_key()]
    if !isinstalled(url, tarball_hash; prefix=prefix)
        # Download and install binaries
        install(url, tarball_hash; prefix=prefix, force=true, verbose=verbose)

        # check again whether the dependency is satisfied, which
        # may not be true if dlopen fails due to a libc++ incompatibility (#50)
        unsatisfied = any(!satisfied(p; verbose=verbose) for p in products)
    end
end

if unsatisfied || forcecompile
    # Fall back to building from source, giving the library a different name
    # so that it is not overwritten by BinaryBuilder downloads or vice-versa.
    libname = "libnlopt_from_source"
    products = [ LibraryProduct(prefix, [libname], :libnlopt) ]
    source_path = joinpath(prefix, "downloads", "src.tar.gz")
    if !isfile(source_path) || !verify(source_path, source_hash; verbose=verbose) || !satisfied(products[1]; verbose=verbose)
        compile(libname, source_url, source_hash, prefix=prefix, verbose=verbose)
    end
end

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