# Copied from https://github.com/gridap/GridapGmsh.jl/blob/master/deps/build.jl
# to use the new officially registered Gmsh JLL

gmsh_root = nothing

if haskey(ENV,"GMSHROOT") && !isempty(ENV["GMSHROOT"])
  gmsh_root = ENV["GMSHROOT"]
  @info """
  Using the gmsh installation found via the GMSHROOT environment variable.
  GMSHROOT=$gmsh_root"""
else
  using gmsh_jll
  GMSH_FOUND = gmsh_jll.is_available()
  if GMSH_FOUND
    gmsh_root = gmsh_jll.artifact_dir
    @info """
    Using the gmsh installation installed via BinaryBuilder."""
  end
end

GMSH_FOUND = false
if gmsh_root !== nothing
  gmsh_bin = (Sys.iswindows() ? joinpath(gmsh_root,"bin","gmsh.exe") :
              joinpath(gmsh_root,"bin","gmsh"))
  if isfile(gmsh_bin)
    GMSH_FOUND = true
    @info "gmsh binary found in $gmsh_bin"
  else
    @warn """
    gmsh binary not found in $gmsh_bin
    Make sure that GMSHROOT points to a correct gmsh installation"""
  end
  gmsh_jl = joinpath(gmsh_root,"lib","gmsh.jl")
  if isfile(gmsh_jl)
    GMSH_FOUND = true
    @info "Using the gmsh Julia API found in $gmsh_jl"
  else
    @warn """
    gmsh Julia API not found in $gmsh_jl
    Make sure that you have installed the full gmsh SDK"""
  end
end

open("deps.jl","w") do f
  println(f, "# This file is automatically generated")
  println(f, "# Do not edit")
  println(f)
  println(f, :(const GMSH_FOUND = $GMSH_FOUND))
  println(f, :(const gmsh_jl    = $gmsh_jl))
end