(* -*- tuareg -*- *)
open StdLabels
open Jbuild_plugin.V1

let git_version =
  let placeholder = "5.9.1" in
  match run_and_read_lines "git describe --always --dirty" with
  | exception _ -> ""
  | version :: _ ->
    if "%%" ^ "VERSION" ^ "%%" = placeholder
    then version
    else placeholder
  | [] -> ""

let dyn =
  Printf.sprintf
    {|
(rule
  (target GIT-VERSION)
  (action (with-stdout-to %%{target} (echo "%s"))))
|}
    git_version

let () = send dyn
