(library
  (name        opam_client)
  (public_name opam-client)
  (synopsis    "OCaml Package Manager client and CLI library")
  (modules     (:standard \ opamMain opamManifest get_git_version))
  (libraries   opam-state opam-solver re cmdliner)
  (flags       (:standard
               (:include ../ocaml-flags-standard.sexp)
               (:include ../ocaml-context-flags.sexp)))
  (wrapped     false))

(executable
  (name        opamMain)
  (public_name opam)
  (package     opam)
  (modules     opamMain opamManifest)
  (flags       (:standard
               (:include ../ocaml-flags-standard.sexp)
               (:include ../ocaml-context-flags.sexp)
               (:include linking.sexp)))
  (link_flags  (:include manifest.sexp))
  (libraries   opam-client))

(rule
  (targets manifest.sexp)
  (deps    (:script ../../shell/subst_var.ml) ../../config.status (:input manifest.sexp.in))
  (action  (with-stdout-to %{targets} (run ocaml %{script} CONF_MANIFEST_O "" %{input}))))

(include opamManifest.inc)

(include manifest.inc)

(rule
  (targets git-sha)
  (deps    (universe))
  (action  (ignore-stderr (with-stdout-to %{targets} (system "git rev-parse --quiet --verify HEAD || echo .")))))

(rule
  (targets git-describe)
  (deps    (universe))
  (action  (ignore-stderr (with-stdout-to %{targets} (system "git describe --exact HEAD || echo [dev]")))))

(rule
  (targets no-git-version)
  (mode fallback)
  (action (copy git-sha %{targets})))

(rule
  (with-stdout-to get_git_version.ml
    (echo "print_string @@ \
             let v = \"%{read-lines:no-git-version}\" in \
             let w = \"%{read-lines:git-describe}\" in \
             if v = \"\" || v = \".\" || w <> \"[dev]\" then \
               \"let version = None\" \
             else \
               \"let version = Some \\\"\" ^ v ^ \"\\\"\"")))

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

(rule
  (with-stdout-to linking.sexp (run echo "()")))
