Autogenerate integration files via automake

-Creates systemd unit file;
-Creates phosphor-debug-collector (dreport) plugin.

Change-Id: I5d4d2c7452c5b9c042c27170c67e282f34882d11
Signed-off-by: Artem Senichev <artemsen@gmail.com>
diff --git a/Makefile.am b/Makefile.am
index 2197ef3..649cab6 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -23,9 +23,45 @@
 hostlogger_CXXFLAGS = $(SDBUSPLUS_CFLAGS) $(PHOSPHOR_DBUS_INTERFACES_CFLAGS)
 hostlogger_LDADD = $(SDBUSPLUS_LIBS) $(PHOSPHOR_DBUS_INTERFACES_LIBS)
 
+if DEBUG
+hostlogger_CXXFLAGS += -DDEBUG -g3 -O0
+else
+hostlogger_CXXFLAGS += -DNDEBUG
+endif  # DEBUG
+
 BUILT_SOURCES = $(GENERATED_HPP) $(GENERATED_CPP)
 CLEANFILES = $(BUILT_SOURCES)
 
+if HAVE_SYSTEMD
+SYSTEMD_UNIT = hostlogger.service
+systemdsystemunit_DATA = $(SYSTEMD_UNIT)
+CLEANFILES += $(SYSTEMD_UNIT)
+endif  # HAVE_SYSTEMD
+
+if HAVE_DREPORT
+DREPORT_SHARE = $(datarootdir)/dreport.d
+CLEANFILES += hostlogger.dreport
+
+install-data-hook:
+	install -d $(DESTDIR)$(DREPORT_SHARE)/plugins.d
+	install -m 0755 hostlogger.dreport $(DESTDIR)$(DREPORT_SHARE)/plugins.d/hostlogger
+	install -d $(DESTDIR)$(DREPORT_SHARE)/pl_core.d
+	ln -sr $(DESTDIR)$(DREPORT_SHARE)/plugins.d/hostlogger \
+		$(DESTDIR)$(DREPORT_SHARE)/pl_core.d/hostlogger
+	install -d $(DESTDIR)$(DREPORT_SHARE)/pl_elog.d
+	ln -sr $(DESTDIR)$(DREPORT_SHARE)/plugins.d/hostlogger \
+		$(DESTDIR)$(DREPORT_SHARE)/pl_elog.d/hostlogger
+	install -d $(DESTDIR)$(DREPORT_SHARE)/pl_user.d
+	ln -sr $(DESTDIR)$(DREPORT_SHARE)/plugins.d/hostlogger \
+		$(DESTDIR)$(DREPORT_SHARE)/pl_user.d/hostlogger
+
+uninstall-hook:
+	rm -f $(DESTDIR)$(DREPORT_SHARE)/plugins.d/hostlogger \
+		$(DESTDIR)$(DREPORT_SHARE)/pl_core.d/hostlogger \
+		$(DESTDIR)$(DREPORT_SHARE)/pl_elog.d/hostlogger \
+		$(DESTDIR)$(DREPORT_SHARE)/pl_user.d/hostlogger
+endif  # HAVE_DREPORT
+
 $(GENERATED_HPP): $(YAML_IFACE)
 	@mkdir -p $(@D)
 	$(SDBUSPLUSPLUS) -r $(srcdir) interface server-header $(DBUS_IFACE) > $@
