gen_settings.pl: Pass in new commandline argument

This is the first commit in a series to solve the problem
of needing to adjust an MRW attribute's value before writing
it back out to the settings override value.

This will be done by allowing mathematical expressions to
be present in the input file, and evaluating them to get
a final result that will get written out.  These expressions
can also use variables whose names and values are passed in
from the command line.

This commit adds the ability to pass in the optional variables
and values from the command line with the -v argument, like:
  -v "VAR1=VAL1 VAR2=VAL2"

Change-Id: I3baf47b38d6da25c5abdaae175ddfc6e97176927
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
diff --git a/gen_settings.pl b/gen_settings.pl
index d3389b5..2f94e49 100755
--- a/gen_settings.pl
+++ b/gen_settings.pl
@@ -1,4 +1,20 @@
 #!/usr/bin/env perl
+
+#This script replaces MRW attribute names with their values from the
+#MRW XML in any file.  In addition, it can evaluate mathematical
+#expressions if they are in [[ ]]s and can use variables passed in from
+#the command line in those expressions.
+#
+#For example, if the attribute FOO has a value of 50 in the MRW, and
+#the program was started with: -v "MY_VAR1=200 MY_VAR2=400"
+#
+#then the line
+#  [[(MRW_FOO * MY_VAR1) + 5]]..[[MRW_FOO * MY_VAR2]]
+#
+#would get written out as:
+#  10005..20000
+#
+
 use strict;
 use warnings;
 
@@ -6,11 +22,13 @@
 use Getopt::Long; # For parsing command line arguments
 
 # Globals
-my $force           = 0;
+my $force          = 0;
 my $serverwizFile  = "";
-my $debug           = 0;
+my $debug          = 0;
 my $outputFile     = "";
 my $settingsFile   = "";
+my $expressionVars = "";
+my %exprVars;
 
 # Command line argument parsing
 GetOptions(
@@ -18,6 +36,7 @@
 "i=s" => \$serverwizFile,    # string
 "o=s" => \$outputFile,       # string
 "s=s" => \$settingsFile,     # string
+"v=s" => \$expressionVars,   # string
 "d"   => \$debug,
 )
 or printUsage();
@@ -45,6 +64,11 @@
 open(my $inFh, '<', $settingsFile) or die "Could not open file '$settingsFile' $!";
 open(my $outFh, '>', $outputFile) or die "Could not open file '$outputFile' $!";
 
+if (length($expressionVars) > 0)
+{
+    loadVars($expressionVars);
+}
+
 # Process all the targets in the XML
 foreach my $target (sort keys %{$targetObj->getAllTargets()})
 {
@@ -67,16 +91,41 @@
     close $outFh;
 }
 
+#Parse the variable=value string passed in from the
+#command line and load it into %exprVars.
+sub loadVars
+{
+    my $varString = shift;
+
+    #Example: "VAR1=VALUE1 VAR2=VALUE2"
+    my @entries = split(' ', $varString);
+
+    for my $entry (@entries)
+    {
+        my ($var, $value) = $entry =~ /(.+)=(.+)/;
+
+        if ((not defined $var) || (not defined $value))
+        {
+            die "Could not parse expression variable string $varString\n";
+        }
+
+        $exprVars{$var} = $value;
+    }
+}
+
 # Usage
 sub printUsage
 {
     print "
-    $0 -i [XML filename] -s [Settings YAML] -o [Output filename] [OPTIONS]
+    $0 -i [XML filename] -s [Settings YAML] -o [Output filename] -v [expr vars] [OPTIONS]
 
 Required:
     -i = MRW XML filename
     -s = The Setting YAML with MRW variables in MRW_<MRW variable name> format
     -o = YAML output filename
+Optional:
+    -v = Variables and values for any [[expression]] evaluation
+         in the form: \"VAR1=VALUE1 VAR2=VALUE2\"
 Options:
     -f = force output file creation even when errors
     -d = debug mode