(* -*- tuareg -*- *)

let find_hphp_parent dir =
  let path = String.split_on_char '/' dir in
  let rec reconstruct_till_hphp path acc =
    match path with
    | [] -> acc
    | "hphp" :: path ->
      if List.mem "hphp" path
      then reconstruct_till_hphp path (acc ^ "hphp/")
      else acc
    | dir :: path ->
      let acc = acc ^ dir ^ "/" in
      reconstruct_till_hphp path acc in
  reconstruct_till_hphp path ""

let find_root =
  match Sys.getenv "CMAKE_SOURCE_DIR" with
  | t -> t^"/"
  | exception Not_found ->
    let workingdir = Sys.getcwd () in
    find_hphp_parent workingdir

let () =
  Printf.ksprintf Jbuild_plugin.V1.send "\
(library
  (name hhi)
  (wrapped false)
  (libraries
    global_config
    relative_path
    sys_utils)
  (preprocess (pps ppx_gen_hhi -- -hhi-dir \"%s/hphp/hack/hhi\")))
" find_root
