From 5d8084daa7812931b53d65312b2cebf9a8453e49 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Leonardo=20Hern=C3=A1ndez=20Hern=C3=A1ndez?=
 <leohdz172@protonmail.com>
Date: Mon, 9 May 2022 14:52:03 -0500
Subject: add flag to print version and exit

---
 Makefile            |  2 +-
 config.mk           |  3 +++
 dwl.c               |  4 +++-
 generate-version.sh | 13 +++++++++++++
 4 files changed, 20 insertions(+), 2 deletions(-)
 create mode 100755 generate-version.sh

diff --git a/Makefile b/Makefile
index 48a0aa7..c235633 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 include config.mk
 
-CFLAGS += -I. -DWLR_USE_UNSTABLE -std=c99 -pedantic
+CFLAGS += -I. -DWLR_USE_UNSTABLE -std=c99 -pedantic -DVERSION=\"$(VERSION)\"
 
 WAYLAND_PROTOCOLS=$(shell pkg-config --variable=pkgdatadir wayland-protocols)
 WAYLAND_SCANNER=$(shell pkg-config --variable=wayland_scanner wayland-scanner)
diff --git a/config.mk b/config.mk
index 960fc8a..37b4114 100644
--- a/config.mk
+++ b/config.mk
@@ -1,3 +1,6 @@
+_VERSION = 0.3.1
+VERSION = $(shell ./generate-version.sh $(_VERSION))
+
 # paths
 PREFIX = /usr/local
 MANDIR = $(PREFIX)/share/man
diff --git a/dwl.c b/dwl.c
index 74b11a8..48eb1a3 100644
--- a/dwl.c
+++ b/dwl.c
@@ -2516,9 +2516,11 @@ main(int argc, char *argv[])
 	char *startup_cmd = NULL;
 	int c;
 
-	while ((c = getopt(argc, argv, "s:h")) != -1) {
+	while ((c = getopt(argc, argv, "s:hv")) != -1) {
 		if (c == 's')
 			startup_cmd = optarg;
+		else if (c == 'v')
+			die("dwl " VERSION);
 		else
 			goto usage;
 	}
diff --git a/generate-version.sh b/generate-version.sh
new file mode 100755
index 0000000..cf408e1
--- /dev/null
+++ b/generate-version.sh
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+if git tag --contains HEAD | grep -q $1; then
+	echo $1
+else
+	branch="$(git rev-parse --abbrev-ref HEAD)"
+	commit="$(git rev-parse --short HEAD)"
+	if [ "${branch}" != "main" ]; then
+		echo $1-$branch-$commit
+	else
+		echo $1-$commit
+	fi
+fi
-- 
cgit v1.2.3