(* -*- tuareg -*- *)
(* This is a workaround to avoid compilation of the modules
   under this directory, when graphics library does not exist.
   
   See https://github.com/ocaml/dune/issues/563 .
*)
#require "unix"

let jbuild = {|(jbuild_version 1)

(library
 ((name camlimages_graphics)
  (public_name camlimages.graphics)
  (optional)
  (wrapped false)
  (libraries ( camlimages.core ))
  (virtual_deps (graphics))
  ))
|}


let graphics_exists =
  let dest = Filename.temp_file "test" ".cma" in
  let res = match Sys.command (Printf.sprintf "ocamlfind ocamlc -package %s -o %s -linkpkg" "graphics" dest) with
  | 0 -> true
  | _ -> false
  | exception _ -> false
  in
  (try Sys.remove dest with _ -> ());
  res

let () =
  let jbuild = if graphics_exists then jbuild else {|(jbuild_version 1)|} in
  Jbuild_plugin.V1.send jbuild
