refactor: use updated LAVA log names in post-LAVA Expect scripts

The log splitting script (part of the CI jobs repository) now directs
LAVA terminal output to files of the form `lava-${NAME}.log`, where
`NAME` is the name of the terminal captured by the `NAME` regular
expression groups specified by the feedbacks in the LAVA job definition.

A recent edge-case showed that platforms with duplicate terminal names
may exist, which means we must now make several adjustments to how we
enumerate the terminals and name our feedbacks.

The most prominent change is that feedback terminals in the LAVA job
definitions now use the port as the feedback name. The port is the only
mechanism we have for uniquely identifying a UART so given its FVP
terminal printout so, while it isn't pretty, it is at least resilient.

Signed-off-by: Chris Kay <chris.kay@arm.com>
Change-Id: Ib6222e6342e7d795ea1f3f603ba1fed9405cb81b
diff --git a/script/expect-post-runner.sh b/script/expect-post-runner.sh
index 4f3ab68..b695b31 100755
--- a/script/expect-post-runner.sh
+++ b/script/expect-post-runner.sh
@@ -9,60 +9,80 @@
 # plans prepare in artefacts-lava/run/. See expect-post/README.md for
 # more info about post-expect scripts.
 
-if [ -z "$WORKSPACE" ]; then
-    echo "Error: WORKSPACE is not set. This script is intended to be run from Jenkins build. (Or suitably set up local env)."
-    exit 1
-fi
-
-total=0
-failed=0
+ci_root="$(readlink -f "$(dirname "$0")/..")" && \
+    . "${ci_root}/utils.sh"
 
 # TODO: move dependency installation to the Dockerfile
 sudo DEBIAN_FRONTEND=noninteractive apt update && \
     sudo DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC apt install -y expect ||
     exit 1
 
-for uartdir in $WORKSPACE/artefacts-lava/run/uart*; do
-    # In case no dirs exist and the glob above isn't expanded at all.
-    if [ ! -d "$uartdir" ]; then
-        break
-    fi
+archive="${WORKSPACE}/artefacts-lava"
 
+# Extract UART numbering from the FVP common log using the ports script
+declare -a ports=()
+
+ports_output="$(mktempfile)"
+
+awk -v "num_uarts=$(get_num_uarts "${archive}")" \
+    -f "$(get_ports_script "${archive}")" "${WORKSPACE}/lava-common.log" \
+        > "${ports_output}"
+
+. "${ports_output}" # Appends to `ports`
+
+total=0
+failed=0
+
+for uart in "${!ports[@]}"; do
     total=$((total + 1))
 
-    expscript_fragment=$(cat ${uartdir}/expect)
-    expscript=${WORKSPACE}/tf-a-ci-scripts/expect/${expscript_fragment}
+    uart_log_file="${WORKSPACE}/lava-uart${uart}.log"
+    uart_log_expect_file="${WORKSPACE}/lava-uart${uart}-expect.log"
+
+    if [ "${uart}" = "$(get_payload_uart "${archive}")" ]; then
+        mv "${WORKSPACE}/lava-common.log" "${uart_log_file}"
+    else
+        mv "${WORKSPACE}/lava-${ports[${uart}]:?}.log" "${uart_log_file}"
+    fi
+
+    expscript_stem="$(get_uart_expect_script "${archive}" "${uart}")"
+    expscript="${WORKSPACE}/tf-a-ci-scripts/expect/${expscript_stem}"
+
+    if [ -z "${expscript_stem}" ]; then
+        continue # Some UARTs may (legitimately) not have expectations
+    fi
 
     if [ ! -f "${expscript}" ]; then
-        echo "expect/${expscript_fragment}: MISS"
+        echo "expect/${expscript_stem}: MISS"
         failed=$((failed + 1))
 
         continue
     fi
 
-    uart=$(basename $uartdir)
-
     (
-        if [ -f "${uartdir}/env" ]; then
+        export uart_log_file # Required by the Expect script
+
+        if [ -f "$(get_uart_env_path "${archive}" "${uart}")/env" ]; then
             set -a
-            source "${uartdir}/env"
+
+            . "$(get_uart_env_path "${archive}" "${uart}")/env"
+
             set +a
         fi
 
-        export uart_log_file="${WORKSPACE}/lava-${uart}.log"
-
-        2>&1 expect "${expscript}" > "${WORKSPACE}/lava-${uart}-expect.log"
+        2>&1 expect "${expscript}" > "${uart_log_expect_file}"
     )
 
     if [ $? != 0 ]; then
-        echo "expect/${expscript_fragment}(${uart}): FAIL"
+        echo "expect/${expscript_stem}(UART${uart}): FAIL"
+
         failed=$((failed + 1))
     else
-        echo "expect/${expscript_fragment}(${uart}): pass"
+        echo "expect/${expscript_stem}(UART${uart}): PASS"
     fi
 done
 
-echo "Post expect scripts: total=$total failed=$failed"
+echo "Post-LAVA Expect scripts results: total=$total failed=$failed"
 
 if [ $failed -gt 0 ]; then
     exit 1