From 3bcadbaaf209a96e237525adcf0add6f795576dd Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Fri, 28 Jan 2022 14:54:09 +0100
Subject: [PATCH] [ogs] Added new cli option 'write-prj' to write out processed
 prj file.

The file is written to out_dir/[prj_base_name]_processed.prj.
---
 Applications/CLI/ogs.cpp  | 11 +++++++++--
 BaseLib/PrjProcessing.cpp | 11 ++++++-----
 BaseLib/PrjProcessing.h   |  3 ++-
 3 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/Applications/CLI/ogs.cpp b/Applications/CLI/ogs.cpp
index 147852552cc..04d30ad5371 100644
--- a/Applications/CLI/ogs.cpp
+++ b/Applications/CLI/ogs.cpp
@@ -90,6 +90,12 @@ int main(int argc, char* argv[])
         false, "");
     cmd.add(xml_patch_files);
 
+    TCLAP::SwitchArg write_prj("",
+                               "write-prj",
+                               "Writes processed project file to output "
+                               "path / [prj_base_name]_processed.prj.");
+    cmd.add(write_prj);
+
     TCLAP::ValueArg<std::string> outdir_arg("o", "output-directory",
                                             "the output directory to write to",
                                             false, "", "PATH");
@@ -197,8 +203,9 @@ int main(int argc, char* argv[])
             run_time.start();
 
             std::stringstream prj_stream;
-            BaseLib::prepareProjectFile(prj_stream, project_arg.getValue(),
-                                        xml_patch_files.getValue());
+            BaseLib::prepareProjectFile(
+                prj_stream, project_arg.getValue(), xml_patch_files.getValue(),
+                write_prj.getValue(), outdir_arg.getValue());
 
             auto project_config = BaseLib::makeConfigTree(
                 project_arg.getValue(), !nonfatal_arg.getValue(),
diff --git a/BaseLib/PrjProcessing.cpp b/BaseLib/PrjProcessing.cpp
index 7d932ef31b4..1f2105ddaf8 100644
--- a/BaseLib/PrjProcessing.cpp
+++ b/BaseLib/PrjProcessing.cpp
@@ -19,7 +19,6 @@
 
 namespace BaseLib
 {
-
 bool isEqual(const unsigned char* a, std::string const& b)
 {
     return std::string{reinterpret_cast<const char*>(a)} == b;
@@ -273,7 +272,8 @@ void readAndPatchPrj(std::stringstream& prj_stream, std::string& prj_file,
 
 void prepareProjectFile(std::stringstream& prj_stream,
                         const std::string& filepath,
-                        const std::vector<std::string>& patch_files)
+                        const std::vector<std::string>& patch_files,
+                        bool write_prj, const std::string& out_directory)
 {
     std::string prj_file = filepath;
 
@@ -288,7 +288,7 @@ void prepareProjectFile(std::stringstream& prj_stream,
             patchStream(patch_file, prj_stream, true);
         }
     }
-    if (true)
+    if (write_prj)
     {
         // pretty-print
         xmlKeepBlanksDefault(0);
@@ -300,8 +300,9 @@ void prepareProjectFile(std::stringstream& prj_stream,
         // xmlThrDefTreeIndentString("");
         auto doc =
             xmlParseMemory(prj_stream.str().c_str(), prj_stream.str().size());
-        auto prj_out =
-            std::filesystem::path(prj_file).parent_path() / "_project.prj";
+        auto prj_out = std::string(std::filesystem::path(out_directory) /
+                                   std::filesystem::path(prj_file).stem()) +
+                       "_processed.prj";
         xmlSaveFormatFileEnc(prj_out.c_str(), doc, "utf-8", 1);
         xmlFreeDoc(doc);
     }
diff --git a/BaseLib/PrjProcessing.h b/BaseLib/PrjProcessing.h
index 2b54de2d7a7..267f81cd834 100644
--- a/BaseLib/PrjProcessing.h
+++ b/BaseLib/PrjProcessing.h
@@ -24,5 +24,6 @@ namespace BaseLib
  */
 void prepareProjectFile(std::stringstream& prj_stream,
                         const std::string& filepath,
-                        const std::vector<std::string>& patch_files = {});
+                        const std::vector<std::string>& patch_files,
+                        bool write_prj, const std::string& out_directory);
 }  // namespace BaseLib
-- 
GitLab