Fixup the inventory instance numbering

1) Remove the '-' from before the instance number.
2) Remove the instance number completely if there's
   only one instance of that segment present.

Change-Id: If970799bb2e6c8a834be2df44d6a9fb86e026b39
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
diff --git a/Inventory.pm b/Inventory.pm
index 19dccdb..4efc38b 100644
--- a/Inventory.pm
+++ b/Inventory.pm
@@ -129,6 +129,12 @@
     #which is different than the regular type.
     renameSegmentWithTargetType("card-motherboard", "motherboard",
                                 $targetObj, $inventory);
+
+    #Don't need instance numbers unless there are more than 1 present
+    removeInstNumIfOneInstPresent($inventory);
+
+    #We want card1, not card-1
+    removeHyphensFromInstanceNum($inventory);
 }
 
 
@@ -290,6 +296,55 @@
     }
 }
 
+
+#Removes the instance number from the OBMC_NAME segments
+#where only 1 of those segments exists because numbering isn't
+#necessary to distinguish them.
+sub removeInstNumIfOneInstPresent
+{
+    my ($inventory) = @_;
+    my %instanceHash;
+
+    for my $item (@$inventory) {
+        #Look at all the segments, keeping track if we've
+        #seen a particular segment with the same instance before.
+        my @segments = split('/', $item->{OBMC_NAME});
+        for my $segment (@segments) {
+            my ($s, $inst) = $segment =~ /(\w+)-(\d+)/;
+            if (defined $s) {
+                if (not exists $instanceHash{$s}) {
+                    $instanceHash{$s}{inst} = $inst;
+                }
+                else {
+                    if ($instanceHash{$s}{inst} ne $inst) {
+                        $instanceHash{$s}{keep} = 1;
+                    }
+                }
+            }
+        }
+    }
+
+    #Remove the instanc numbers we don't need to keep.
+    for my $segment (keys %instanceHash) {
+        if (not exists $instanceHash{$segment}{keep}) {
+            for my $item (@$inventory) {
+               $item->{OBMC_NAME} =~ s/$segment-\d+/$segment/;
+            }
+        }
+    }
+}
+
+
+#Removes the '-' from between the segment name and instance.
+sub removeHyphensFromInstanceNum
+{
+    my ($inventory) = @_;
+
+    for my $item (@$inventory) {
+        $item->{OBMC_NAME} =~ s/-(\d+)\b/$1/g;
+    }
+}
+
 1;
 
 =head1 NAME