(library
  (name        opam_state)
  (public_name opam-state)
  (libraries   opam-repository)
  (synopsis    "OCaml Package Manager instance management library")
  (modules_without_implementation OpamStateTypes)
  (modules     :standard \ flags)
  (flags       (:standard
               (:include ../ocaml-flags-standard.sexp)
               (:include ../ocaml-flags-configure.sexp)
               (:include ../ocaml-context-flags.sexp)
               (:include flags.sexp)))
  (wrapped     false))

(rule
  (targets opamScript.ml)
  (deps    ../../shell/crunch.ml (glob_files shellscripts/*.*sh))
  (action  (with-stdout-to %{targets} (run ocaml %{deps}))))


;; These rules are here to document and for easy (re-)generation of the module,
;; but relying on the checked-in file should be OK (we don't want to rely on
;; either `jq` or a network download). Hence the `(mode fallback)`.
(rule
  (targets opamSpdxList.ml)
  (mode fallback)
  (action (with-stdout-to %{targets}
    (progn
      (echo "(* THIS FILE IS GENERATED. See dune file *)\n\n")
      (echo "open OpamCompat\n")
      (echo "let licenses = OpamStd.String.Set.of_list @@ List.map String.lowercase_ascii @@ [\n")
      (cat spdx-license-list)
      (echo "]\n")
      (echo "let exceptions = OpamStd.String.Set.of_list @@ List.map String.lowercase_ascii @@ [\n")
      (cat spdx-license-exn-list)
      (echo "]\n")))))
(rule
  (targets spdx-license-list)
  (action  (with-stdout-to %{targets}
    (system
      "curl https://raw.githubusercontent.com/spdx/license-list-data/master/json/licenses.json |
       jq -rc '.licenses | map(\"  \\\"\" + .licenseId + \"\\\";\") | join(\"\\n\")'"))))
(rule
  (targets spdx-license-exn-list)
  (action  (with-stdout-to %{targets}
    (system
      "curl https://raw.githubusercontent.com/spdx/license-list-data/master/json/exceptions.json |
       jq -rc '.exceptions | map(\"  \\\"\" + .licenseExceptionId + \"\\\";\") | join(\"\\n\")'"))))

(rule
  (with-stdout-to flags.ml
    (echo "print_string (if String.sub Sys.ocaml_version 0 5 = \"4.02.\" then \"(-w -50)\" else \"()\")")))

(rule
  (with-stdout-to flags.sexp
    (run ocaml %{dep:flags.ml})))
