From bfba732f762e0cafbd5035d7010894f5e09a1a06 Mon Sep 17 00:00:00 2001
From: kytv <kytv@mail.i2p>
Date: Thu, 13 Mar 2014 02:29:41 +0000
Subject: [PATCH] Debian: Add systemd support (ticket #1208)

Currently just for Debian unstable and Ubuntu Saucy and newer.
---
 debian/changelog    |  9 +++++
 debian/control      | 90 +++++++++++++++++++++++----------------------
 debian/i2p.postinst | 21 ++++++++++-
 debian/i2p.service  | 26 +++++++++++++
 debian/rules        | 11 ++++++
 5 files changed, 111 insertions(+), 46 deletions(-)
 create mode 100644 debian/i2p.service

diff --git a/debian/changelog b/debian/changelog
index 0630433829..f02e8d39a4 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,12 @@
+i2p (0.9.11-2) UNRELEASED; urgency=medium
+
+  * systemd support in Ubuntu Saucy (and newer) and Debian unstable (ticket
+    #1208)
+  * Set homepage address to geti2p.net
+  * Bump standards version (no source changes needed)
+
+ -- Kill Your TV <killyourtv@i2pmail.org>  Thu, 13 Mar 2014 01:10:46 +0000
+
 i2p (0.9.11-1) unstable; urgency=medium
 
   * New release
diff --git a/debian/control b/debian/control
index 499d77bf40..7819031879 100644
--- a/debian/control
+++ b/debian/control
@@ -1,20 +1,24 @@
 Source: i2p
 Maintainer: Kill Your TV <killyourtv@i2pmail.org>
 Section: net
-Standards-Version: 3.9.4
+Standards-Version: 3.9.5
 Priority: optional
 Bugs: mailto:killyourtv@i2pmail.org
-Homepage: http://www.i2p2.de/
-Build-Depends: debhelper (>= 7.0.50~),
- ant,
- debconf,
- default-jdk,
- gettext,
- libgmp3-dev,
-# uncomment the next line for PPA builds
-# libservice-wrapper-java,
- libcommons-logging-java,
- po-debconf
+Homepage: https://geti2p.net
+Build-Depends: debhelper (>= 7.0.50~)
+ ,ant (>= 1.8)
+ ,ant-optional
+ ,debconf
+ ,default-jdk | openjdk-7-jdk | openjdk-6-jdk
+ ,gettext
+ ,libgmp3-dev
+# uncomment the next line for official builds
+# ,libservice-wrapper-java
+ ,libcommons-logging-java
+ ,hardening-wrapper
+ ,po-debconf
+# Uncomment the next line for Debian unstable or Ubuntu Saucy (and newer)
+# ,dh-systemd
 
 Package: i2p
 Architecture: all
@@ -49,46 +53,44 @@ Description: Java Big Integer library
  the ones used by I2P. You can expect to see a 5-7x speed improvement on certain
  tasks, such as elGamal encryption/decryption.
 
-Package: i2p-doc
-Architecture: all
-Section: doc
-Depends: ${misc:Depends}
-Suggests: i2p, default-jdk-doc
-Description: Documentation for I2P
- I2P is an anonymizing network, offering a simple layer that identity-sensitive
- applications can use to securely communicate. All data is wrapped with several
- layers of encryption, and the network is both distributed and dynamic, with no
- trusted parties.
- .
- This package contains the Javadoc files.
+#Package: i2p-doc
+#Architecture: all
+#Section: doc
+#Depends: ${misc:Depends}
+#Suggests: i2p, default-jdk-doc
+#Description: Documentation for I2P
+# I2P is an anonymizing network, offering a simple layer that identity-sensitive
+# applications can use to securely communicate. All data is wrapped with several
+# layers of encryption, and the network is both distributed and dynamic, with no
+# trusted parties.
+# .
+# This package contains the Javadoc files.
 
 Package: i2p-router
 Architecture: all
 Section: net
 Priority: optional
 Depends: ${misc:Depends}, ${java:Depends}, ${shlibs:Depends},
- openjdk-6-jre-headless | openjdk-7-jre-headless | default-jre-headless | java5-runtime-headless | java6-runtime-headless, libecj-java
+ openjdk-7-jre-headless | openjdk-6-jre-headless | default-jre-headless | java7-runtime-headless | java6-runtime-headless, libecj-java
 Replaces: i2p ( << 0.8.6-5)
 Breaks: i2p (<< 0.8.6-5)
-Recommends: libjbigi-jni (>= ${source:Version}), libjbigi-jni (<< ${source:Version}.1~), ttf-dejavu
-Suggests: tor,
- i2p-messenger,
- i2phex,
- i2p-tahoe-lafs,
- imule,
- i2pfox,
- irc-client,
- itoopie,
- jircii,
- mail-client,
- mail-reader,
- news-reader,
- polipo,
- privoxy,
- robert,
- syndie,
- www-browser,
- xul-ext-torbutton
+Recommends: libjbigi-jni, ttf-dejavu
+Suggests: tor
+ ,i2p-messenger
+ ,i2phex
+ ,i2p-tahoe-lafs
+ ,imule
+ ,irc-client
+ ,itoopie
+ ,mail-client
+ ,mail-reader
+ ,news-reader
+ ,polipo
+ ,privox
+ ,robert
+ ,syndie
+ ,www-browser
+ ,xul-ext-torbutton
 Description: Load-balanced unspoofable packet switching network
  I2P is an anonymizing network, offering a simple layer that identity-sensitive
  applications can use to securely communicate. All data is wrapped with several
diff --git a/debian/i2p.postinst b/debian/i2p.postinst
index dca27ccd24..1cf9ef8755 100755
--- a/debian/i2p.postinst
+++ b/debian/i2p.postinst
@@ -1,9 +1,12 @@
-#!/bin/sh  -e
+#!/bin/sh
+
+set -e
 
 I2PHOME=/var/lib/i2p
 I2PSYSUSER=i2psvc
 
 conffile="/etc/default/i2p"
+systemdservice="/lib/systemd/system/i2p.service"
 
 # Source debconf library -- we have a Depends line
 # to make sure it is there...
@@ -57,6 +60,20 @@ case "$1" in
             < $conffile > $conffile.tmp
         mv -f $conffile.tmp $conffile
 
+        if [ -e "$systemdservice" ]; then
+            sed -e "s/User=.*/User=$I2PUSER/" < "$systemdservice" > "$systemdservice.tmp"
+            mv -f "$systemdservice.tmp" "$systemdservice"
+            chmod 0644 -f "$systemdservice"
+            if [ -x /bin/systemctl ]; then
+                    systemctl --system daemon-reload
+                    if [ $RUN_DAEMON = 'true' ]; then
+                            systemctl enable i2p.service
+                    else
+                            systemctl disable i2p.service
+                    fi
+            fi
+        fi
+
         sed -e "s/^ *wrapper\.java\.maxmemory=.*/wrapper\.java\.maxmemory=$MEMORYLIMIT/" \
             < /etc/i2p/wrapper.config > /etc/i2p/wrapper.config.tmp
         mv -f /etc/i2p/wrapper.config.tmp /etc/i2p/wrapper.config
@@ -72,7 +89,7 @@ case "$1" in
         usermod -c "I2P Router Daemon" -d $I2PHOME -g $I2PSYSUSER -s "/bin/false" \
             $I2PSYSUSER -e 1 > /dev/null 2>&1 || true
     else
-        adduser --system --quiet --group --home $I2PHOME $I2PSYSUSER
+        adduser --system --quiet --group --home $I2PHOME $I2PSYSUSER > /dev/null 2>&1
     fi
 
     [ -d /var/log/i2p ] || mkdir -m0750 /var/log/i2p
diff --git a/debian/i2p.service b/debian/i2p.service
new file mode 100644
index 0000000000..6a4c8d3b40
--- /dev/null
+++ b/debian/i2p.service
@@ -0,0 +1,26 @@
+[Unit]
+Description=load-balanced unspoofable packet switching network
+After=network.target
+
+[Service]
+Type=forking
+EnvironmentFile=/etc/default/i2p
+Environment="I2P_ARGS=/etc/i2p/wrapper.config \
+ wrapper.java.additional.1=-DloggerFilenameOverride=/var/log/i2p/log-router-@.txt \
+ wrapper.java.additional.10=-Dwrapper.logfile=/var/log/i2p/wrapper.log \
+ wrapper.java.additional.11=-Di2p.dir.pid=/run/i2p \
+ wrapper.java.additional.12=-Di2p.dir.temp=/tmp/i2p-daemon \
+ wrapper.logfile=/var/log/i2p/wrapper.log \
+ wrapper.pidfile=/run/i2p/i2p.pid\
+ wrapper.java.pidfile=/run/i2p/routerjvm.pid \
+ wrapper.daemonize=TRUE" TZ=UTC
+User=i2psvc
+PermissionsStartOnly=true
+ExecStartPre=/bin/mkdir -p /run/i2p /tmp/i2p-daemon
+ExecStartPre=/bin/chown -R ${I2PUSER}:${I2PUSER} /var/log/i2p /run/i2p /tmp/i2p-daemon
+ExecStartPre=/bin/chmod 750 /var/log/i2p
+ExecStart=/usr/sbin/wrapper "$I2P_ARGS"
+ExecStopPost=/bin/rm -rf /run/i2p /tmp/i2p-daemon
+
+[Install]
+WantedBy=multi-user.target
diff --git a/debian/rules b/debian/rules
index 9cab2018ce..447268238a 100755
--- a/debian/rules
+++ b/debian/rules
@@ -36,6 +36,10 @@ export DEBIANVERSION := $(shell dpkg-parsechangelog |awk -F' ' '/^Version:/{prin
 
 %:
 	dh $@ --with quilt
+	# with systemd needs dh-systemd which isn't available in Wheezy (except from backports),
+	# and is only Ubuntu as of "Saucy". The official packages will enable this for Debian unstable and
+	# Ubuntu Saucy (and newer)
+	#dh $@ --with systemd,quilt
 
 clean: createcopyright
 
@@ -50,6 +54,13 @@ override_dh_auto_clean:
 
 override_dh_auto_build:
 
+# Uncomment the next override line for systems that support dh-systemd
+# (currently Debian unstable and Ubuntu Saucy (and newer))
+#override_dh_systemd_enable:
+	# We purposely do not set I2P to start at boot.
+	# This matches the behavior of the initscript
+	# and the setting RUN_DAEMON=false in /etc/default/i2p
+
 binary-indep: build-indep
 build-indep:
 	dh_prep
-- 
GitLab