From 3d50ad8b7f5ef0eb9dad1918e1c7a46217e9a525 Mon Sep 17 00:00:00 2001
From: Jim Fehlig <jfehlig@suse.com>
Date: Thu, 27 Apr 2017 09:52:02 -0600
Subject: [PATCH 2/2] Add systemd service file

vhostmd is long overdue for a systemd service file. Add one, and
while at it support specifying the type of init system in the
configure script.

Signed-off-by: Jim Fehlig <jfehlig@suse.com>
---
 Makefile.am     | 36 +++++++++++++++++++++++++++++++++---
 configure.ac    | 24 +++++++++++++++++++++++-
 vhostmd.service | 13 +++++++++++++
 3 files changed, 69 insertions(+), 4 deletions(-)

Index: vhostmd-0.4/Makefile.am
===================================================================
--- vhostmd-0.4.orig/Makefile.am
+++ vhostmd-0.4/Makefile.am
@@ -7,11 +7,41 @@ EXTRA_DIST = $(doc_DATA) vhostmd.init vh
 
 AUTOMAKE_OPTIONS=dist-bzip2
 
-install-data-local:
+install-data-local: install-init-systemv install-init-systemd
 	$(MKDIR_P) $(DESTDIR)/etc/vhostmd
-	$(MKDIR_P) $(DESTDIR)/etc/init.d
-	$(MKDIR_P) $(DESTDIR)/usr/sbin
 	$(INSTALL_DATA) $(srcdir)/vhostmd.xml $(DESTDIR)/etc/vhostmd/vhostmd.conf
 	$(INSTALL_DATA) $(srcdir)/vhostmd.dtd $(DESTDIR)/etc/vhostmd
 	$(INSTALL_DATA) $(srcdir)/metric.dtd $(DESTDIR)/etc/vhostmd
+
+uninstall-local: uninstall-init-systemv uninstall-init-systemd
+	rm -f $(DESTDIR)/etc/vhostmd/vhostmd.conf
+	rm -f $(DESTDIR)/etc/vhostmd/vhostmd.dtd
+	rm -f $(DESTDIR)/etc/vhostmd/metric.dtd
+	rmdir $(DESTDIR)/etc/vhostmd || :
+
+if INIT_SCRIPT_SYSTEMV
+install-init-systemv: $(srcdir)/vhostmd.init
+	$(MKDIR_P) $(DESTDIR)$(sysconfdir)/init.d
 	$(INSTALL_SCRIPT) $(srcdir)/vhostmd.init $(DESTDIR)/etc/init.d/vhostmd
+
+uninstall-init-systemv:
+	rm -f $(DESTDIR)$(sysconfdir)/init.d/vhostmd
+	rmdir $(DESTDIR)$(sysconfdir)/init.d || :
+else ! INIT_SCRIPT_SYSTEMV
+install-init-systemv:
+uninstall-init-systemv:
+endif ! INIT_SCRIPT_SYSTEMV
+
+if INIT_SCRIPT_SYSTEMD
+SYSTEMD_UNIT_DIR = $(prefix)/lib/systemd/system
+install-init-systemd: $(srcdir)/vhostmd.service
+	$(MKDIR_P) $(DESTDIR)$(SYSTEMD_UNIT_DIR)
+	$(INSTALL_DATA) $(srcdir)/vhostmd.service $(DESTDIR)$(SYSTEMD_UNIT_DIR)/vhostmd.service
+
+uninstall-init-systemd:
+	rm -f $(DESTDIR)$(SYSTEMD_UNIT_DIR)/vhostmd.service
+	rmdir $(DESTDIR)$(SYSTEMD_UNIT_DIR) || :
+else ! INIT_SCRIPT_SYSTEMD
+install-init-systemd:
+uninstall-init-systemd:
+endif ! INIT_SCRIPT_SYSTEMD
Index: vhostmd-0.4/configure.ac
===================================================================
--- vhostmd-0.4.orig/configure.ac
+++ vhostmd-0.4/configure.ac
@@ -1,4 +1,4 @@
-AC_INIT(Virtual Host Metrics Daemon, 0.4, jfehlig@novell.com, vhostmd)
+AC_INIT(Virtual Host Metrics Daemon, 0.4, jfehlig@suse.com, vhostmd)
 
 AC_CONFIG_SRCDIR(vhostmd/vhostmd.c)
 AC_CONFIG_HEADERS([config.h])
@@ -81,6 +81,30 @@ AC_ARG_WITH([xenstore],
   esac],[with_xenstore=true]) 
 AM_CONDITIONAL(WITH_XENSTORE, test x$with_xenstore = xtrue)
 
+# Configure argument to support type of init system
+AC_ARG_WITH([init_script],
+  [AS_HELP_STRING([--with-init-script],
+    [Type of init script to install: systemv, systemd, check @<:@default=check@:>@])],
+  [],
+  [with_init_script=check])
+init_systemv=no
+init_systemd=no
+if test "$with_init_script" = check; then
+   if type systemctl >/dev/null 2>&1; then
+      init_systemd=yes
+   else
+      init_systemv=yes
+   fi
+fi
+case "${with_init_script}" in
+   systemv) init_systemv=yes;;
+   systemd) init_systemd=yes;;
+   check) ;;
+   *) AC_MSG_ERROR([Unknown initscript type $with_init_script]);;
+esac
+AM_CONDITIONAL([INIT_SCRIPT_SYSTEMV], test "$init_systemv" = "yes")
+AM_CONDITIONAL([INIT_SCRIPT_SYSTEMD], test "$init_systemd" = "yes")
+
 AC_OUTPUT(vhostmd/Makefile
           include/Makefile
           libmetrics/Makefile
Index: vhostmd-0.4/vhostmd.service
===================================================================
--- /dev/null
+++ vhostmd-0.4/vhostmd.service
@@ -0,0 +1,13 @@
+[Unit]
+Description=Daemon for collecting virtualization host metrics
+After=libvirtd.service
+Documentation=man:vhostmd(8)
+
+[Service]
+Type=forking
+ExecStart=/usr/sbin/vhostmd
+ExecReload=/bin/kill -HUP $MAINPID
+ExecStop=/bin/kill -TERM $MAINPID
+
+[Install]
+WantedBy=multi-user.target
