; This is an ad-hoc rule to ease the migration, it should be handled
; natively by Dune in the future.
(rule
 (targets index-subcomponents.html depends.dot)
 (deps
   %{project_root}/dev/tools/subcomponents.py
   (source_tree %{project_root}/theories)
   (source_tree %{project_root}/subcomponents))
 (action
  (chdir %{project_root}
   (run env python dev/tools/subcomponents.py doc/stdlib/index-subcomponents.html doc/stdlib/depends.dot))))

(rule
 (targets depends.svg)
 (deps depends.dot)
 (action (bash "dot -T svg depends.dot | sed -e 's/width=\".*\"/width=\"100%\"/' > depends.svg")))

(rule
  (targets (dir coqdoc-html))
  (deps
   ; This will be replaced soon by `theories/**/*.v` soon, thanks to rgrinberg
   (source_tree %{project_root}/theories)
   (source_tree %{project_root}/user-contrib)
   (:header %{project_root}/doc/common/styles/html/coqremote/header.html)
   (:footer %{project_root}/doc/common/styles/html/coqremote/footer.html)
   ; For .glob files, should be gone when Coq Dune is smarter.
   ; Please update .github/workflows/alpine.yml to still build rocq-stdlib when removing this dependency
   (package rocq-stdlib))
 (action
  (progn
   (run mkdir -p coqdoc-html)
   (bash "%{bin:coqdoc} -q -d coqdoc-html --with-header %{header} --with-footer %{footer} --multi-index --html -g --coqlib_url ../corelib -Q %{project_root}/theories Stdlib $(find %{project_root}/theories -name *.v)")
   )))

(rule
  (targets (dir html))
  (alias stdlib-html)
  (deps
   coqdoc-html
   index.html
   index-subcomponents.html
   depends.svg
   (:header %{project_root}/doc/common/styles/html/coqremote/header.html)
   (:footer %{project_root}/doc/common/styles/html/coqremote/footer.html))
 (action
  (progn
   (run cp -ar coqdoc-html html)
   (run mv html/index.html html/genindex.html)
   (with-stdout-to _index-subcomponents.html ; dune same-directory restriction
    (progn (cat %{header}) (echo "<h1>Rocq Standard Library: Internal Component Dependencies</h1>") (cat depends.svg) (cat index-subcomponents.html) (cat %{footer})))
   (run cp _index-subcomponents.html html/index-subcomponents.html)
   ; (run cp depends.svg html/depends.svg)
   (with-stdout-to _index.html ; dune same-directory restriction
    (progn (cat %{header}) (cat index.html) (cat %{footer})))
   (run cp _index.html html/index.html))))
