VOL-1761: Support for getting openolt and bal_core_dist version at agent

Change-Id: Ia059fd5a27005c87e7e1f0e91315b94f4d53a537
diff --git a/agent/Makefile.in b/agent/Makefile.in
index 29bdaf3..b68b15e 100644
--- a/agent/Makefile.in
+++ b/agent/Makefile.in
@@ -54,13 +54,25 @@
 HOST_SYSTEM = $(shell uname | cut -f 1 -d_)
 SYSTEM ?= $(HOST_SYSTEM)
 
+VERSION                 ?= $(shell cat ../VERSION)
+
+#set ref and commit date only if committed
+LABEL_VCS_URL       ?= $(shell git remote get-url $(shell git remote))
+LABEL_VCS_REF       = $(shell git rev-parse HEAD)
+LABEL_BUILD_DATE    ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
+LABEL_COMMIT_DATE   = $(shell git show -s --format=%cd --date=iso-strict HEAD)
+
 LIBGRPC_PATH=$(shell pkg-config --libs-only-L grpc | sed s/-L// | sed s/\ //g)
 LIBPROTOBUF_PATH=$(shell PKG_CONFIG_ALLOW_SYSTEM_LIBS=true pkg-config --libs-only-L protobuf | sed s/-L// | sed s/\ //g)
 
 CXX = @CXX@
 CXXFLAGS += @CXXFLAGS@
 CXXFLAGS += $(shell pkg-config --cflags-only-I grpc++)
+CPPFLAGS += @CPPFLAGS@
 CPPFLAGS += `pkg-config --cflags protobuf grpc`
+CPPFLAGS += -DVERSION=\"$(VERSION)\" -DBAL_VER=\"$(BAL_VER)\" -DLABEL_VCS_URL=\"$(LABEL_VCS_URL)\" \
+            -DLABEL_VCS_REF=\"$(LABEL_VCS_REF)\" -DLABEL_BUILD_DATE=\"$(LABEL_BUILD_DATE)\" \
+            -DLABEL_COMMIT_DATE=\"$(LABEL_COMMIT_DATE)\"
 CXXFLAGS += -std=c++11 -fpermissive -Wno-literal-suffix
 LDFLAGS += @LDFLAGS@
 LDFLAGS += `pkg-config --libs protobuf grpc++ grpc` -ldl -lgpr
@@ -69,8 +81,9 @@
 export CXX CXXFLAGS
 
 prereq:
-	sudo apt-get -q -y install git pkg-config build-essential autoconf libtool libgflags-dev libgtest-dev clang libc++-dev unzip docker.io
+	sudo apt-get -q -y install pkg-config build-essential autoconf libtool libgflags-dev libgtest-dev clang libc++-dev unzip docker.io
 	sudo apt-get install -y build-essential autoconf libssl-dev gawk debhelper dh-systemd init-system-helpers
+	sudo add-apt-repository -y ppa:git-core/ppa && apt-get -y update && apt-get -y install git
 
 	# Install GRPC, libprotobuf and protoc
 	rm -rf $(GRPC_DST)
@@ -198,7 +211,7 @@
 ##
 ##
 common/%.o: common/%.cc
-	$(CXX) $(CXXFLAGS) -I./common -c $< -o $@
+	$(CXX) $(CXXFLAGS) $(CPPFLAGS) -I./common -c $< -o $@
 
 ########################################################################
 ##
@@ -248,7 +261,7 @@
 	ln -sf $(LIBGRPC_PATH)/libgrpc.so.6 $(BUILD_DIR)/libgrpc.so.6
 	ln -sf $(LIBGRPC_PATH)/libgrpc++.so.1 $(BUILD_DIR)/libgrpc++.so.1
 src/%.o: src/%.cc
-	$(CXX) $(CXXFLAGS) $(CXXFLAGSDEVICE) -I./common -c $< -o $@
+	$(CXX) $(CXXFLAGS) $(CXXFLAGSDEVICE) $(CPPFLAGS) -I./common -c $< -o $@
 
 deb:
 	cp $(BUILD_DIR)/release_$(OPENOLTDEVICE)_V$(BAL_MAJOR_VER).$(DEV_VER).tar.gz mkdebian/debian
diff --git a/agent/common/main.cc b/agent/common/main.cc
index b211cfb..c4a0b53 100644
--- a/agent/common/main.cc
+++ b/agent/common/main.cc
@@ -20,8 +20,65 @@
 #include "server.h"
 #include "core.h"
 
+using namespace std;
+
+/*
+*   This function displays openolt version, BAL version, openolt build date
+*   and other VCS params like VCS url, VCS ref, commit date and exits.
+*
+*   @param argc : number of arguments
+*   @param argv : vector of arguments
+*/
+void display_version_info(int argc, char *argv[]) {
+
+    string version = "";
+    string bal_version = "";
+    string label_vcs_url = "";
+    string label_vcs_ref = "";
+    string label_build_date = "";
+    string label_commit_date = "";
+
+    #ifdef VERSION
+        version = VERSION;
+    #endif
+
+    #ifdef BAL_VER
+        bal_version = BAL_VER;
+    #endif
+
+    #ifdef LABEL_VCS_URL
+        label_vcs_url = LABEL_VCS_URL;
+    #endif
+
+    #ifdef LABEL_VCS_REF
+        label_vcs_ref = LABEL_VCS_REF;
+    #endif
+
+    #ifdef LABEL_BUILD_DATE
+       label_build_date = LABEL_BUILD_DATE;
+    #endif
+
+    #ifdef LABEL_COMMIT_DATE
+       label_commit_date = LABEL_COMMIT_DATE;
+    #endif
+
+    for (int i = 1; i < argc; ++i) {
+        if(strcmp(argv[i], "--version") == 0 || (strcmp(argv[i], "-v") == 0)) {
+           std::cout << "OpenOLT agent: " << version << "\n";
+           std::cout << "BAL version: " << bal_version << "\n";
+           std::cout << "Label VCS Url: " << label_vcs_url << "\n";
+           std::cout << "Label VCS Ref: " << label_vcs_ref << "\n";
+           std::cout << "Label build date: " << label_build_date << "\n";
+           std::cout << "Label commit date: " << label_commit_date << "\n";
+           exit(0);
+        }
+    }
+}
+
 int main(int argc, char** argv) {
 
+    display_version_info(argc, argv);
+
     Status status = Enable_(argc, argv);
     if (!status.ok()) {
         std::cout << "ERROR: Enable_ failed - "