; opam must always be invoked as %{bin:opam} to ensure that the manifested runtime on mingw is
; assembled, if it was selected at configure-time (%{exe:opamMain.exe} is not executable in this
; case.

(rule
  (targets opam.1)
  (deps opam-topics.inc opam-admin-topics.inc)
  (action (with-stdout-to %{targets} (run %{bin:opam} --help=groff))))

(install
  (section man)
  (package opam)
  (files opam.1))

(rule
  (with-stdout-to opam-installer.1 (run %{bin:opam-installer} --help=groff)))

(install
  (section man)
  (package opam-installer)
  (files opam-installer.1))

(executable
  (name dune_man)
  (libraries unix))

(rule
  (targets opam-topics.inc)
  (deps %{bin:opam} using-built-opam)
  (mode promote)
  (action (with-stdout-to %{targets} (run %{exe:dune_man.exe} opam)))
  (package opam))

(rule
  (targets opam-admin-topics.inc)
  (deps %{bin:opam} using-built-opam)
  (mode promote)
  (action (with-stdout-to %{targets} (run %{exe:dune_man.exe} opam admin)))
  (package opam))

(include opam-topics.inc)

(include opam-admin-topics.inc)

; This ensures that no opam command run will block asking for input
(env
  (_ (env-vars ("OPAMYES" "no"))))

; This ensure that %{bin:opam} really refers to the opam built in the tree
(rule
  (with-stdout-to check_local_build.ml
    (echo "let s = Sys.argv.(1) in exit (if not (Filename.is_implicit s) && Filename.is_relative s then 0 else 1)")))

(rule
  (action
    (with-stdout-to using-built-opam (run ocaml %{dep:check_local_build.ml} %{bin:opam})))
  (package opam))
