From 65a393cb3fc8086be05b86cbbffc7e40978531ca Mon Sep 17 00:00:00 2001
From: downtownallday <downtownallday@gmail.com>
Date: Wed, 22 Jun 2022 11:21:22 -0400
Subject: [PATCH] Fix the installed state version parsing logic

---
 tests/lib/installed-state.sh | 41 ++++++++++++++++++++++++++++++++----
 1 file changed, 37 insertions(+), 4 deletions(-)

diff --git a/tests/lib/installed-state.sh b/tests/lib/installed-state.sh
index 860e7e62..b092b98f 100644
--- a/tests/lib/installed-state.sh
+++ b/tests/lib/installed-state.sh
@@ -6,6 +6,29 @@
 # installations to a subsequent MiaB-LDAP upgrade
 #
 
+parse_miab_version_string() {
+    local tmpfile
+    tmpfile=$(mktemp)
+    awk -F- '
+/^v[0-9]+\./ { split($1,a,"."); print "MAJOR="substr(a[1],2); print "MINOR="a[2]; print "RELEASE="$2; next }  
+
+/^v[0-9]+[a-z]$/ { print "MAJOR="substr($1,2,length($1)-2); print "MINOR="substr($1,length($1))-"a"+1; print "RELEASE="; next }
+
+/^v[0-9]+[A-Z]$/ { print "MAJOR="substr($1,2,length($1)-2); print "MINOR="substr($1,length($1))-"A"+1; print "RELEASE="; next }
+
+/^v[0-9]+$/ { print "MAJOR="substr($1,2); print "MINOR="; print "RELEASE="; next } 
+
+{ exit 1 }' >> "$tmpfile" <<< "$1"
+    
+    if [ $? -ne 0 ]; then
+        rm -f "$tmpfile"
+        return 1
+    fi
+    source "$tmpfile"
+    rm -f "$tmpfile"
+    return 0
+}
+
 
 installed_state_capture() {
     # users and aliases
@@ -29,8 +52,18 @@ installed_state_capture() {
     fi
     H2 "create info.txt"
     echo "STATE_VERSION=1" > "$info"
-    echo "GIT_VERSION='$(git describe)'" >>"$info"
-    git describe | awk -F- '{ split($1,a,"."); print "MAJOR="substr(a[1],2); print "MINOR="a[2]; print "RELEASE="$2 }' >>"$info"
+    local gitver=$(git describe)
+    echo "GIT_VERSION='$gitver'" >>"$info"
+
+    parse_miab_version_string "$gitver"
+    if [ $? -ne 0 ]; then
+        echo "Unable to parse version string: $gitver"
+        return 1
+    fi
+    echo "MAJOR=$MAJOR" >>"$info"
+    echo "MINOR=$MINOR" >>"$info"
+    echo "RELEASE=$RELEASE" >>"$info"
+    
     echo "GIT_ORIGIN='$(git remote -v | grep ^origin | grep 'fetch)$' | awk '{print $2}')'" >>"$info"
     echo "MIGRATION_VERSION=$([ -e "$STORAGE_ROOT/mailinabox.version" ] && cat "$STORAGE_ROOT/mailinabox.version")" >>"$info"
     echo "MIGRATION_ML_VERSION=$([ -e "$STORAGE_ROOT/mailinabox-ldap.version" ] && cat "$STORAGE_ROOT/mailinabox-ldap.version")" >>"$info"
@@ -85,14 +118,14 @@ installed_state_compare() {
     #
     source "$s1/info.txt"
     MAJOR_A="$MAJOR"
-    MINOR_A="$MINOR"
+    MINOR_A="${MINOR:-0}"
     RELEASE_A="${RELEASE:-0}"
     PROD_A="miab"
     grep "mailinabox-ldap" <<<"$GIT_ORIGIN" >/dev/null && PROD_A="miabldap"
     
     source "$s2/info.txt"
     MAJOR_B="$MAJOR"
-    MINOR_B="$MINOR"
+    MINOR_B="${MINOR:-0}"
     RELEASE_B="${RELEASE:-0}"
     PROD_B="miab"
     grep "mailinabox-ldap" <<<"$GIT_ORIGIN" >/dev/null && PROD_B="miabldap"