Index: Makefile
===================================================================
--- Makefile	(revision 7e90c032377bbc2105caa071dfdf55c678359d4f)
+++ Makefile	(revision 8ca172fcfb1f60f9933a3933fdd809699ce466ce)
@@ -33,5 +33,7 @@
 	@echo Installed successfully
 	@echo
+ifndef SYSTEMDSYSTEMUNITDIR
 	@if ! [ -d $(DESTDIR)$(CONFIG) ]; then echo -e '\nThe configuration directory $(DESTDIR)$(CONFIG) does not exist yet, don'\''t forget to create it!'; fi
+endif
 	@if ! [ -e $(DESTDIR)$(ETCDIR)/bitlbee.conf ]; then echo -e '\nNo files are installed in '$(DESTDIR)$(ETCDIR)' by make install. Run make install-etc to do that.'; fi
 ifdef SYSTEMDSYSTEMUNITDIR
@@ -144,4 +146,10 @@
 endif
 
+ifdef SYSUSERSDIR
+	$(INSTALL) -Dm644 init/bitlbee.sysusers $(DESTDIR)/$(SYSUSERSDIR)/bitlbee.conf
+else
+	@echo SYSUSERSDIR not set, not install sysusers.d files
+endif
+
 tar:
 	fakeroot debian/rules clean || make distclean
Index: configure
===================================================================
--- configure	(revision 7e90c032377bbc2105caa071dfdf55c678359d4f)
+++ configure	(revision 8ca172fcfb1f60f9933a3933fdd809699ce466ce)
@@ -19,4 +19,5 @@
 includedir='$prefix/include/bitlbee/'
 systemdsystemunitdir=''
+sysusersdir=''
 libevent='/usr/'
 pidfile='/var/run/bitlbee.pid'
@@ -55,4 +56,6 @@
 
 pie=1
+
+systemd=auto
 
 arch=$(uname -s)
@@ -129,4 +132,5 @@
 --plugindir=...						$plugindir
 --systemdsystemunitdir=...				$systemdsystemunitdir
+--sysusersdir                       $sysusersdir
 --pidfile=...						$pidfile
 --config=...						$config
@@ -157,4 +161,5 @@
 							$ssl
 --external_json_parser=0/1/auto	Use External JSON parser $external_json_parser
+--systemd=0/1  Enable/disable systemd support $systemd
 
 
@@ -750,13 +755,37 @@
 fi
 
-if [ -z "$systemdsystemunitdir" ]; then
-	if $PKG_CONFIG --exists systemd; then
-		systemdsystemunitdir=$($PKG_CONFIG --variable=systemdsystemunitdir systemd)
-	fi
-fi
-if [ -n "$systemdsystemunitdir" ]; then
-	if [ "$systemdsystemunitdir" != "no" ]; then
-		echo "SYSTEMDSYSTEMUNITDIR=$systemdsystemunitdir" >> Makefile.settings
-	fi
+if [ ! "$systemd" = 0 ]; then
+    if ! $PKG_CONFIG --exists systemd ; then
+        if [ -n "$systemdsystemunitdir" ] || [ "$systemd" -eq 1 ]; then
+            echo "systemd requested but not found"
+            exit 1
+        else
+            systemd=0
+        fi
+    fi
+fi
+
+pkgconf_systemd_var() {
+    # First try deprecated variable, use newer variable if not found
+    if $PKG_CONFIG --print-variables systemd | grep -q $1 ; then
+        $PKG_CONFIG --variable=$1 systemd
+    else
+        $PKG_CONFIG --variable=$2 systemd
+    fi
+}
+
+if [ "$systemd" -eq 1 ]; then
+	if [ -z "$systemdsystemunitdir" ]; then
+		systemdsystemunitdir=$(pkgconf_systemd_var systemdsystemunitdir systemd_system_unit_dir)
+	fi
+    if [ -z "$tmpfiles_dir" ] ; then
+        tmpfilesdir=$(pkgconf_systemd_var tmpfilesdir tmpfiles_dir)
+    fi
+    if [ -z "$sysusersdir" ] ; then
+        sysusersdir=$(pkgconf_systemd_var sysusersdir sysusers_dir)
+    fi
+
+	echo "SYSTEMDSYSTEMUNITDIR=$systemdsystemunitdir" >> Makefile.settings
+    echo "SYSUSERSDIR=$sysusersdir" >> Makefile.settings
 fi
 
@@ -1005,5 +1034,5 @@
 fi
 
-if [ -n "$systemdsystemunitdir" ]; then
+if [ "$systemd" -eq 1 ]; then
 	echo '  systemd enabled.'
 else
Index: init/bitlbee.service.in
===================================================================
--- init/bitlbee.service.in	(revision 7e90c032377bbc2105caa071dfdf55c678359d4f)
+++ init/bitlbee.service.in	(revision 8ca172fcfb1f60f9933a3933fdd809699ce466ce)
@@ -5,4 +5,7 @@
 ExecStart=@sbindir@bitlbee -F -n
 KillMode=process
+StateDirectory=bitlbee
+RuntimeDirectory=bitlbee
+User=bitlbee
 
 [Install]
Index: init/bitlbee.socket
===================================================================
--- init/bitlbee.socket	(revision 7e90c032377bbc2105caa071dfdf55c678359d4f)
+++ init/bitlbee.socket	(revision 8ca172fcfb1f60f9933a3933fdd809699ce466ce)
@@ -4,4 +4,5 @@
 [Socket]
 ListenStream=6667
+ListenStream=%t/bitlbee/socket
 BindToDevice=lo
 Accept=yes
Index: init/bitlbee.sysusers
===================================================================
--- init/bitlbee.sysusers	(revision 8ca172fcfb1f60f9933a3933fdd809699ce466ce)
+++ init/bitlbee.sysusers	(revision 8ca172fcfb1f60f9933a3933fdd809699ce466ce)
@@ -0,0 +1,2 @@
+#Type Name       ID                  GECOS                    Home directory Shell
+u     bitlbee    -  "BitlBee user"                            /var/lib/bitlbee - 
Index: init/bitlbee@.service.in
===================================================================
--- init/bitlbee@.service.in	(revision 7e90c032377bbc2105caa071dfdf55c678359d4f)
+++ init/bitlbee@.service.in	(revision 8ca172fcfb1f60f9933a3933fdd809699ce466ce)
@@ -7,2 +7,4 @@
 StandardError=syslog
 User=bitlbee
+StateDirectory=bitlbee
+RuntimeDirectory=bitlbee
