From d9002f47e0489f517f8c839500c49690e72fbc16 Mon Sep 17 00:00:00 2001
From: Christian Engwer <christi@dune-project.org>
Date: Tue, 7 Oct 2008 18:34:26 +0000
Subject: [PATCH] implemented code to do a version check in dunecontrol

next step is to add parameters to the dune.module file
and parse these.

[[Imported from SVN: r5305]]
---
 bin/dunemodules.inc      | 101 ++++++++++++++++++++++++++++++++++++++
 bin/test-version-compare | 102 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 203 insertions(+)
 create mode 100755 bin/test-version-compare

diff --git a/bin/dunemodules.inc b/bin/dunemodules.inc
index de2e6280c..9b0cc277c 100644
--- a/bin/dunemodules.inc
+++ b/bin/dunemodules.inc
@@ -401,3 +401,104 @@ check_modname() {
 #  fi
   return 0
 }
+
+#
+# compare a sub part of the version string
+#
+# parameters:
+# $1 version
+# $2 part
+#
+# parts:
+# 1: major
+# 2: minor
+# 3: revision
+#
+get_sub_version() {
+  echo $1 | awk "{ split(\$0,s,\".\"); match(s[$2],/[[:digit:]]*/); print substr(s[$2],RSTART,RLENGTH) }"
+}
+
+#
+# compare to versions
+#
+# parameters:
+# $1 version1
+# $2 version1
+#
+# return:
+# 0: equal
+# 1: v1 > v2
+# 2: v1 < v2
+#
+compare_versions() {
+  local v1="$1"
+  local v2="$2"
+  local result
+
+  for i in 1 2 3; do
+    result=0
+    compare_sub_version $v1 $v2 $i || return 0
+  done
+
+  echo "eq"
+}
+
+compare_sub_version() {
+  # compare sub version number
+  local sub1=`get_sub_version $1 $3`
+  local sub2=`get_sub_version $2 $3`
+  if test x$sub1 = x ; then
+    sub1=0
+  fi
+  if test x$sub2 = x ; then
+    sub2=0
+  fi
+
+  if test $sub1 -gt $sub2; then
+    echo "gt"
+	return 1
+  fi
+  if test $sub1 -lt $sub2; then
+    echo "lt"
+	return 1
+  fi
+
+  return 0
+}
+
+check_version() {
+  local v=$1
+  local PATTERN="^ *\([<>=]*\) *\([0-9.]*\)\(.*\)$"
+  if test x != `echo "$2" | sed -e "s/$PATTERN/x/"`; then
+    echo "ERROR: invalid version constraint $2" >&2
+    exit 1
+  fi
+  local op=`echo "$2" | sed -e "s/$PATTERN/\1/"`
+  local v2=`echo "$2" | sed -e "s/$PATTERN/\2/"`
+  local rest=`echo "$2" | sed -e "s/$PATTERN/\3/" -e 's/ //g'`
+  local result=1
+  
+  local rel=`compare_versions $v $v2`
+
+  case $rel$op in
+  "eq<="|"eq="|"eq>="|\
+  "gt>="|"gt>"|\
+  "lt<="|"lt<")
+    result=0
+	;;
+  esac
+  if test -z "$rest"; then
+    return $result
+  fi
+  PATTERN="\([|&]\{2\}\)\(.*\)$"
+  if test xx != x`echo "$rest" | sed -e "s/$PATTERN/x/"`; then
+    echo "ERROR: invalid version constraint '$rest'" >&2
+    exit 1
+  fi
+  op=`echo "$rest" | sed -e "s/$PATTERN/\1/"`
+  v2=`echo "$rest" | sed -e "s/$PATTERN/\2/"`
+  if eval "test $result -eq 0" $op "check_version \"$v\" \"$v2\""; then
+    return 0
+  fi
+  return 1
+}
\ No newline at end of file
diff --git a/bin/test-version-compare b/bin/test-version-compare
new file mode 100755
index 000000000..b381f35a0
--- /dev/null
+++ b/bin/test-version-compare
@@ -0,0 +1,102 @@
+#!/bin/bash
+
+###############################################
+###
+### read lib
+###
+
+canonicalname(){
+	if test $# -ne 1; then
+		echo Usage: canonicalname path >&2
+		return 1
+	fi
+	file="$1"
+	if test ! -e "$file"; then
+		echo $file: file not found >&2
+		return 1
+	fi
+    # if this is a symlink, then follow the symlink
+	if test -L "$file"; then
+		fdir="`dirname \"$file\"`"
+		flink="`readlink \"$file\"`"
+		if test -e "$flink"; then
+			# these are absolute links, or links in the CWD
+			canonicalname "$flink"
+		else
+			canonicalname "$fdir/$flink"
+		fi
+	else
+        # if this is a file, then remember the filename and
+        # canonicalize the directory name
+		if test -f "$file"; then
+			fdir="`dirname \"$file\"`"
+			fname="`basename \"$file\"`"
+			fdir="`canonicalname \"$fdir\"`"
+			echo "$fdir/$fname"
+		fi
+        # if this is a directory, then create an absolute 
+        # directory name and we are done
+		if test -d "$file"; then
+			(cd "$file"; pwd)
+		fi
+	fi
+}
+
+canonicalpath(){
+  if test $# -ne 1; then
+     echo Usage: canonicalpath path >&2
+     return 1
+  fi
+  dirname $(canonicalname "$1")
+}
+
+checkdebug () {
+  while test $# -gt 0; do
+    if test x$1 = x--debug; then
+      echo yes
+      return
+    fi
+    shift
+  done
+  echo no
+}
+
+DEBUG=`checkdebug $@`
+if test "x$DEBUG" = "xyes"; then
+  set -x
+  set -v
+fi
+
+export COMMAND_DIR="`canonicalpath $0`"
+
+# Read the modules find part
+. "$COMMAND_DIR/dunemodules.inc"
+
+#
+# test version checks
+#
+
+test_version_check () {
+  if ! check_version "$1" "$2"; then
+    echo "ERROR: version does not match (found $1, required $2)" >&2
+    #exit 1
+  else
+    echo "OK:    version does match     (found $1, required $2)" >&2
+  fi
+}
+
+test_version_check "1.2.3" ">= 1.2.5 || < 1.2.4"
+test_version_check "1.2.4" ">= 1.2.5 || < 1.2.4"
+test_version_check "1.2.5" ">= 1.2.5 || < 1.2.4"
+test_version_check "1.2" ">= 1.2.5 || < 1.2.4"
+
+test_version_check "1.2.3" ">= 1.2.5 && < 1.2.4"
+test_version_check "1.2.4" "< 1.2.5 && >= 1.2.4"
+
+test_version_check "1.2.3" ">= 1.2"
+test_version_check "1.2.3" "= 2.4.1"
+test_version_check "1.2.3" "= 1.2.3"
+test_version_check "1.2.3" "> 1.2"
+test_version_check "1.2.3" "= 1.2"
+test_version_check "1.2.3" "< 1.2"
+
-- 
GitLab