(* -*- tuareg -*- *)

open Printf

let with_file path ~f =
  let oc = open_out_bin path in
  let pr fmt = fprintf oc (fmt ^^ "\n") in
  f pr;
  close_out oc

let () =
  let ver = Scanf.sscanf Sys.argv.(1) "%u.%u" (fun a b -> a, b) in
  with_file Sys.argv.(2) ~f:(fun pr -> (* location_helper *)
    if ver < (4, 06) then
      pr {|
let deprecated loc s =
  Ocaml_common.Location.prerr_warning loc (Ocaml_common.Warnings.Deprecated s)
|};
    if ver < (4, 08) then begin
      pr {|
let print_error ppf loc = Ocaml_common.Location.print_error ppf loc
let error_of_printer ~loc x y = Ocaml_common.Location.error_of_printer loc x y
|};
    end else begin
      pr {|
let print_error ppf loc = Format.fprintf ppf "%%aError:" Ocaml_common.Location.print_loc loc
let error_of_printer ~loc x y = Ocaml_common.Location.error_of_printer ~loc x y
|};
    end);
  with_file Sys.argv.(3) ~f:(fun pr -> (* clflags_helper *)
    if ver < (4, 08) then begin
      pr {|
let is_unsafe () = !Ocaml_common.Clflags.fast[@ocaml.warning "-3"]
|};
    end else begin
      pr {|
let is_unsafe () = !Ocaml_common.Clflags.unsafe[@ocaml.warning "-3"]
|};
    end);
  with_file Sys.argv.(4) ~f:(fun pr -> (* misc_helper *)
    if ver < (4, 10) then begin
      pr {|
let may = Import.Misc.may
|};
    end else begin
      pr {|
let may f = function Some v -> f v | None -> ()
|};
    end)
