|
|
@@ -0,0 +1,108 @@
|
|
|
+using Invercargill.Mapping;
|
|
|
+using InvercargillJson;
|
|
|
+
|
|
|
+public int genconfig_main() {
|
|
|
+ // Create a configuration with is_managed = false and autodiscovered paths
|
|
|
+ var config = new Usm.Configuration();
|
|
|
+ config.is_managed = false;
|
|
|
+ config.managed_config = null;
|
|
|
+
|
|
|
+ // Start with defaults and autodetect system-specific paths
|
|
|
+ var paths = new Usm.Paths.defaults();
|
|
|
+
|
|
|
+ // Autodetect the correct lib directory for this system
|
|
|
+ // Uses ldconfig to find the system's primary library directory
|
|
|
+ // canonlib remains "lib" as it's for architecture-independent libraries
|
|
|
+ paths.lib = detect_lib_dir();
|
|
|
+
|
|
|
+ config.paths = paths;
|
|
|
+
|
|
|
+ // Map to properties and create JSON
|
|
|
+ var properties = Usm.Configuration.get_mapper().map_from(config);
|
|
|
+ var json_element = new JsonElement.from_properties(properties);
|
|
|
+
|
|
|
+ // Output pretty-printed JSON to stdout
|
|
|
+ print("%s\n", json_element.stringify(true));
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+private string detect_lib_dir() {
|
|
|
+ try {
|
|
|
+ // Use ldconfig -p to get the library cache and find the primary lib directory
|
|
|
+ // The first entry's path tells us where the system stores its libraries
|
|
|
+ var proc = new Subprocess.newv(
|
|
|
+ new string[] { "ldconfig", "-p" },
|
|
|
+ SubprocessFlags.STDOUT_PIPE
|
|
|
+ );
|
|
|
+
|
|
|
+ var stdout_pipe = proc.get_stdout_pipe();
|
|
|
+ var data_input = new DataInputStream(stdout_pipe);
|
|
|
+
|
|
|
+ // Skip the header line (e.g., "3426 libs found in cache `/etc/ld.so.cache'")
|
|
|
+ var header = data_input.read_line();
|
|
|
+ if (header == null) {
|
|
|
+ // Drain remaining output before wait
|
|
|
+ while (data_input.read_line() != null) {}
|
|
|
+ proc.wait();
|
|
|
+ return "lib";
|
|
|
+ }
|
|
|
+
|
|
|
+ // Read the first library entry to get the path
|
|
|
+ var first_entry = data_input.read_line();
|
|
|
+
|
|
|
+ // Drain remaining output to avoid deadlock
|
|
|
+ while (data_input.read_line() != null) {}
|
|
|
+
|
|
|
+ proc.wait();
|
|
|
+
|
|
|
+ if (first_entry == null) {
|
|
|
+ return "lib";
|
|
|
+ }
|
|
|
+
|
|
|
+ // Parse the entry: " libname.so (arch) => /lib64/libname.so"
|
|
|
+ // We want to extract the directory from the path after "=> "
|
|
|
+ var arrow_pos = first_entry.index_of("=> ");
|
|
|
+ if (arrow_pos == -1) {
|
|
|
+ return "lib";
|
|
|
+ }
|
|
|
+
|
|
|
+ var full_path = first_entry.substring(arrow_pos + 3).strip();
|
|
|
+ // full_path is now "/lib64/lib3mf.so.2"
|
|
|
+
|
|
|
+ // Find the directory part (everything before the last "/")
|
|
|
+ var last_slash = full_path.last_index_of("/");
|
|
|
+ if (last_slash <= 0) {
|
|
|
+ return "lib";
|
|
|
+ }
|
|
|
+
|
|
|
+ // Extract directory path (e.g., "/lib64" from "/lib64/lib3mf.so.2")
|
|
|
+ var dir_path = full_path.substring(0, last_slash);
|
|
|
+ // dir_path is now "/lib64"
|
|
|
+
|
|
|
+ // Get the directory name (e.g., "lib64" from "/lib64")
|
|
|
+ var prev_slash = dir_path.last_index_of("/");
|
|
|
+ var lib_dir = prev_slash >= 0 ? dir_path.substring(prev_slash + 1) : dir_path;
|
|
|
+
|
|
|
+ // Verify it's actually a lib directory
|
|
|
+ if (!lib_dir.has_prefix("lib")) {
|
|
|
+ return "lib";
|
|
|
+ }
|
|
|
+
|
|
|
+ return lib_dir;
|
|
|
+ }
|
|
|
+ catch (Error e) {
|
|
|
+ // Fallback to checking for directory existence if ldconfig fails
|
|
|
+ return detect_lib_dir_fallback();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+private string detect_lib_dir_fallback() {
|
|
|
+ var usr_lib64 = File.new_for_path("/usr/lib64");
|
|
|
+ if (usr_lib64.query_exists()) {
|
|
|
+ return "lib64";
|
|
|
+ }
|
|
|
+
|
|
|
+ // Default fallback
|
|
|
+ return "lib";
|
|
|
+}
|