sdbus++-gendir: ensure errors exit with error code

If 'wait' is called without a pid, it waits for all existing
jobs to exit but does not reproduce the return code of the waited-on
jobs.  This means that if an error occurs in the sdbus++ processing
this script would still return a good return code.

Create a 'waitall' function that waits for pending jobs one at a time
so that any error code is correctly caught and replicated to the caller
of this script.

Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: Ic7745752fd5bdde2b924a7509ea64464af73fdd7
diff --git a/tools/sdbus++-gendir b/tools/sdbus++-gendir
index 921645b..95bdaf3 100755
--- a/tools/sdbus++-gendir
+++ b/tools/sdbus++-gendir
@@ -76,6 +76,8 @@
     exit 1
 fi
 
+all_jobs=""
+
 declare -A emitted_file_names
 # generate_single -- make a single call to sdbus++.
 #   $1: sdbus++ TYPE
@@ -91,8 +93,10 @@
     elif [ "x" == "x$5" ];
     then
         $sdbuspp $1 $2 $3 > $outputdir/$4 &
+        all_jobs="$all_jobs $!"
     else
         $sdbuspp $1 $2 $3 >> $outputdir/$4 &
+        all_jobs="$all_jobs $!"
     fi
 
     # Emit filename as needed.
@@ -118,11 +122,19 @@
     fi
 
     # Ensure that no more than ${parallel} jobs are running at a time and if so
-    # wait for at least one to finish.
-    while [[ $(jobs -r -p | wc -l) -ge $parallel ]];
+    # wait for them to finish.
+    if [[ $(echo $all_jobs | wc -w) -ge $parallel ]];
+    then
+        waitall
+    fi
+}
+
+function waitall {
+    for job in $all_jobs;
     do
-        wait -n
+        wait $job
     done
+    all_jobs=""
 }
 
 for d in $@;
@@ -155,7 +167,7 @@
         generate_single interface markdown $iface $path.md append
 
     done
-    wait # finish all before continuing
+    waitall # finish all before continuing
 
 
     for e in $errors;
@@ -168,5 +180,5 @@
         generate_single error markdown $iface $path.md append
 
     done
-    wait # finish all before continuing
+    waitall # finish all before continuing
 done