From 341151f015a37c77c48a0f6ea14913ac59d67ecf Mon Sep 17 00:00:00 2001
From: "Andre Moreira Magalhaes (andrunko)" <andre.magalhaes@collabora.co.uk>
Date: Thu, 8 Nov 2018 20:21:25 -0200
Subject: [PATCH] Fix arch parsing on external-binaries.cfg

This fixes an issue prefix= and filename= were provided
but no architecture specified.

Signed-off-by: Andre Moreira Magalhaes (andrunko) <andre.magalhaes@collabora.co.uk>
---
 update_test_binaries.sh | 63 +++++++++++++++++++++++++++++++----------
 1 file changed, 48 insertions(+), 15 deletions(-)

diff --git a/update_test_binaries.sh b/update_test_binaries.sh
index b807576..9a55123 100755
--- a/update_test_binaries.sh
+++ b/update_test_binaries.sh
@@ -57,6 +57,18 @@ ARCHS="amd64 armhf arm64"
 PACKAGES=""
 BINARIES=""
 
+trim()
+{
+    local trimmed="$1"
+
+    # Strip leading space.
+    trimmed="${trimmed## }"
+    # Strip trailing space.
+    trimmed="${trimmed%% }"
+
+    echo "$trimmed"
+}
+
 download_packages_description() {
 	for SOURCE in ${SOURCES} ; do
 		[ -f Packages.${ARCH}.${SOURCE} ] && continue
@@ -84,15 +96,20 @@ download_extract_packages () {
 		[[ "$line" =~ ^#.*$ ]] && continue
 
 		IFS=" " read -r PACKAGE GLOB AVAILABLE_OPTIONS <<< $line
+		IFS="," read -r -a AVAILABLE_OPTIONS <<< $AVAILABLE_OPTIONS
 
-		#Leave only the list of arches if any
-		AVAILABLE_OPTIONS="${AVAILABLE_OPTIONS//prefix=.*(|,)/}"
-		AVAILABLE_OPTIONS="${AVAILABLE_OPTIONS//filename=.*(|,)/}"
-		# If package is listed for all architectures implicitly
-		[ -z "$AVAILABLE_OPTIONS" ] && AVAILABLE_OPTIONS="${ARCH}"
+		local REQUESTED_ARCHS=
+		for i in ${!AVAILABLE_OPTIONS[@]}; do
+			# If not a key=value pair, lets consider it a requested arch
+			if ! [[ ${AVAILABLE_OPTIONS[i]} =~ ^.*=.* ]]; then
+				REQUESTED_ARCHS="$REQUESTED_ARCHS ${AVAILABLE_OPTIONS[i]}"
+				continue
+			fi
+		done
+		[ -z "$REQUESTED_ARCHS" ] && REQUESTED_ARCHS="${ARCH}"
 
 		# If target arch is listed in arches list
-		if [[ "$AVAILABLE_OPTIONS" =~ (^|[[:space:]]|,)$ARCH($|[[:space:]]|,) ]]; then
+		if [[ "$REQUESTED_ARCHS" =~ (^|[[:space:]])$ARCH($|[[:space:]]) ]]; then
 			get_package ${PACKAGE}
 			dpkg --extract ${PACKAGE}_*_${ARCH}.deb ${EXTRACT}
 		fi
@@ -111,26 +128,42 @@ test_repository_requires_binaries () {
 
 update_test_repository () {
 	# Parse available options
-	# Format is <package> <file> <arch1>,<arch2>,prefix=<relative path>
+	# Format is <package> <file> <arch1>,<arch2>,prefix=<relative path>,filename=<filename>
 	# If an arch is specified, all non specified archs will be skipped
-	# If a prefix is added, the file will copied to target project under
+	# If prefix is specified, the file will copied to target project under
 	# <arch>/<relative path>. It is copied below "<arch>/bin" if no prefix
 	# is specified
+	# If filename is specified, use it as the file name
 	while read -r line ; do
 		# skip lines starting with "#"
 		[[ "$line" =~ ^#.*$ ]] && continue
 
 		IFS=" " read -r PACKAGE GLOB AVAILABLE_OPTIONS <<< $line
+		IFS="," read -r -a AVAILABLE_OPTIONS <<< $AVAILABLE_OPTIONS
+
+		local PREFIX=
+		local FILENAME=
+		local REQUESTED_ARCHS=
+		for i in ${!AVAILABLE_OPTIONS[@]}; do
+			# If not a key=value pair, lets consider it a requested arch
+			if ! [[ ${AVAILABLE_OPTIONS[i]} =~ ^.*=.* ]]; then
+				REQUESTED_ARCHS="$REQUESTED_ARCHS ${AVAILABLE_OPTIONS[i]}"
+				continue
+			fi
 
-		local PREFIX=$(echo "$AVAILABLE_OPTIONS" | sed -n 's/.*prefix=\([^,]*\).*/\1/p')
-		AVAILABLE_OPTIONS=${AVAILABLE_OPTIONS/prefix=$PREFIX/}
-		local FILENAME=$(echo "$AVAILABLE_OPTIONS" | sed -n 's/.*filename=\([^,]*\).*/\1/p')
-		AVAILABLE_OPTIONS=${AVAILABLE_OPTIONS/filename=$FILENAME/}
+			IFS="=" read -r -a REQUESTED_OPTIONS <<< ${AVAILABLE_OPTIONS[i]}
+			local OPTION_KEY=$(trim "${REQUESTED_OPTIONS[0]}")
+			local OPTION_VALUE=$(trim "${REQUESTED_OPTIONS[1]}")
+			case $OPTION_KEY in
+				prefix) PREFIX="$OPTION_VALUE" ;;
+				filename) FILENAME="$OPTION_VALUE" ;;
+				*) ;;
+			esac
+		done
 
 		# skip binaries not available for this arch
-		if [ ! -z "$AVAILABLE_OPTIONS" ]; then
-			AVAILABLE_OPTIONS=${AVAILABLE_OPTIONS//,/ } # {var//x/y} replaces all occurences of x in var
-			if ! [[ $AVAILABLE_OPTIONS =~ (^|[[:space:]])$ARCH($|[[:space:]]) ]]; then
+		if [ ! -z "$REQUESTED_ARCHS" ]; then
+			if ! [[ "$REQUESTED_ARCHS" =~ (^|[[:space:]])$ARCH($|[[:space:]]) ]]; then
 				echo "skipping ${GLOB} for ${ARCH}"
 				continue
 			fi
-- 
GitLab