From d29e1624e38b528a7ae0e4cba83e4a790102aa8d Mon Sep 17 00:00:00 2001
From: Tobias Markmann <tm@ayena.de>
Date: Wed, 7 Feb 2018 16:11:00 +0100
Subject: Move man page generation from package.sh to scons

Pass help2man=1 to have scons build man page sources via
help2man.
Adjusted package.sh to use scons to build man pages.

Test-Information:

Tested on Ubuntu 16.04 and it generated man page sources
when help2man=1.

Tested package_all_platforms on Ubuntu 16.04. The build does
not fail due to missing man pages anymore.

Change-Id: I6c62bc3f666ffd9f926038045672eb6e456082e5

diff --git a/BuildTools/SCons/SConscript.boot b/BuildTools/SCons/SConscript.boot
index d9678a5..3b21dde 100644
--- a/BuildTools/SCons/SConscript.boot
+++ b/BuildTools/SCons/SConscript.boot
@@ -111,6 +111,7 @@ vars.Add(BoolVariable("unbound", "Build bundled ldns and unbound. Use them for D
 vars.Add(BoolVariable("check_headers", "Independently build compilation units for all Swiften headers for detecting missing dependencies.", "no"))
 vars.Add("win_target_arch", "Target architecture for Windows builds. x86 for 32-bit (default) or x86_64 for 64-bit.", "x86")
 vars.Add(BoolVariable("install_git_hooks", "Install git hooks", "true"))
+vars.Add(BoolVariable("help2man", "Run help2man to geneate man pages", "false"))
 
 # Code Signing Options
 vars.Add("codesign_identity", "macOS code signing identity to be passed to codesign when building the distribution package. Must match the Commen Name of the Subject of the code signing certificate.", "")
diff --git a/Swift/Packaging/Debian/debian/control.in b/Swift/Packaging/Debian/debian/control.in
index 26544e5..e779f21 100644
--- a/Swift/Packaging/Debian/debian/control.in
+++ b/Swift/Packaging/Debian/debian/control.in
@@ -3,7 +3,7 @@ Section: net
 Priority: optional
 Maintainer: Swift Package Maintainer <packages@swift.im>
 Uploaders: Remko Tronçon <dev@el-tramo.be>, Kevin Smith <kevin@kismith.co.uk>
-Build-Depends: debhelper (>= 9), scons (>= 1.2.0), libssl-dev (>= 0.9.8g), qtbase5-dev (>= 5.0.0), qtchooser, qtbase5-dev-tools (>= 5.0.0), libqt5x11extras5-dev (>= 5.0.0), libqt5webkit5-dev (>= 5.0.0), qtmultimedia5-dev (>=5.0.0), qttools5-dev-tools (>=5.0.0), qt5-image-formats-plugins (>=5.0.0), libqt5svg5-dev (>=5.0.0), libxml2-dev (>= 2.7.6), libxss-dev (>= 1.2.0), libboost-dev (>= 1.34.1), libboost-filesystem-dev (>= 1.34.1), libboost-program-options-dev (>= 1.34.1), libboost-regex-dev (>= 1.34.1), libboost-signals-dev (>= 1.34.1), libboost-system-dev (>= 1.34.1), libboost-thread-dev (>= 1.34.1), libboost-date-time-dev (>= 1.34.1), libidn11-dev (>= 1.10), docbook-xsl (>= 1.75.0), docbook-xml (>= 4.5), xsltproc, libxml2-utils, libnatpmp-dev, libminiupnpc-dev, libsqlite3-dev, libhunspell-dev, zlib1g-dev
+Build-Depends: debhelper (>= 9), scons (>= 1.2.0), libssl-dev (>= 0.9.8g), qtbase5-dev (>= 5.0.0), qtchooser, qtbase5-dev-tools (>= 5.0.0), libqt5x11extras5-dev (>= 5.0.0), libqt5webkit5-dev (>= 5.0.0), qtmultimedia5-dev (>=5.0.0), qttools5-dev-tools (>=5.0.0), qt5-image-formats-plugins (>=5.0.0), libqt5svg5-dev (>=5.0.0), libxml2-dev (>= 2.7.6), libxss-dev (>= 1.2.0), libboost-dev (>= 1.34.1), libboost-filesystem-dev (>= 1.34.1), libboost-program-options-dev (>= 1.34.1), libboost-regex-dev (>= 1.34.1), libboost-signals-dev (>= 1.34.1), libboost-system-dev (>= 1.34.1), libboost-thread-dev (>= 1.34.1), libboost-date-time-dev (>= 1.34.1), libidn11-dev (>= 1.10), docbook-xsl (>= 1.75.0), docbook-xml (>= 4.5), xsltproc, libxml2-utils, libnatpmp-dev, libminiupnpc-dev, libsqlite3-dev, libhunspell-dev, zlib1g-dev, help2man
 Standards-Version: 3.9.8
 Vcs-Git: git://swift.im/swift
 Vcs-Browser: http://swift.im/git/swift
diff --git a/Swift/Packaging/Debian/debian/rules b/Swift/Packaging/Debian/debian/rules
index 87c551b..ed432bb 100755
--- a/Swift/Packaging/Debian/debian/rules
+++ b/Swift/Packaging/Debian/debian/rules
@@ -4,7 +4,7 @@
 export PYTHONDONTWRITEBYTECODE=1
 export QT_SELECT=qt5
 
-SCONS_FLAGS=V=1 qt5=1 optimize=1 debug=1 allow_warnings=1 swiften_dll=1 docbook_xsl=/usr/share/xml/docbook/stylesheet/docbook-xsl docbook_xml=/usr/share/xml/docbook/schema/dtd/4.5 linkflags="$(shell dpkg-buildflags --get LDFLAGS)" ccflags="$(shell dpkg-buildflags --get CPPFLAGS) $(shell dpkg-buildflags --get CFLAGS)"
+SCONS_FLAGS=V=1 qt5=1 optimize=1 debug=1 allow_warnings=1 swiften_dll=1 help2man=1 docbook_xsl=/usr/share/xml/docbook/stylesheet/docbook-xsl docbook_xml=/usr/share/xml/docbook/schema/dtd/4.5 linkflags="$(shell dpkg-buildflags --get LDFLAGS)" ccflags="$(shell dpkg-buildflags --get CPPFLAGS) $(shell dpkg-buildflags --get CFLAGS)"
 SCONS_INSTALL_BASE=$(CURDIR)/debian/tmp
 SCONS_INSTALL_FLAGS=SWIFT_INSTALLDIR=$(SCONS_INSTALL_BASE)/usr SWIFTEN_INSTALLDIR=$(SCONS_INSTALL_BASE)/usr
 SWIFT_INSTALLDIR=$(SCONS_INSTALL_BASE)/usr SWIFTEN_INSTALLDIR=$(SCONS_INSTALL_BASE)/usr
@@ -21,7 +21,7 @@ override_dh_clean:
 override_dh_configure:
 
 override_dh_auto_build:
-	scons $(SCONS_FLAGS) $(SCONS_EXTRA_FLAGS) Swift Swiften
+	scons $(SCONS_FLAGS) $(SCONS_EXTRA_FLAGS) Swift Swiften debian/swift-im.1 debian/swiften-config.1
 
 override_dh_auto_install:
 	scons $(SCONS_FLAGS) $(SCONS_EXTRA_FLAGS) $(SCONS_INSTALL_FLAGS) $(SCONS_INSTALL_BASE)
diff --git a/Swift/Packaging/Debian/package.sh b/Swift/Packaging/Debian/package.sh
index d19f5dc..62dfff0 100755
--- a/Swift/Packaging/Debian/package.sh
+++ b/Swift/Packaging/Debian/package.sh
@@ -95,18 +95,6 @@ cat $DIRNAME/debian/control.in | sed -e "s/%SWIFTEN_SOVERSION%/$SWIFTEN_SOVERSIO
 rm $DIRNAME/debian/control.in
 mv $DIRNAME/debian/libswiften.install $DIRNAME/debian/libswiften$SWIFTEN_SOVERSION.install
 cat ../../../COPYING.thirdparty | tail -n +3 >> $DIRNAME/debian/copyright
-# Generate updated man-page if possible
-if type "help2man" > /dev/null 2>&1; then
-	if [ -f ../../QtUI/swift-im ]; then 
-		help2man -m "Swift Manual" -S "Swift" -n "swift-im" -N ../../QtUI/swift-im > $DIRNAME/debian/swift-im.1
-	fi
-	if [ -f ../../../Swiften/Config/swiften-config ]; then
-		help2man -m "Swift Manual" -S "swiften-config" -n "swiften-config" -N ../../../Swiften/Config/swiften-config > $DIRNAME/debian/swiften-config.1
-	fi
-else
-	>2& echo "Unable to generate man pages. Please ensure that help2man is installed"
-	exit 1;
-fi
 
 # Build
 cd $DIRNAME
diff --git a/Swift/Packaging/SConscript b/Swift/Packaging/SConscript
new file mode 100644
index 0000000..bd7e6d8
--- /dev/null
+++ b/Swift/Packaging/SConscript
@@ -0,0 +1,29 @@
+Import("env")
+import os
+
+################################################################################
+# Build
+################################################################################
+
+if env["SCONS_STAGE"] == "build" :
+    help2man = env.WhereIs('help2man', os.environ['PATH'])
+    if help2man:
+        env['HELP2MAN'] = help2man
+        env['HELP2MANSTR'] = "HELP2MAN $TARGET"
+
+        if Dir("#/debian").exists():
+            # This is needed for debian packaging using pbuilder which expects
+            # generated man pages in this location.
+            env['HELP2MAN_DEBIAN_DIR'] = "#/debian"
+        else:
+            env['HELP2MAN_DEBIAN_DIR'] = "Debian/debian"
+
+        swiftenConfigHelp = env.Command(
+            target='$HELP2MAN_DEBIAN_DIR/swiften-config.1', source='#/Swiften/Config/swiften-config',
+            action = Action('$HELP2MAN -m "Swift Manual" -S "swiften-config" -n "swiften-config" -N $SOURCE > $TARGET', cmdstr = "$HELP2MANSTR"))
+        swiftHelp = env.Command(
+            target='$HELP2MAN_DEBIAN_DIR/swift-im.1', source='#/Swift/QtUI/swift-im',
+            action = Action('$HELP2MAN -m "Swift Manual" -S "Swift" -n "swift-im" -N $SOURCE > $TARGET', cmdstr = "$HELP2MANSTR"))
+    else:
+        print "Enabled help2man but help2man is not in the PATH of the current environment."
+        Exit(1)
\ No newline at end of file
diff --git a/Swift/SConscript b/Swift/SConscript
index 863597a..b211435 100644
--- a/Swift/SConscript
+++ b/Swift/SConscript
@@ -19,3 +19,5 @@ if "Swift" in env["PROJECTS"] and env["BOOST_1_64_DETECTED"] and not env.get("al
     #Version 1.64 has some issues with the serialization of boost::optional, see https://svn.boost.org/trac10/ticket/13050
     print "Boost 1.64 has been detected. It is not recommended to use this version due to a regression within the library. Swift has been removed from the current build. You can still use this version by setting allow_boost_1_64 to true, but recent chats and highlighting rules will reset."
     env["PROJECTS"].remove("Swift")
+if env["help2man"]:
+    SConscript("Packaging/SConscript")
\ No newline at end of file
-- 
cgit v0.10.2-6-g49f6