From 4c83b363bffbcff54d261a83c5987bdc20c4ce29 Mon Sep 17 00:00:00 2001
From: Michael Carlberg <c@rlberg.se>
Date: Tue, 28 Jun 2016 14:50:21 +0200
Subject: [PATCH] feat(build): Build with version

---
 CMakeLists.txt      |  9 ++++++---
 bump.sh             |  9 ++++++++-
 include/version.hpp |  3 +++
 src/lemonbuddy.cpp  | 34 ++++++++++++++++++++++++++++++++--
 version.sh          | 21 +++++++++++++++++++++
 5 files changed, 70 insertions(+), 6 deletions(-)
 create mode 100644 include/version.hpp
 create mode 100755 version.sh

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8d43beee..3f973300 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2,9 +2,7 @@
 # Build configuration
 #
 cmake_minimum_required(VERSION 3.0)
-
-# TODO(jaagr): Replace with git tag
-project(lemonbuddy VERSION 1.2.2)
+project(lemonbuddy)
 
 # Include the local cmake modules
 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
@@ -139,6 +137,11 @@ add_subdirectory("${PROJECT_SOURCE_DIR}/lib/xpp")
 set(PROJECT_INCL_DIRS ${PROJECT_INCL_DIRS} ${XPP_INCLUDE_DIRS})
 set(PROJECT_LINK_LIBS ${PROJECT_LINK_LIBS} ${XPP_LIBRARIES})
 
+#
+# Execute versioning script
+#
+execute_process(COMMAND ./version.sh WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} OUTPUT_QUIET ERROR_QUIET)
+
 #
 # Install executable and wrapper
 #
diff --git a/bump.sh b/bump.sh
index 3fd5839e..132890aa 100755
--- a/bump.sh
+++ b/bump.sh
@@ -1,5 +1,10 @@
 #!/bin/sh
 
+# Use passed argument as new tag
+[ $# -eq 1 ] && git tag "$@"
+
+./version.sh
+
 # shellcheck disable=SC2016
 tag_prev=$(git tag -l | tail -2 | head -1)
 tag_curr=$(git tag -l | tail -1)
@@ -7,4 +12,6 @@ tag_curr=$(git tag -l | tail -1)
 sed -r "s/${tag_prev}/${tag_curr}/g" -i README.md CMakeLists.txt contrib/lemonbuddy.aur/PKGBUILD contrib/lemonbuddy.aur/.SRCINFO
 git add README.md CMakeLists.txt contrib/lemonbuddy.aur/PKGBUILD contrib/lemonbuddy.aur/.SRCINFO
 git commit -m "build: Bump version to ${tag_curr}"
-git show HEAD
+
+# Recreate the tag to include the last commit
+[ $# -eq 1 ] && git tag -f "$@"
diff --git a/include/version.hpp b/include/version.hpp
new file mode 100644
index 00000000..aa4e545d
--- /dev/null
+++ b/include/version.hpp
@@ -0,0 +1,3 @@
+#pragma once
+
+#define GIT_TAG "1.2.2-dev"
diff --git a/src/lemonbuddy.cpp b/src/lemonbuddy.cpp
index c14b2ab1..9444c834 100644
--- a/src/lemonbuddy.cpp
+++ b/src/lemonbuddy.cpp
@@ -3,6 +3,7 @@
 #include <sys/stat.h>
 #include <thread>
 
+#include "version.hpp"
 #include "bar.hpp"
 #include "config.hpp"
 #include "eventloop.hpp"
@@ -59,14 +60,43 @@ int main(int argc, char **argv)
     cli::add_option("-d", "--dump", "PARAM", "Show value of PARAM in section [bar_name]");
     cli::add_option("-x", "--print-exec", "Print the generated command line string used to start the lemonbar process");
     cli::add_option("-w", "--print-wmname", "Print the generated WM_NAME");
+    cli::add_option("-v", "--version", "Print version information");
 
     /**
      * Parse command line arguments
      */
-    if (argc < 2 || cli::is_option(argv[1], "-h", "--help") || argv[1][0] == '-')
+    if (argc < 2 || cli::is_option(argv[1], "-h", "--help"))
       cli::usage(usage, (argc > 1 && cli::is_option(argv[1], "-h", "--help")));
+
     cli::parse(2, argc, argv);
-    if (cli::has_option("help"))
+
+    if (cli::has_option("version") || cli::is_option(argv[1], "-v", "--version")) {
+      std::cout << APP_NAME << " " << GIT_TAG << std::endl;
+
+      if (std::strncmp(argv[1], "-vv", 3) == 0) {
+        std::cout << "\n" << "Built with: "
+          << (ENABLE_ALSA    ? "+" : "-") << "alsa "
+          << (ENABLE_I3      ? "+" : "-") << "i3 "
+          << (ENABLE_MPD     ? "+" : "-") << "mpd "
+          << (ENABLE_NETWORK ? "+" : "-") << "network "
+          << "\n\n";
+        if (ENABLE_ALSA)
+          std::cout
+            << "ALSA_SOUNDCARD        " << ALSA_SOUNDCARD << std::endl;
+        std::cout
+            << "CONNECTION_TEST_IP    " << CONNECTION_TEST_IP << "\n"
+            << "PATH_BACKLIGHT_VAL    " << PATH_BACKLIGHT_VAL << "\n"
+            << "PATH_BACKLIGHT_MAX    " << PATH_BACKLIGHT_MAX << "\n"
+            << "BSPWM_SOCKET_PATH     " << BSPWM_SOCKET_PATH << "\n"
+            << "BSPWM_STATUS_PREFIX   " << BSPWM_STATUS_PREFIX << "\n"
+            << "PATH_CPU_INFO         " << PATH_CPU_INFO << "\n"
+            << "PATH_MEMORY_INFO      " << PATH_MEMORY_INFO << "\n";
+      }
+
+      return EXIT_SUCCESS;
+    }
+
+    if (cli::has_option("help") || argv[1][0] == '-')
       cli::usage(usage);
 
     /**
diff --git a/version.sh b/version.sh
new file mode 100755
index 00000000..74968096
--- /dev/null
+++ b/version.sh
@@ -0,0 +1,21 @@
+#!/bin/sh
+
+msg() {
+  echo " \033[1;32m**\033[0m" "$@"
+}
+
+main() {
+  version="$(git describe --tags --dirty=-dev)"
+
+  msg "Current version: ${version}"
+
+  sed -r "/#define GIT_TAG/s/GIT_TAG .*/GIT_TAG \"${version}\"/" -i include/version.hpp
+
+  if [ "$(git diff include/version.hpp 2>/dev/null)" ]; then
+    msg "Updated include/version.hpp"
+  else
+    msg "<include/version.hpp> is already up-to-date"
+  fi
+}
+
+main "$@"