From 3ad9c8d346f90ce22408b97c2effb4cb5225e1bd Mon Sep 17 00:00:00 2001
From: John Haugabook <johnhaugabook@gmail.com>
Date: Wed, 16 Jul 2025 22:51:09 +0100
Subject: [PATCH setup] add --build-depend option

add --build-depend option initial debugging

add --build-depend option
---
 Makefile.am      |  2 ++
 build_depends.cc | 92 ++++++++++++++++++++++++++++++++++++++++++++++++
 build_depends.h  | 24 +++++++++++++
 main.cc          |  8 ++++-
 res/en/res.rc    |  1 +
 resource.h       |  1 +
 6 files changed, 127 insertions(+), 1 deletion(-)
 create mode 100644 build_depends.cc
 create mode 100644 build_depends.h

diff --git a/Makefile.am b/Makefile.am
index 4046d37..daf1609 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -136,6 +136,8 @@ endif
 	archive_tar.cc \
 	archive_tar.h \
 	archive_tar_file.cc \
+	build_depends.h \
+	build_depends.cc \
 	choose.cc \
 	choose.h \
 	compactos.cc \
diff --git a/build_depends.cc b/build_depends.cc
new file mode 100644
index 0000000..6829f2c
--- /dev/null
+++ b/build_depends.cc
@@ -0,0 +1,92 @@
+/*
+ *     This program is free software; you can redistribute it and/or modify
+ *     it under the terms of the GNU General Public License as published by
+ *     the Free Software Foundation; either version 2 of the License, or
+ *     (at your option) any later version.
+ *
+ *     A copy of the GNU General Public License can be found at
+ *     http://www.gnu.org/
+ *
+ */
+
+
+#include <fstream>
+#include <algorithm>
+#include <iostream>
+#include <string>
+#include <sstream>
+#include <cstdio>
+
+#include "io_stream.h"
+#include "ini.h"
+#include "resource.h"
+
+
+std::string 
+build_depend(std::string pkg_search, std::string stream) 
+{
+  // Use option passed as argument.
+  std::string target_pkg = pkg_search;
+
+  // Mark and property for identifying package and extracting value.
+  std::string pkg_marker = "@";
+  std::string key = "build-depends:";
+
+  std::string depend_packages; // for final output
+  // NEXT
+  //std::ifstream infile("setup.ini");
+  std::ifstream infile(stream);
+  
+  // Ensure setup.ini was identified
+  if (!infile) {
+    depend_packages = "Error opening setup.ini";
+  }
+
+  // Start process of reading setup.
+  std::string line;
+  bool inPkg = false;
+  //io_stream *infile = stream;
+  while (std::getline(infile, line)) {
+    std::istringstream iss(line);
+    std::string first, second;
+    iss >> first >> second;
+
+    // For each pkg_marker check if name matches option.
+    if (first == pkg_marker) 
+    {
+      if (second == target_pkg) 
+      {
+        // Package found, handle next lines accordingly.
+        inPkg = true;
+        continue;
+      }
+      else 
+      {
+        inPkg = false;
+        continue;
+      }
+    }
+
+    // If package found and hsa property build-depends.
+    if (inPkg && line.find(key) == 0) {
+      /* Extract by property length, then remove space character 
+      using ASCII encoding. */
+      std::string depend_packages = line.substr(key.size());
+      replace(depend_packages.begin(), depend_packages.end(), 32, 00);
+      
+      // NEXT
+      // Output to PackageOption.
+      return "build-depends: " + depend_packages;
+      break;
+    } else {
+     continue;
+    }
+  }
+
+  // Output if no package.
+  if (inPkg == false) 
+  {
+    return "build-depends: no"; 
+  }
+  return 0;
+}
diff --git a/build_depends.h b/build_depends.h
new file mode 100644
index 0000000..eec9921
--- /dev/null
+++ b/build_depends.h
@@ -0,0 +1,24 @@
+/*
+ *     This program is free software; you can redistribute it and/or modify
+ *     it under the terms of the GNU General Public License as published by
+ *     the Free Software Foundation; either version 2 of the License, or
+ *     (at your option) any later version.
+ *
+ *     A copy of the GNU General Public License can be found at
+ *     http://www.gnu.org/
+ *
+ */
+
+#ifndef USE_BUILD_DEPENDS_H
+#define USE_BUILD_DEPENDS_H
+
+#include "getopt++/StringOption.h"
+#include "resource.h"
+
+// Create option class.
+StringOption BuildDependsOption ("", '\0', "build-depend", IDS_HELPTEXT_BUILD_DEPENDS, false);
+
+// Forward declare function for use.
+std::string build_depend(std::string pkg_search, std::string stream);
+
+#endif /* USE_BUILD_DEPENDS_H */
diff --git a/main.cc b/main.cc
index 91d0eca..45e456f 100644
--- a/main.cc
+++ b/main.cc
@@ -42,6 +42,7 @@
 #include "mount.h"
 #include "LogFile.h"
 #include "setup_version.h"
+#include "build_depends.h"
 
 #include "proppage.h"
 #include "propsheet.h"
@@ -300,7 +301,12 @@ WinMain (HINSTANCE h,
     std::string elevate_extra_args;
 
     if (unattended_mode || output_only || !elevate)
-      set_cout ();
+    {
+      std::string build_depends_options = BuildDependsOption;
+       if (build_depends_options.length() > 1)
+           std::cout << build_depend(build_depends_options, "setup.ini") << std::endl;
+      set_cout();
+    }
 
     /* Start logging only if we don't elevate.  Same for setting default
        security settings. */
diff --git a/res/en/res.rc b/res/en/res.rc
index 0bdd4c7..d8b2298 100644
--- a/res/en/res.rc
+++ b/res/en/res.rc
@@ -656,6 +656,7 @@ BEGIN
     IDS_HELPTEXT_ALLOW_UNSUPPORTED_WINDOWS "Allow old, unsupported Windows versions"
     IDS_HELPTEXT_ARCH "Architecture to install (x86_64 or x86)"
     IDS_HELPTEXT_CATEGORIES "Specify categories to install"
+    IDS_HELPTEXT_BUILD_DEPENDS "Install the build requirements of a package"
     IDS_HELPTEXT_COMPACTOS "Compress installed files with Compact OS (xpress4k, xpress8k, xpress16k, lzx)"
     IDS_HELPTEXT_DELETE_ORPHANS "Remove orphaned packages"
     IDS_HELPTEXT_DISABLE_ANTIVIRUS "Disable known or suspected buggy anti virus software packages during execution"
diff --git a/resource.h b/resource.h
index a08489c..11fef73 100644
--- a/resource.h
+++ b/resource.h
@@ -158,6 +158,7 @@
 #define IDS_HELPTEXT_HEADER              1546
 #define IDS_HELPTEXT_FOOTER              1547
 #define IDS_HELPTEXT_NO_WRITE_REGISTRY   1548
+#define IDS_HELPTEXT_BUILD_DEPENDS       1549
 
 // Dialogs
 
-- 
2.49.0.windows.1