diff --git a/README.md b/README.md
index 4a1f01f..e5fec90 100644
--- a/README.md
+++ b/README.md
@@ -11,7 +11,7 @@
 ```
 +-------------+                                       +----------------+
 |     Host    | State  +---------------------+ Event  |  Host Logger   |
-|             +------->|        D-Bus        |------->|                |
+|             |------->|        D-Bus        |------->|                |
 |             |        +---------------------+        | +------------+ |
 |             |                                  D +--->| Log buffer | |
 |             |        +---------------------+   a |  | +------+-----+ |
@@ -65,16 +65,8 @@
 
 ### File rotation policy
 - `--rotate`: Set the maximum number of files stored on the flash memory.
-  Default value is `10`. The service will remove the oldest files during buffer
-  the flush procedure.
-
-
-## Integration
-Directory "integration" contains example files used to integrate the service
-into OpenBMC system:
-- `systemd.service`: systemd unit file used to describe the service;
-- `dreport.plugin`: plugin for dreport module, used to add host's logs into
-  a dump archive.
+  Default value is `10`. The service will remove the oldest files during the
+  buffer flush procedure.
 
 ## D-Bus
 The service provides an implementation of a D-Bus interface
diff --git a/configure.ac b/configure.ac
index caf360a..6c9f93c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,8 +1,7 @@
 AC_PREREQ([2.69])
 AC_INIT([phosphor-hostlogger],
         [m4_esyscmd_s([git describe --always --dirty --long])],
-        [https://github.com/YADRO-KNS/phosphor-hostlogger], ,
-        [https://github.com/YADRO-KNS/phosphor-hostlogger])
+        [https://github.com/openbmc/phosphor-hostlogger/issues])
 AC_LANG([C++])
 AM_INIT_AUTOMAKE([subdir-objects -Wall -Werror foreign])
 AM_SILENT_RULES([yes])
@@ -16,52 +15,92 @@
 
 # Compiler flags
 AX_CXX_COMPILE_STDCXX([17], [noext])
-AX_APPEND_COMPILE_FLAGS([-fpic -Wall], [CFLAGS])
 AX_APPEND_COMPILE_FLAGS([-fpic -Wall], [CXXFLAGS])
 
 # Checks for external dependencies
-AC_CHECK_LIB([z], [gzopen], [], [AC_MSG_ERROR(zlib not found)])
-PKG_CHECK_MODULES([SYSTEMD], [libsystemd >= 221], ,
-    AC_MSG_ERROR(["systemd required but not found."]))
-PKG_CHECK_MODULES([PHOSPHOR_DBUS_INTERFACES], [phosphor-dbus-interfaces], ,
-    AC_MSG_ERROR(["Requires phosphor-dbus-interfaces package."]))
-PKG_CHECK_MODULES([SDBUSPLUS], [sdbusplus], ,
-    AC_MSG_ERROR(["Requires sdbusplus package."]))
+AC_CHECK_LIB([z], [gzopen], [],
+             [AC_MSG_ERROR([zlib required but not found])])
+PKG_CHECK_MODULES([SYSTEMD], [libsystemd >= 221], [],
+                  [AC_MSG_ERROR([systemd required but not found])])
+PKG_CHECK_MODULES([PHOSPHOR_DBUS_INTERFACES], [phosphor-dbus-interfaces], [],
+                  [AC_MSG_ERROR([phosphor-dbus-interfaces required but not found])])
+PKG_CHECK_MODULES([SDBUSPLUS], [sdbusplus], [],
+                  [AC_MSG_ERROR([sdbusplus required but not found])])
 AC_PATH_PROG([SDBUSPLUSPLUS], [sdbus++])
-AS_IF([test "x$SDBUSPLUSPLUS" == "x"],
-    AC_MSG_ERROR(["Requires sdbus++"]))
+AS_IF([test "x$SDBUSPLUSPLUS" = "x"],
+      [AC_MSG_ERROR([sdbus++ required but not found])])
+
+# systemd unit general configuration
+PKG_PROG_PKG_CONFIG
+AC_ARG_WITH([systemdsystemunitdir],
+            [AS_HELP_STRING([--with-systemdsystemunitdir=DIR],
+                            [Directory for systemd service files])], [],
+            [with_systemdsystemunitdir=auto])
+AS_IF([test "x$with_systemdsystemunitdir" = "xyes" -o "x$with_systemdsystemunitdir" = "xauto"],
+      [def_systemdsystemunitdir=$($PKG_CONFIG --variable=systemdsystemunitdir systemd)
+       AS_IF([test "x$def_systemdsystemunitdir" = "x"],
+             [AS_IF([test "x$with_systemdsystemunitdir" = "xyes"],
+                    [AC_MSG_ERROR([systemd support requested but pkg-config unable to query systemd package])],
+                    [with_systemdsystemunitdir=no])],
+             [with_systemdsystemunitdir="$def_systemdsystemunitdir"])])
+AS_IF([test "x$with_systemdsystemunitdir" != "xno"],
+      [AC_SUBST([systemdsystemunitdir], [$with_systemdsystemunitdir])])
+AM_CONDITIONAL([HAVE_SYSTEMD], [test "x$with_systemdsystemunitdir" != "xno"])
+
+# systemd unit parameters
+AC_ARG_VAR([SYSTEMD_TARGET], [systemd target for starting service, default is multi-user.target])
+AS_IF([test "x$SYSTEMD_TARGET" = "x"], [SYSTEMD_TARGET=multi-user.target])
+AC_ARG_VAR([HOST_TTY], [host teletype (TTY), default is ttyVUART0])
+AS_IF([test "x$HOST_TTY" = "x"], [HOST_TTY=ttyVUART0])
 
 # Configuration property: path to the ouput directory of log files
-AC_ARG_VAR([LOG_OUTPUT_PATH], [Path used to store logs.])
-AS_IF([test "x$LOG_OUTPUT_PATH" == "x"], [LOG_OUTPUT_PATH="/var/lib/obmc/hostlogs"])
-AC_DEFINE_UNQUOTED([LOG_OUTPUT_PATH], ["$LOG_OUTPUT_PATH"])
+AC_ARG_VAR([LOG_OUTPUT_PATH], [Path used to store logs, default is /var/lib/obmc/hostlogs])
+AS_IF([test "x$LOG_OUTPUT_PATH" = "x"], [LOG_OUTPUT_PATH="/var/lib/obmc/hostlogs"])
+AC_DEFINE_UNQUOTED([LOG_OUTPUT_PATH], ["${LOG_OUTPUT_PATH}"])
 
 # Configuration property: log storage limit (maximum number of messages to store)
 AC_ARG_VAR([LOG_STORAGE_SIZE_LIMIT], [Limit number of messages in store, default is 3000])
-AS_IF([test "x$LOG_STORAGE_SIZE_LIMIT" == "x"], [LOG_STORAGE_SIZE_LIMIT=3000])
+AS_IF([test "x$LOG_STORAGE_SIZE_LIMIT" = "x"], [LOG_STORAGE_SIZE_LIMIT=3000])
 AC_DEFINE_UNQUOTED([LOG_STORAGE_SIZE_LIMIT], [${LOG_STORAGE_SIZE_LIMIT}])
 
 # Configuration property: log storage limit (oldest time in hours)
 AC_ARG_VAR([LOG_STORAGE_TIME_LIMIT], [Limit message store by oldest time in hours, default is 0])
-AS_IF([test "x$LOG_STORAGE_TIME_LIMIT" == "x"], [LOG_STORAGE_TIME_LIMIT=0])
+AS_IF([test "x$LOG_STORAGE_TIME_LIMIT" = "x"], [LOG_STORAGE_TIME_LIMIT=0])
 AC_DEFINE_UNQUOTED([LOG_STORAGE_TIME_LIMIT], [${LOG_STORAGE_TIME_LIMIT}])
 
 # Configuration property: flush period policy.
 AC_ARG_VAR([LOG_FLUSH_PERIOD], [Set the default flush period time, default is 0])
-AS_IF([test "x$LOG_FLUSH_PERIOD" == "x"], [LOG_FLUSH_PERIOD=0])
+AS_IF([test "x$LOG_FLUSH_PERIOD" = "x"], [LOG_FLUSH_PERIOD=0])
 AC_DEFINE_UNQUOTED([LOG_FLUSH_PERIOD], [${LOG_FLUSH_PERIOD}])
 
 # Configuration property: limit for log rotation - maximum number of files to store in the log's directory
 AC_ARG_VAR([LOG_ROTATION_LIMIT], [Limit for log rotation, default is 10])
-AS_IF([test "x$LOG_ROTATION_LIMIT" == "x"], [LOG_ROTATION_LIMIT=10])
+AS_IF([test "x$LOG_ROTATION_LIMIT" = "x"], [LOG_ROTATION_LIMIT=10])
 AC_DEFINE_UNQUOTED([LOG_ROTATION_LIMIT], [${LOG_ROTATION_LIMIT}])
 
-# Debug property
-AC_ARG_ENABLE(debug,
-    [AS_HELP_STRING([--enable-debug], [enable debugging [disabled]])],
-    [AM_CPPFLAGS="$AM_CPPFLAGS -DDEBUG -g -O0"]
-    [AM_CPPFLAGS="$AM_CPPFLAGS -DNDEBUG"]
-)
+# dreport integration
+AC_ARG_ENABLE([dreport],
+              [AS_HELP_STRING([--enable-dreport], [enable dreport integration as plugin [yes]])],
+              [case "${enableval}" in
+                 yes) dreport=true ;;
+                 no)  dreport=false ;;
+                 *)   AC_MSG_ERROR([bad value ${enableval} for --enable-dreport]) ;;
+               esac],
+              [dreport=true])
+AM_CONDITIONAL(HAVE_DREPORT, test "x$dreport" = "xtrue")
+
+# Debug build configuration
+AC_ARG_ENABLE([debug],
+              [AS_HELP_STRING([--enable-debug], [turn on debugging [no]])],
+              [case "${enableval}" in
+                 yes) debug=true ;;
+                 no)  debug=false ;;
+                 *)   AC_MSG_ERROR([bad value ${enableval} for --enable-debug]) ;;
+               esac],
+              [debug=false])
+AM_CONDITIONAL(DEBUG, test "x$debug" = "xtrue")
 
 AC_CONFIG_FILES([Makefile])
+AC_CONFIG_FILES([hostlogger.service])
+AC_CONFIG_FILES([hostlogger.dreport])
 AC_OUTPUT
diff --git a/hostlogger.dreport.in b/hostlogger.dreport.in
new file mode 100644
index 0000000..bbbf168
--- /dev/null
+++ b/hostlogger.dreport.in
@@ -0,0 +1,19 @@
+#!/bin/bash
+#
+# config: 123 20
+# @brief: Collect host's logs
+#
+
+. ${DREPORT_INCLUDE}/functions
+
+DESCRIPTION="Host logs"
+LOGS_PATH="@LOG_OUTPUT_PATH@"
+
+if [[ -d ${LOGS_PATH} ]]; then
+  # Flush currently collected messages
+  busctl --no-pager --verbose call \
+    xyz.openbmc_project.HostLogger /xyz/openbmc_project/HostLogger \
+    xyz.openbmc_project.HostLogger Flush
+  # Copy log directory
+  add_copy_file "${LOGS_PATH}" "${DESCRIPTION}"
+fi
diff --git a/hostlogger.service.in b/hostlogger.service.in
new file mode 100644
index 0000000..1f29416
--- /dev/null
+++ b/hostlogger.service.in
@@ -0,0 +1,13 @@
+[Unit]
+Description=Host logging
+Wants=obmc-console@@HOST_TTY@.service
+After=obmc-console@@HOST_TTY@.service
+
+[Service]
+ExecStart=@sbindir@/hostlogger
+Restart=always
+Type=dbus
+BusName=xyz.openbmc_project.HostLogger
+
+[Install]
+WantedBy=@SYSTEMD_TARGET@
diff --git a/integration/dreport.plugin b/integration/dreport.plugin
deleted file mode 100644
index d884785..0000000
--- a/integration/dreport.plugin
+++ /dev/null
@@ -1,17 +0,0 @@
-#!/bin/bash
-#
-# config: 123 20
-# @brief: Collect host's logs
-#
-
-. $DREPORT_INCLUDE/functions
-
-desc="Logs from host"
-file_name="/var/lib/obmc/hostlogs"
-
-# Flush currently collected messages
-busctl --no-pager --verbose call \
-    xyz.openbmc_project.HostLogger /xyz/openbmc_project/HostLogger \
-    xyz.openbmc_project.HostLogger Flush
-
-add_copy_file "$file_name" "$desc"
diff --git a/integration/systemd.service b/integration/systemd.service
deleted file mode 100644
index ac49668..0000000
--- a/integration/systemd.service
+++ /dev/null
@@ -1,13 +0,0 @@
-[Unit]
-Description=Host logging
-Wants=obmc-console@ttyVUART0.service
-After=obmc-console@ttyVUART0.service
-
-[Service]
-ExecStart=/usr/bin/hostlogger
-Restart=always
-Type=dbus
-BusName=xyz.openbmc_project.HostLogger
-
-[Install]
-WantedBy=obmc-standby.target