using BinaryProvider # requires BinaryProvider 0.3.0 or later

# Example taken from
# https://github.com/JuliaIO/ImageMagick.jl/blob/master/deps/build.jl
dependencies = [
    "build_SQLite.v3.28.0.jl",
]

for elem in dependencies
    # it's a bit faster to run the build in an anonymous module instead of
    # starting a new julia process
    m = Module(:__anon__)
    Core.include(m, (joinpath(@__DIR__, elem)))
end

# 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, ["libproj"], :libproj),
    ExecutableProduct(prefix, "cct", :cct_path),
    ExecutableProduct(prefix, "cs2cs", :cs2cs_path),
    ExecutableProduct(prefix, "geod", :geod_path),
    ExecutableProduct(prefix, "gie", :gie_path),
    ExecutableProduct(prefix, "proj", :proj_path),
    ExecutableProduct(prefix, "projinfo", :projinfo_path),
    FileProduct(prefix, "share/proj/CH", :ch_path),
    FileProduct(prefix, "share/proj/GL27", :gl27_path),
    FileProduct(prefix, "share/proj/ITRF2000", :itrf2000_path),
    FileProduct(prefix, "share/proj/ITRF2008", :itrf2008_path),
    FileProduct(prefix, "share/proj/ITRF2014", :itrf2014_path),
    FileProduct(prefix, "share/proj/nad.lst", :nad_lst_path),
    FileProduct(prefix, "share/proj/nad27", :nad27_path),
    FileProduct(prefix, "share/proj/nad83", :nad83_path),
    FileProduct(prefix, "share/proj/null", :null_path),
    FileProduct(prefix, "share/proj/other.extra", :other_extra_path),
    FileProduct(prefix, "share/proj/proj.db", :proj_db_path),
    FileProduct(prefix, "share/proj/world", :world_path),
    FileProduct(prefix, "share/proj/alaska", :alaska_path),
    FileProduct(prefix, "share/proj/conus", :conus_path),
    FileProduct(prefix, "share/proj/egm96_15.gtx", :egm96_15_path),
    FileProduct(prefix, "share/proj/ntv1_can.dat", :ntv1_can_path),
]

# Download binaries from hosted location
bin_prefix = "https://github.com/JuliaGeo/PROJBuilder/releases/download/v6.1.0-1"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    Linux(:aarch64, libc=:glibc) => ("$bin_prefix/PROJ.v6.1.0.aarch64-linux-gnu.tar.gz", "424e8a5e50ad0ea3458c4fa4245ee67570bb641887a3b7cab4c9fa0809bd0f22"),
    Linux(:aarch64, libc=:musl) => ("$bin_prefix/PROJ.v6.1.0.aarch64-linux-musl.tar.gz", "0f4ca2cb518cb6d40a30ca47ac40447bfb6dd7496cc05944e704510d5ddcc9ef"),
    Linux(:armv7l, libc=:glibc, call_abi=:eabihf) => ("$bin_prefix/PROJ.v6.1.0.arm-linux-gnueabihf.tar.gz", "7d92b09644616e8ae354312beb8e33a751017806f38d5a1c98aee8df598c8304"),
    Linux(:armv7l, libc=:musl, call_abi=:eabihf) => ("$bin_prefix/PROJ.v6.1.0.arm-linux-musleabihf.tar.gz", "867ceb4c28f5fd13761c01f15b6def155a45e181f5fa974827b8aca041284c37"),
    Linux(:i686, libc=:glibc) => ("$bin_prefix/PROJ.v6.1.0.i686-linux-gnu.tar.gz", "aeb5e670b395e2f60e5f4f35c2a95c997bdbe1e4c4520e7e76dc10f2cc1072d3"),
    Linux(:i686, libc=:musl) => ("$bin_prefix/PROJ.v6.1.0.i686-linux-musl.tar.gz", "5c8f003938f5ac0785afd41106f7b4e0d2500a1fa2ddf7369887498fd47a3cad"),
    # removed compiler_abi as suggested in https://github.com/JuliaPackaging/BinaryBuilder.jl/issues/407#issuecomment-473688254
    # such that GCC4 platforms will also pick up this GCC7 build, ref https://github.com/JuliaPackaging/BinaryBuilder.jl/issues/407
    Windows(:i686) => ("$bin_prefix/PROJ.v6.1.0.i686-w64-mingw32-gcc7.tar.gz", "9e1a042a74861263295a8be06630a7554bbc3de944bd73ec6bd15b3f00c107b9"),
    Linux(:powerpc64le, libc=:glibc) => ("$bin_prefix/PROJ.v6.1.0.powerpc64le-linux-gnu.tar.gz", "912998fa0f459bf43ace29eca6efe30997136cb0b21cb84a3e42e62f9e2ac9eb"),
    MacOS(:x86_64) => ("$bin_prefix/PROJ.v6.1.0.x86_64-apple-darwin14.tar.gz", "eb3252074d7513d203ce9b7d86d9370c5f6e2f183db5eb060418545147a07488"),
    Linux(:x86_64, libc=:glibc) => ("$bin_prefix/PROJ.v6.1.0.x86_64-linux-gnu.tar.gz", "ecd78e16e613bd888e7b74371688f52da62140795a7e23088e8bc8009a53c90d"),
    Linux(:x86_64, libc=:musl) => ("$bin_prefix/PROJ.v6.1.0.x86_64-linux-musl.tar.gz", "d725d227c8636a3a4ea389f5ae385f2fdf31d4cf966dc2cef077feb047f75bfa"),
    FreeBSD(:x86_64) => ("$bin_prefix/PROJ.v6.1.0.x86_64-unknown-freebsd11.1.tar.gz", "146a3f7d91115d45fe00d79d53e701c27c5e7c241ba8a691b709274c39fdb9ab"),
    # removed compiler_abi as suggested in https://github.com/JuliaPackaging/BinaryBuilder.jl/issues/407#issuecomment-473688254
    # such that GCC4 platforms will also pick up this GCC7 build, ref https://github.com/JuliaPackaging/BinaryBuilder.jl/issues/407
    Windows(:x86_64) => ("$bin_prefix/PROJ.v6.1.0.x86_64-w64-mingw32-gcc7.tar.gz", "92b330c66647ac63870865546d717e095c34bb09dd11b81eb2ac0915aa49bc95"),
)

# Install unsatisfied or updated dependencies:
unsatisfied = any(!satisfied(p; verbose=verbose) for p in products)
dl_info = choose_download(download_info, platform_key_abi())
if dl_info === nothing && unsatisfied
    # If we don't have a 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 (\"$(Sys.MACHINE)\", parsed as \"$(triplet(platform_key_abi()))\") is not supported by this package!")
end

# If we have a download, and we are unsatisfied (or the version we're
# trying to install is not itself installed) then load it up!
if unsatisfied || !isinstalled(dl_info...; prefix=prefix)
    # Download and install binaries
    install(dl_info...; prefix=prefix, force=true, verbose=verbose)
end

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