Get settings values from the MRW

Replace any MRW variables in the setting YAML with
their MRW value.
An MRW variable must be in the MRW_<MRW variable name> format.

Change-Id: Ic7e163beb4f5d13cae00ff2013b1c4080d39a256
Signed-off-by: Gunnar Mills <gmills@us.ibm.com>
diff --git a/gen_settings.pl b/gen_settings.pl
index 06794ff..d3389b5 100755
--- a/gen_settings.pl
+++ b/gen_settings.pl
@@ -10,17 +10,19 @@
 my $serverwizFile  = "";
 my $debug           = 0;
 my $outputFile     = "";
+my $settingsFile   = "";
 
 # Command line argument parsing
 GetOptions(
 "f"   => \$force,            # numeric
 "i=s" => \$serverwizFile,    # string
 "o=s" => \$outputFile,       # string
+"s=s" => \$settingsFile,     # string
 "d"   => \$debug,
 )
 or printUsage();
 
-if (($serverwizFile eq "") or ($outputFile eq ""))
+if (($serverwizFile eq "") or ($outputFile eq "") or ($settingsFile eq "") )
 {
     printUsage();
 }
@@ -40,17 +42,44 @@
 $targetObj->loadXML($serverwizFile);
 print "Loaded MRW XML: $serverwizFile \n";
 
+open(my $inFh, '<', $settingsFile) or die "Could not open file '$settingsFile' $!";
+open(my $outFh, '>', $outputFile) or die "Could not open file '$outputFile' $!";
+
+# Process all the targets in the XML
+foreach my $target (sort keys %{$targetObj->getAllTargets()})
+{
+    # A future improvement could be to specify the MRW target.
+    next if ("SYS" ne $targetObj->getType($target, "TYPE"));
+    # Read the settings YAML replacing any MRW_<variable name> with their
+    # MRW value
+    while (my $row = <$inFh>)
+    {
+        while ($row =~ /MRW_(.*?)\W/g)
+        {
+            my $setting = $1;
+            my $settingValue = $targetObj->getAttribute($target, $setting);
+            $row =~ s/MRW_${setting}/$settingValue/g;
+        }
+        print $outFh $row;
+    }
+    last;
+    close $inFh;
+    close $outFh;
+}
+
 # Usage
 sub printUsage
 {
     print "
-    $0 -i [XML filename] -o [Output filename] [OPTIONS]
+    $0 -i [XML filename] -s [Settings YAML] -o [Output filename] [OPTIONS]
+
+Required:
+    -i = MRW XML filename
+    -s = The Setting YAML with MRW variables in MRW_<MRW variable name> format
+    -o = YAML output filename
 Options:
     -f = force output file creation even when errors
     -d = debug mode
-
-PS: mrw::Targets can be found in https://github.com/open-power/serverwiz/
-    mrw::Inventory can be found in https://github.com/openbmc/phosphor-mrw-tools/
     \n";
     exit(1);
 }