From 8e492b2bd3a92e18283902eee197302eabdaa140 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= <karel.koci@nic.cz>
Date: Wed, 2 Dec 2020 09:54:16 +0100
Subject: [PATCH] compile_pkgs: refactor stat step

This makes stat little bit more resilient. It also should make it more
readable as we first prepare data and then insert it in formated output.
---
 compile_pkgs | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/compile_pkgs b/compile_pkgs
index b4401580c..a65d3d3e2 100755
--- a/compile_pkgs
+++ b/compile_pkgs
@@ -168,15 +168,20 @@ available_commands+=( ["stats"]="	Prints statistics regarding number of built pa
 stats() {
 	mkdir -p logs
 	report "Reporting statistics"
-	{
-		echo "Statistics of the build:"
-		echo
-		echo " * $(find bin/packages -name '*.ipk' | wc -l) binary packages built"
-		[ -f "logs/package/error.txt" ] && \
-			echo -n " * $(wc -l <logs/package/error.txt)" || \
-			echo -n " * 0"
-		echo "/$(find logs/package/ -name 'compile.txt' | wc -l) source packages failed"
-	} | tee logs/stats
+	local binpackages=0 failed=0 packages=0
+	if [ -d "bin" ]; then
+		binpackages="$(find bin -name '*.ipk' | wc -l)"
+	fi
+	if [ -f "logs/package/error.txt" ]; then
+		failed="$(wc -l < logs/package/error.txt)"
+		packages="$(find logs/package -name 'compile.txt' | wc -l)"
+	fi
+	tee logs/stats <<-EOF
+		Statistics of the build:
+
+		 * $binpackages binary packages built"
+		 * $failed/$packages source packages failed"
+	EOF
 }
 
 available_commands+=( ["configure"]="Recreates configuration for target boards" )
-- 
GitLab