From 45c8ce5341d4e799a061d522851621795d454c5e Mon Sep 17 00:00:00 2001
From: Aki Tuomi <aki.tuomi@open-xchange.com>
Date: Tue, 22 Feb 2022 08:57:39 +0200
Subject: [PATCH] configure: Use external script to determine version

---
 .gitignore                        |  1 +
 Makefile.am                       |  4 ++++
 build-aux/git-abi-version-gen     | 14 ++++++++++++++
 build-aux/git-tarname-version-gen | 14 ++++++++++++++
 build-aux/git-version-gen         | 18 ++++++++++++++++++
 configure.ac                      | 11 ++++++++++-
 version.in                        |  1 +
 7 files changed, 62 insertions(+), 1 deletion(-)
 create mode 100755 build-aux/git-abi-version-gen
 create mode 100755 build-aux/git-tarname-version-gen
 create mode 100755 build-aux/git-version-gen
 create mode 100644 version.in

diff --git a/.gitignore b/.gitignore
index 6c303b0c2..9d1dcd4b4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -27,6 +27,7 @@ Makefile.in
 /build-aux/missing
 /run-test.sh
 /aclocal.m4
+/version
 /stamp.h
 /stamp-h1
 /stamp-h2
diff --git a/Makefile.am b/Makefile.am
index 1dfb0e6f4..39f32d6a5 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -12,6 +12,7 @@ SUBDIRS = \
 ACLOCAL_AMFLAGS = -I m4
 
 EXTRA_DIST = \
+	version \
 	tests \
 	examples \
 	COPYING.LGPL \
@@ -225,3 +226,6 @@ test: all-am $(test_cases) $(failure_test_cases)
 test-plugins: all-am $(extprograms_test_cases)
 
 check: check-am test
+
+version:
+	$(AM_V_GEN)$(top_srcdir)/build-aux/git-version-gen > $@
diff --git a/build-aux/git-abi-version-gen b/build-aux/git-abi-version-gen
new file mode 100755
index 000000000..f25ccd733
--- /dev/null
+++ b/build-aux/git-abi-version-gen
@@ -0,0 +1,14 @@
+#!/bin/sh
+
+## serial 1
+
+if test "${VERSION}" != ""; then
+  VN=`echo ${VERSION} | sed -e "s/^\([0-9]*\.[0-9]*\)\.\([0-9]*\).*/\1.ABIv\2(${VERSION})/"`
+elif test -f version; then
+  VN=`cat version`
+  VN=`echo $VN | sed -e "s/^\([0-9]*\.[0-9]*\)\.\([0-9]*\).*/\1.ABIv\2($VN)/"`
+else test -d .git;
+  VN="0.0.ABIv0(0.0.0)"
+fi
+
+echo \"$VN\"
diff --git a/build-aux/git-tarname-version-gen b/build-aux/git-tarname-version-gen
new file mode 100755
index 000000000..021cbd61f
--- /dev/null
+++ b/build-aux/git-tarname-version-gen
@@ -0,0 +1,14 @@
+#!/bin/sh
+
+## serial 1
+
+if test "${VERSION}" != ""; then
+  VN=`echo ${VERSION} | sed -e "s/^\([0-9]*\.[0-9]*\)\.\([0-9]*\).*/dovecot-\1-pigeonhole/"`
+elif test -f version; then
+  VN=`cat version`
+  VN=`echo $VN | sed -e "s/^\([0-9]*\.[0-9]*\)\.\([0-9]*\).*/dovecot-\1-pigeonhole/"`
+else
+  VN='dovecot-0.0-pigeonhole'
+fi
+
+echo $VN
diff --git a/build-aux/git-version-gen b/build-aux/git-version-gen
new file mode 100755
index 000000000..ab736fa37
--- /dev/null
+++ b/build-aux/git-version-gen
@@ -0,0 +1,18 @@
+#!/bin/sh
+
+## serial 1
+
+if test "${VERSION}" != ""; then
+  VN="${VERSION}"
+elif test -s version; then
+  VN=`cat version`
+elif test -d .git; then
+ VN="0.0.0-`git rev-list --count HEAD`+`git describe --always`"
+ git update-index -q --refresh
+ test x = x"`git diff-index --name-only HEAD --`" || VN="${VN}-dirty"
+else
+  echo "Cannot determine version number">&2
+  exit 1
+fi
+
+echo $VN
diff --git a/configure.ac b/configure.ac
index 1aba7788a..d8ba11cac 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,8 +1,17 @@
+m4_define([pigeonhole_VERSION],
+    m4_esyscmd_s([build-aux/git-version-gen]))))
+
+m4_define([pigeonhole_ABI_VERSION],
+    m4_esyscmd_s([build-aux/git-abi-version-gen]))))
+
+m4_define([pigeonhole_TAR_VERSION],
+    m4_esyscmd_s([build-aux/git-tarname-version-gen]))))
+
 AC_PREREQ([2.59])
 
 # Be sure to update ABI version also if anything changes that might require
 # recompiling plugins. Most importantly that means if any structs are changed.
-AC_INIT([Pigeonhole], [0.6.devel], [dovecot@dovecot.org], [dovecot-2.4-pigeonhole])
+AC_INIT([Pigeonhole], m4_defn([pigeonhole_VERSION]), [dovecot@dovecot.org], m4_defn([pigeonhole_TAR_VERSION]))
 AC_DEFINE_UNQUOTED([PIGEONHOLE_ABI_VERSION], "0.6.ABIv0($PACKAGE_VERSION)", [Pigeonhole ABI version])
 
 AC_CONFIG_SRCDIR([src])
diff --git a/version.in b/version.in
new file mode 100644
index 000000000..a24f9877a
--- /dev/null
+++ b/version.in
@@ -0,0 +1 @@
+@PACKAGE_VERSION@
-- 
GitLab