packaging: install repo and keys

Currently it is very difficult for non-power-users to get Ring
automatically updated when using the Debian package from the website.

This is because configuring the Ring package repository requires
command line operations.

We want the Ring Debian/Ubuntu/whatever-Debian-based package (the
one from the website, not the official one from the Debian archive)
to make sure Ring's repository url and GnuPG key are installed as
trusted source together with the binaries so that Ring gets
transparently updated along with system updates.

In order to do that, we have to change some things from the official
package we have in the Debian archive:

* we need to provide both daemon and client in a single ring-all
package. Otherwise we will not be able to provide a one-click install
(users would have to install daemon and client in two separate steps
and that doesn't make much sense to non-geek users).

So, replace ring by ring-all and keep building ring-daemon as before
(we still want to provide daemon-only packages).

* we need to add prerm and postinst scripts which will take care of
installing the repo and keys (and this has to be done in different
ways depending on the distribution) in the ring-all case.

Also, remove transition package ring-gnome which has already
disappeared from the Debian package some time ago.

Change-Id: I34d5e5ba000d5d750c8db55c65b536a237a738b5
Reviewed-by: Sebastien Blin <sebastien.blin@savoirfairelinux.com>
diff --git a/packaging/rules/debian/README.Debian b/packaging/rules/debian/README.Debian
index 52310f1..33f73b6 100644
--- a/packaging/rules/debian/README.Debian
+++ b/packaging/rules/debian/README.Debian
@@ -1,3 +1,30 @@
+# Differences and compatibility with official debian.org package
+
+This source package provides two binary packages ring-all and ring-daemon.
+It differs slightly from the official Debian package (the one from debian.org).
+
+ring-daemon: structurally identical to the Debian package, the only difference
+ potentially being the shipped version of Ring (the debian.org package is
+ usually older).
+
+ring-all: all-in-one package, ships both the Ring Daemon and the GNOME client.
+ This package is not available in the debian.org repositories (not needed in the
+ Debian case).
+
+ ring-all allows
+ - easy installation of Ring as a single package from ring.cx
+ - installation of release keys and ring.cx package repository as trusted peers
+   so Ring can be updated from ring.cx along with system updates
+
+ Why not two packages ring and ring-daemon instead of a single all package ?
+ -> One cannot register a repository in source.list.d and directly use it to
+    fetch the dependencies. That would require something like an apt update in
+    the maintscripts and we don't want that. So, it is not possible to have a
+    one-click-install with two packages.
+
+ ring-all conflicts with ring and ring-daemon since they are distributing the
+ same files.
+
 # Notes
 
 ## Importing a new version
diff --git a/packaging/rules/debian/control b/packaging/rules/debian/control
index 2f001e6..f127826 100644
--- a/packaging/rules/debian/control
+++ b/packaging/rules/debian/control
@@ -61,19 +61,22 @@
 Vcs-Git: https://anonscm.debian.org/git/pkg-voip/ring.git
 Vcs-Browser: https://anonscm.debian.org/cgit/pkg-voip/ring.git
 
-Package: ring
+Package: ring-all
 Architecture: any
 Depends: ${shlibs:Depends},
-         ${misc:Depends},
-         ring-daemon (=${binary:Version})
-Replaces: ring-gnome
-Conflicts: ring-gnome
-Description: Secure and distributed voice, video and chat platform - desktop client
+         ${misc:Depends}
+Replaces: ring,
+          ring-gnome,
+          ring-daemon
+Conflicts: ring,
+           ring-gnome,
+           ring-daemon
+Description: Secure and distributed voice, video and chat platform - all in one package
  Ring (ring.cx) is a secure and distributed voice, video and chat communication
  platform that requires no centralized server and leaves the power of privacy
  in the hands of the user.
  .
- This package contains the desktop client: gnome-ring.
+ This package contains the GNOME desktop client and dring, the Ring daemon.
 
 Package: ring-daemon
 Architecture: any
@@ -85,11 +88,3 @@
  in the hands of the user.
  .
  This package contains the Ring daemon: dring.
-
-Package: ring-gnome
-Depends: ring, ${misc:Depends}
-Architecture: all
-Priority: extra
-Section: oldlibs
-Description: transitional dummy package
- This is a transitional dummy package. It can safely be removed.
diff --git a/packaging/rules/debian/ring.lintian-overrides b/packaging/rules/debian/ring-all.lintian-overrides
similarity index 100%
rename from packaging/rules/debian/ring.lintian-overrides
rename to packaging/rules/debian/ring-all.lintian-overrides
diff --git a/packaging/rules/debian/ring.manpages b/packaging/rules/debian/ring-all.manpages
similarity index 66%
rename from packaging/rules/debian/ring.manpages
rename to packaging/rules/debian/ring-all.manpages
index d920cbc..87574cc 100644
--- a/packaging/rules/debian/ring.manpages
+++ b/packaging/rules/debian/ring-all.manpages
@@ -1 +1,2 @@
 client-gnome/doc/gnome-ring.1
+daemon/dring.1
diff --git a/packaging/rules/debian/ring-all.postinst b/packaging/rules/debian/ring-all.postinst
new file mode 100755
index 0000000..65e8aad
--- /dev/null
+++ b/packaging/rules/debian/ring-all.postinst
@@ -0,0 +1,171 @@
+#!/bin/sh
+set -e
+
+###############################################################################
+# RING PACKAGE POSTINST                                                       #
+#                                                                             #
+# Install Ring's package repository to the trusted sources and add Ring's     #
+# release key to trusted keyring.                                             #
+#                                                                             #
+# NOTE: We follow https://wiki.debian.org/DebianRepository/UseThirdParty      #
+###############################################################################
+
+
+###############################################################################
+# [1] Configuration                                                           #
+###############################################################################
+
+# All PPA/package repo urls are expected to start with this string, regardless
+# of the distribution or version. The end tag is automatically appended,
+# depending on the system the postinst script is run on. Examples:
+#
+# Ubuntu 18.04:
+# $RING_PPA = $RING_PPA_BASE/ubuntu_18.04/
+#
+# Debian 9:
+# $RING_PPA = $RING_PPA_BASE/debian_9
+#
+# To update the appended end tags, modify the switch in [2].
+RING_PPA_BASE="https://dl.ring.cx/ring-nightly"
+
+# Ring release key.
+RING_KEY_FINGERPRINT="A295D773307D25A33AE72F2F64CD5FA175348F84"
+RING_KEY="\
+-----BEGIN PGP PUBLIC KEY BLOCK-----
+Version: GnuPG v1
+
+mQENBFVSdlcBCAC9zC1rp12O2K08PGozI14Y+t4qC931eHicvkuEMF1B9gAhjdRF
+aIJS+UXwgQzoamDIHenxz1Q3fXUjKCMXytjGymB/0LUKccSbtH0Rcsl8kZ2z57KN
+E+GLS7SvlP93ZOxco7eAEBWF/fvMrCsm10sNI6bW7UK0bgql9iIetd6Wrp9xXFVs
+gmoV8Av714OlswsthSNtN+xQls3ozQ/dVGsOkZEyDbBzi88/rQEtuIDztTSWyD0V
+x7WaY5+mVRwsJKzyPlgvsXpbP7A41IFykeOzPKh+vYz3k7dcLIRdOwse79oT2RXt
+2VYEyTyTZIQlCJjGNTJYsU7GVffU4LnI7p/bABEBAAG0QFJpbmcgLSBTYXZvaXIt
+RmFpcmUgTGludXgsIEluYyA8cmluZ0BsaXN0cy5zYXZvaXJmYWlyZWxpbnV4Lm5l
+dD6JARwEEAEIAAY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cGj73sj36qoF
+IjorjymZlT5IdK39oXVMHi1TssiWPOU0hQgn4BIreYnEZUA6xuFX10C7k2DVRFZr
+XF7lpNgPQ8eNZTnQBIuwHUFCGSHO3/kzxSlkE1PBUX3IZ8PSFijyopBnWUhlSXuy
+Rjte8OR7Fl/Rlf0IaOD14sRdAfS333T4Uifq4uOu
+=vaci
+-----END PGP PUBLIC KEY BLOCK-----"
+
+# System paths and generated variables
+# In general: you will not need to modify these variables.
+# WARNING: When modifying the paths, keep in mind that the corresponding
+# postrm paths should be modified as well
+APT_FILE="/etc/apt/sources.list.d/ring-main.sources"
+APT_PREFS_FILE="/etc/apt/preferences.d/ring-main.pref"
+RING_KEY_FILE="/etc/apt/trusted.gpg.d/ring-main.gpg"
+UPDATE_MANAGER_PATH="/etc/update-manager/release-upgrades.d/"
+RING_UPDATE_MANAGER_ID="ring-main"
+
+RING_UPDATE_MANAGER_CFG="${RING_UPDATE_MANAGER_ID}.cfg"
+UPDATE_MANAGER_CFG_PATH="${UPDATE_MANAGER_PATH}/${RING_UPDATE_MANAGER_CFG}"
+
+
+###############################################################################
+# [2] Set package repo url depending on distribution and version              #
+###############################################################################
+
+CAN_ADD_DEB_SOURCE=true
+
+# Detect currently running system using /etc/os-release
+# Debian-based systems are supposed to provide /etc/os-release so if it's not
+# the case then we simply don't want to provide automatic updates
+if [ -f /etc/os-release ]; then
+    . /etc/os-release
+    # Set-up Ring PPA end tag
+    ENDTAG="${ID}_${VERSION_ID}"
+else
+    # Distribution is not supported. Don't provide automatic updates.
+    CAN_ADD_DEB_SOURCE=false
+fi
+
+
+###############################################################################
+# [3] Maintainer script main switch                                           #
+###############################################################################
+
+case "$1" in
+    configure)
+        if [ "$CAN_ADD_DEB_SOURCE" = "true" ]; then
+            RING_PPA="${RING_PPA_BASE}/${ENDTAG}"
+
+            # We first add the key to the trusted keyring.
+            cat > $RING_KEY_FILE <<EOF
+$RING_KEY
+EOF
+
+            # Add an entry for the package repository to the trusted package
+            # sources (DEB822 format)
+            cat > $APT_FILE <<EOF
+# This file makes sure that Ring is kept up-to-date
+# as part of regular system upgrades
+
+Types: deb
+URIs: $RING_PPA
+Suites: ring
+Architectures: i386 amd64
+Components: main
+Signed-By: $RING_KEY_FINGERPRINT
+EOF
+
+            # It is recommended to also restrict the range of the newly added
+            # source.
+            cat > $APT_PREFS_FILE <<EOF
+# forbid upgrading already installed packages from official repositories but
+# allow upgrades to be performed for the ring repository
+Package: *
+Pin: origin dl.ring.cx
+Pin-Priority: 100
+EOF
+
+            # Additionally, if update manager is installed we inform it about
+            # the repository so it doesn't get scrapped after system updates
+            if [ -d /etc/update-manager -a ! -f $UPDATE_MANAGER_CFG_PATH ]
+            then
+                mkdir -p $UPDATE_MANAGER_PATH
+                cat > $UPDATE_MANAGER_CFG_PATH <<EOF
+# Added by Ring to prevent disabling of Ring repository sources on
+# distribution release upgrade.
+[ThirdPartyMirrors]
+ring/${RING_UPDATE_MANAGER_ID}=${RING_PPA}
+EOF
+            fi
+        fi
+        ;;
+esac
+
+# exit with a zero status as everything went well, important for maintscripts
+exit 0
diff --git a/packaging/rules/debian/ring-all.postrm b/packaging/rules/debian/ring-all.postrm
new file mode 100755
index 0000000..898840a
--- /dev/null
+++ b/packaging/rules/debian/ring-all.postrm
@@ -0,0 +1,41 @@
+#!/bin/sh
+set -e
+
+###############################################################################
+# RING PACKAGE POSTRM                                                         #
+#                                                                             #
+# If we installed our key and source to trusted stores, remove them.          #
+#                                                                             #
+# NOTE: We follow https://wiki.debian.org/DebianRepository/UseThirdParty      #
+###############################################################################
+
+
+###############################################################################
+# [1] Configuration                                                           #
+###############################################################################
+
+# System paths and generated variables
+APT_FILE="/etc/apt/sources.list.d/ring-main.sources"
+APT_PREFS_FILE="/etc/apt/preferences.d/ring-main.pref"
+RING_KEY_FILE="/etc/apt/trusted.gpg.d/ring-main.gpg"
+UPDATE_MANAGER_PATH="/etc/update-manager/release-upgrades.d/"
+RING_UPDATE_MANAGER_ID="ring-main"
+
+RING_UPDATE_MANAGER_CFG="${RING_UPDATE_MANAGER_ID}.cfg"
+UPDATE_MANAGER_CFG_PATH="${UPDATE_MANAGER_PATH}/${RING_UPDATE_MANAGER_CFG}"
+
+
+###############################################################################
+# [2] Maintainer script main switch                                           #
+###############################################################################
+
+case "$1" in
+    purge)
+        rm -f "$APT_FILE"
+        rm -f "$APT_PREFS_FILE"
+        rm -f "$UPDATE_MANAGER_CFG_PATH"
+        rm -f "$RING_KEY_FILE"
+        ;;
+esac
+
+exit 0
diff --git a/packaging/rules/debian/ring.triggers b/packaging/rules/debian/ring-all.triggers
similarity index 100%
rename from packaging/rules/debian/ring.triggers
rename to packaging/rules/debian/ring-all.triggers
diff --git a/packaging/rules/debian/rules b/packaging/rules/debian/rules
index 5a69c84..65027e3 100755
--- a/packaging/rules/debian/rules
+++ b/packaging/rules/debian/rules
@@ -12,6 +12,10 @@
 NO_CPUS=1
 endif
 
