Install systemd services with autotools

Change-Id: I015ac3fbe5adc551b76bac5c15ad93d30d0d2b6e
diff --git a/Makefile.am b/Makefile.am
index 2f0a786..60a5b4e 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -13,11 +13,15 @@
 	include \
 	src \
 	tests \
+	contrib \
 	$(NULL)
 
 BUILT_SOURCES = $(top_srcdir)/.version
 EXTRA_DIST = git-version-gen osmoappdesc.py .version
 
+DISTCHECK_CONFIGURE_FLAGS = \
+	--with-systemdsystemunitdir=$$dc_install_base/$(systemdsystemunitdir)
+
 @RELMAKE@
 
 $(top_srcdir)/.version:
diff --git a/configure.ac b/configure.ac
index 54d6a83..c6fa9a1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -157,6 +157,22 @@
 AC_MSG_RESULT([$enable_ext_tests])
 AM_CONDITIONAL(ENABLE_EXT_TESTS, test "x$enable_ext_tests" = "xyes")
 
+# https://www.freedesktop.org/software/systemd/man/daemon.html
+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"])
+
 AC_MSG_RESULT([CFLAGS="$CFLAGS"])
 AC_MSG_RESULT([CPPFLAGS="$CPPFLAGS"])
 
@@ -183,4 +199,6 @@
     tests/handover/Makefile
     doc/Makefile
     doc/examples/Makefile
+    contrib/Makefile
+    contrib/systemd/Makefile
     Makefile)
diff --git a/contrib/Makefile.am b/contrib/Makefile.am
new file mode 100644
index 0000000..3439c97
--- /dev/null
+++ b/contrib/Makefile.am
@@ -0,0 +1 @@
+SUBDIRS = systemd
diff --git a/contrib/systemd/Makefile.am b/contrib/systemd/Makefile.am
new file mode 100644
index 0000000..5119adf
--- /dev/null
+++ b/contrib/systemd/Makefile.am
@@ -0,0 +1,5 @@
+if HAVE_SYSTEMD
+EXTRA_DIST = osmo-bsc.service
+systemdsystemunit_DATA = \
+  osmo-bsc.service
+endif
diff --git a/debian/osmo-bsc.install b/debian/osmo-bsc.install
index 5dc1e0f..651ddb3 100644
--- a/debian/osmo-bsc.install
+++ b/debian/osmo-bsc.install
@@ -1,3 +1,4 @@
+lib/systemd/system/osmo-bsc.service
 usr/bin/osmo-bsc
 usr/share/doc/osmo-bsc/examples/osmo-bsc/osmo-bsc_custom-sccp.cfg usr/share/doc/osmo-bsc/examples
 usr/share/doc/osmo-bsc/examples/osmo-bsc/osmo-bsc.cfg usr/share/doc/osmo-bsc/examples
diff --git a/debian/osmo-bsc.service b/debian/osmo-bsc.service
deleted file mode 120000
index ce03af1..0000000
--- a/debian/osmo-bsc.service
+++ /dev/null
@@ -1 +0,0 @@
-../contrib/systemd/osmo-bsc.service
\ No newline at end of file
diff --git a/debian/rules b/debian/rules
index 71ffbf1..ffc99db 100755
--- a/debian/rules
+++ b/debian/rules
@@ -42,8 +42,10 @@
 
 # main packaging script based on dh7 syntax
 %:
-	dh $@ --with autoreconf 
+	dh $@ --with autoreconf
 
+# debmake generated override targets
+CONFIGURE_FLAGS += --with-systemdsystemunitdir=/lib/systemd/system
 override_dh_auto_configure:
 	dh_auto_configure -- $(CONFIGURE_FLAGS)
 #