using BinaryProvider

JLCXX_DIR = get(ENV, "JLCXX_DIR", "")
if(isempty(JLCXX_DIR))
    JLCXX_DIR =  get(ENV, "JlCxx_DIR", "")
end

const verbose = "--verbose" in ARGS
const prefix = Prefix(JLCXX_DIR == "" ? get([a for a in ARGS if a != "--verbose"], 1, joinpath(@__DIR__, "usr")) : JLCXX_DIR)

products = Product[
    LibraryProduct(prefix, "libcxxwrap_julia", :libcxxwrap_julia),
    LibraryProduct(prefix, "libcxxwrap_julia_stl", :libcxxwrap_julia_stl)
]

supported = true

# Download binaries from hosted location
bin_prefix = "https://github.com/JuliaBinaryWrappers/libcxxwrap_julia_jll.jl/releases/download/libcxxwrap_julia-v0.6.6%2B0/"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    Windows(:i686, compiler_abi=CompilerABI(:gcc7, :cxx11)) => ("$bin_prefix/libcxxwrap_julia.v0.6.6.i686-w64-mingw32-cxx11.tar.gz", "cc5503ad6bad6e7dc3e17ab6e95f08a807d3959dcb634fb5e33be99162fa3245"),
    MacOS(:x86_64) => ("$bin_prefix/libcxxwrap_julia.v0.6.6.x86_64-apple-darwin14-cxx11.tar.gz", "d62b2065e4c0dcaefc0612d7c00d6d2c1dd96601dc87dd24730df71c06c0fcc2"),
    Linux(:x86_64, libc=:glibc, compiler_abi=CompilerABI(:gcc7, :cxx11)) => ("$bin_prefix/libcxxwrap_julia.v0.6.6.x86_64-linux-gnu-cxx11.tar.gz", "bbd1e5fe3fcb9b5c9ee26091b18abe4eb38c0875a96c63a63feef0c09b66cd54"),
    Windows(:x86_64, compiler_abi=CompilerABI(:gcc7, :cxx11)) => ("$bin_prefix/libcxxwrap_julia.v0.6.6.x86_64-w64-mingw32-cxx11.tar.gz", "c764ba83621a12196e5824414d5205d62b3803674fa8be4107edcaaa10065973"),
)

# Install unsatisfied or updated dependencies:
unsatisfied = any(!satisfied(p; verbose=verbose) for p in products)
transform_platform(platform) = typeof(platform)(platform.arch;libc=platform.libc,call_abi=platform.call_abi,compiler_abi=CompilerABI(:gcc7,:cxx11))
transform_platform(platform::MacOS) = MacOS(:x86_64)
if JLCXX_DIR == ""
    platform = transform_platform(platform_key_abi())
    if haskey(download_info, platform)
        if !supported
            error("Julia version $VERSION is not supported for binary download. Please build libcxxwrap-julia from source and set the JLCXX_DIR environment variable to the build dir or installation prefix.")
        end
        url, tarball_hash = download_info[platform]
        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)) is not supported by this package!")
    end
else
    if unsatisfied
        error("The required libraries were not found in the provided JLCXX_DIR directory $JLCXX_DIR")
    end
end

write_deps_file(joinpath(@__DIR__, "deps.jl"), products)