+# Binary package names
+RING_ALL_IN_ONE_PKG_NAME="ring-all"
+RING_DAEMON_PKG_NAME="ring-daemon"
+
 %:
 	dh $@
 
@@ -132,29 +136,36 @@
 
 override_dh_auto_install:
 	#########################
-	## Ring Daemon install ##
+	## Ring daemon install ##
 	#########################
-	cd daemon && make DESTDIR=$(CURDIR)/debian/ring-daemon install
-	rm -rfv $(CURDIR)/debian/ring-daemon/usr/include
-	rm -rfv $(CURDIR)/debian/ring-daemon/usr/lib/*.a
-	rm -rfv $(CURDIR)/debian/ring-daemon/usr/lib/*.la
 
-	###########################
-	## libringclient install ##
-	###########################
-	cd lrc/build && make DESTDIR=$(CURDIR)/debian/ring install
-	rm -rfv $(CURDIR)/debian/ring/usr/include
+	cd daemon && make DESTDIR=$(CURDIR)/debian/$(RING_DAEMON_PKG_NAME) install
+	rm -rfv $(CURDIR)/debian/$(RING_DAEMON_PKG_NAME)/usr/include
+	rm -rfv $(CURDIR)/debian/$(RING_DAEMON_PKG_NAME)/usr/lib/*.a
+	rm -rfv $(CURDIR)/debian/$(RING_DAEMON_PKG_NAME)/usr/lib/*.la
+
+	######################
+	## Ring AiO install ##
+	######################
+
+        ## daemon
+	cd daemon && make DESTDIR=$(CURDIR)/debian/$(RING_ALL_IN_ONE_PKG_NAME) install
+	rm -rfv $(CURDIR)/debian/$(RING_ALL_IN_ONE_PKG_NAME)/usr/include
+	rm -rfv $(CURDIR)/debian/$(RING_ALL_IN_ONE_PKG_NAME)/usr/lib/*.a
+	rm -rfv $(CURDIR)/debian/$(RING_ALL_IN_ONE_PKG_NAME)/usr/lib/*.la
+
+        ## LibRingClient
+	cd lrc/build && make DESTDIR=$(CURDIR)/debian/$(RING_ALL_IN_ONE_PKG_NAME) install
+	rm -rfv $(CURDIR)/debian/$(RING_ALL_IN_ONE_PKG_NAME)/usr/include
 
 	# This is a symlink, should be in -dev package
-	rm -v $(CURDIR)/debian/ring/usr/lib/libringclient.so
+	rm -v $(CURDIR)/debian/$(RING_ALL_IN_ONE_PKG_NAME)/usr/lib/libringclient.so
 
 	# cmake files
-	rm -rfv $(CURDIR)/debian/ring/usr/lib/cmake
+	rm -rfv $(CURDIR)/debian/$(RING_ALL_IN_ONE_PKG_NAME)/usr/lib/cmake
 
-	##########################
-	## gnome client install ##
-	##########################
-	cd client-gnome/build && make DESTDIR=$(CURDIR)/debian/ring install
+        ## GNOME client
+	cd client-gnome/build && make DESTDIR=$(CURDIR)/debian/$(RING_ALL_IN_ONE_PKG_NAME) install
 
 tmpdir:= $(shell mktemp -d)
 workdir:= $(shell pwd)