Index: debian/bitlbee-common.config
===================================================================
--- debian/bitlbee-common.config	(revision 2bfe976616cc80439257959411c3bb20f2ab2e6b)
+++ debian/bitlbee-common.config	(revision 2bfe976616cc80439257959411c3bb20f2ab2e6b)
@@ -0,0 +1,25 @@
+#!/bin/sh -e
+
+. /usr/share/debconf/confmodule
+[ -f /etc/default/bitlbee ] && . /etc/default/bitlbee
+
+db_title BitlBee
+
+if [ -n "$BITLBEE_PORT" ]; then
+	db_set bitlbee/serveport "$BITLBEE_PORT"
+else
+	db_get bitlbee/serveport
+	if [ "$RET" = "stillhavetoask" ]; then
+		listens=$(netstat -ltn | awk '{print $4}')
+		for port in 6667 6666 6668 6669; do
+			if [ $(expr "$listens " : ".*:$port\s") = "0" ]; then
+				break
+			fi
+		done
+		db_set bitlbee/serveport $port;
+	fi
+fi
+
+if db_input medium bitlbee/serveport; then
+	db_go;
+fi
Index: debian/bitlbee-common.docs
===================================================================
--- debian/bitlbee-common.docs	(revision 2bfe976616cc80439257959411c3bb20f2ab2e6b)
+++ debian/bitlbee-common.docs	(revision 2bfe976616cc80439257959411c3bb20f2ab2e6b)
@@ -0,0 +1,6 @@
+doc/user-guide/user-guide.txt
+doc/user-guide/user-guide.html
+doc/AUTHORS
+doc/CREDITS
+doc/FAQ
+doc/README
Index: debian/bitlbee-common.examples
===================================================================
--- debian/bitlbee-common.examples	(revision 2bfe976616cc80439257959411c3bb20f2ab2e6b)
+++ debian/bitlbee-common.examples	(revision 2bfe976616cc80439257959411c3bb20f2ab2e6b)
@@ -0,0 +1,1 @@
+utils/*
Index: debian/bitlbee-common.templates
===================================================================
--- debian/bitlbee-common.templates	(revision 2bfe976616cc80439257959411c3bb20f2ab2e6b)
+++ debian/bitlbee-common.templates	(revision 2bfe976616cc80439257959411c3bb20f2ab2e6b)
@@ -0,0 +1,8 @@
+Template: bitlbee/serveport
+Type: string
+Default: stillhavetoask
+_Description: On what TCP port should BitlBee listen for connections?
+ BitlBee normally listens on the regular IRC port, 6667. This might not be
+ a very good idea when you're running a real IRC daemon as well. 6668 might
+ be a good alternative. Leaving this value blank means that BitlBee will not
+ be run automatically.
Index: debian/bitlbee.postinst
===================================================================
--- debian/bitlbee.postinst	(revision 2bfe976616cc80439257959411c3bb20f2ab2e6b)
+++ debian/bitlbee.postinst	(revision 2bfe976616cc80439257959411c3bb20f2ab2e6b)
@@ -0,0 +1,102 @@
+#!/bin/sh -e
+
+. /usr/share/debconf/confmodule
+
+db_get bitlbee/serveport
+PORT="$RET"
+
+CONFDIR=/var/lib/bitlbee/
+
+update-rc.d bitlbee defaults > /dev/null 2>&1
+
+## Load default option. Don't want to put this in debconf (yet?)
+BITLBEE_OPTS=-F
+BITLBEE_DISABLED=0
+BITLBEE_UPGRADE_DONT_RESTART=0
+[ -r /etc/default/bitlbee ] && . /etc/default/bitlbee
+
+if [ "$BITLBEE_DISABLED" = "0" ] && type update-inetd > /dev/null 2> /dev/null &&
+   ( expr "$2" : '0\..*' > /dev/null || expr "$2" : '1\.0\..*' > /dev/null ); then
+	## Make sure the inetd entry is gone (can still be there from a
+	## previous version.
+	update-inetd --remove '.*/usr/sbin/bitlbee'
+	if grep -q /usr/sbin/bitlbee /etc/inetd.conf 2> /dev/null; then
+		# Thanks for breaking update-inetd! (bugs.debian.org/311111)
+		# I hope that it works at least with xinetd, because this
+		# emergency hack doesn't:
+		perl -pi -e 's:^[^#].*/usr/sbin/bitlbee$:## Now using daemon mode\: # $&:' /etc/inetd.conf
+		killall -HUP inetd || true
+	fi
+fi
+
+cat<<EOF>/etc/default/bitlbee
+## /etc/default/bitlbee: Auto-generated/updated script.
+##
+## If running in (fork)daemon mode, listen on this TCP port.
+BITLBEE_PORT="$PORT"
+
+## Use single-process or forking daemon mode? Can't be changed from debconf,
+## but maintainer scripts will save your changes here.
+BITLBEE_OPTS="$BITLBEE_OPTS"
+
+## In case you want to stick with inetd mode (or if you just want to disable
+## the init scripts for some other reason), you can disable the init script
+## here. (Just set it to 1)
+BITLBEE_DISABLED=$BITLBEE_DISABLED
+
+## As a server operator, you can use the RESTART command to restart only the
+## master process while keeping all the child processes and their IPC
+## connections. By enabling this, the maintainer scripts won't restart
+## BitlBee during upgrades so you can restart the master process by hand.
+BITLBEE_UPGRADE_DONT_RESTART=$BITLBEE_UPGRADE_DONT_RESTART
+EOF
+
+## Bye-bye DebConf, we don't need you anymore.
+db_stop
+
+## Restore the helpfile in case we weren't upgrading but just reconfiguring:
+if [ -e /usr/share/bitlbee/help.upgrading ]; then
+	if [ -e /usr/share/bitlbee/help.txt ]; then
+		rm -f /usr/share/bitlbee/help.upgrading
+	else
+		mv /usr/share/bitlbee/help.upgrading /usr/share/bitlbee/help.txt
+	fi
+fi
+
+if [ -n "$2" -a "$BITLBEE_UPGRADE_DONT_RESTART" != "1" ]; then
+	if which invoke-rc.d >/dev/null 2>&1; then
+		invoke-rc.d bitlbee restart
+	else
+		/etc/init.d/bitlbee restart
+	fi
+fi
+
+## If we're upgrading, we'll probably skip this next part
+if [ -d $CONFDIR ] && chown -R bitlbee: $CONFDIR; then
+	echo 'BitlBee (probably) already installed, skipping user/configdir installation'
+	exit 0
+fi
+
+adduser --system --group --disabled-login --disabled-password --home /var/lib/bitlbee/ bitlbee
+chmod 700 /var/lib/bitlbee/
+
+## Can't do this in packaging phase: Don't know the UID yet. Access to
+## the file should be limited, now that it stores passwords. Added
+## --group later for a little more security, but have to see if I can
+## apply this change to existing installations on upgrades. Will think
+## about that later.
+if getent group bitlbee > /dev/null; then
+	chmod 640 /etc/bitlbee/bitlbee.conf
+	chown root:bitlbee /etc/bitlbee/bitlbee.conf
+else
+	chmod 600 /etc/bitlbee/bitlbee.conf
+	chown bitlbee /etc/bitlbee/bitlbee.conf
+fi
+
+if [ -z "$2" ]; then
+	if which invoke-rc.d >/dev/null 2>&1; then
+		invoke-rc.d bitlbee start
+	else
+		/etc/init.d/bitlbee start
+	fi
+fi
Index: debian/bitlbee.postrm
===================================================================
--- debian/bitlbee.postrm	(revision 2bfe976616cc80439257959411c3bb20f2ab2e6b)
+++ debian/bitlbee.postrm	(revision 2bfe976616cc80439257959411c3bb20f2ab2e6b)
@@ -0,0 +1,14 @@
+#!/bin/sh -e
+
+[ "$1" = "purge" ] || exit 0
+
+if [ -e /usr/share/debconf/confmodule ]; then
+	. /usr/share/debconf/confmodule;
+	db_purge;
+fi
+
+update-rc.d bitlbee remove > /dev/null 2>&1 || true
+rm -f /etc/default/bitlbee
+
+deluser --system --remove-home bitlbee || true
+rm -rf /var/lib/bitlbee ## deluser doesn't seem to do this for homedirs in /var
Index: debian/bitlbee.prerm
===================================================================
--- debian/bitlbee.prerm	(revision 2bfe976616cc80439257959411c3bb20f2ab2e6b)
+++ debian/bitlbee.prerm	(revision 2bfe976616cc80439257959411c3bb20f2ab2e6b)
@@ -0,0 +1,17 @@
+#!/bin/sh -e
+
+if [ "$1" = "upgrade" ]; then
+	## To prevent the help function from breaking in currently running
+	## BitlBee processes. Have to do it like this because dpkg-reconfigure
+	## looks a lot like an upgrade and we don't want to lose help.txt...
+	if [ -e /usr/share/bitlbee/help.txt ]; then
+		rm -f /usr/share/bitlbee/help.upgrading
+		mv /usr/share/bitlbee/help.txt /usr/share/bitlbee/help.upgrading
+	fi
+else
+	if which invoke-rc.d >/dev/null 2>&1; then
+		invoke-rc.d bitlbee stop || exit 0
+	else
+		/etc/init.d/bitlbee stop || exit 0
+	fi
+fi
Index: debian/changelog
===================================================================
--- debian/changelog	(revision e693ac27b08e60c325223a0797dbf49ed2dbc3b3)
+++ debian/changelog	(revision 2bfe976616cc80439257959411c3bb20f2ab2e6b)
@@ -1,2 +1,11 @@
+bitlbee (1.3-0) unstable; urgency=low
+
+  * Setting some bogus version number, fix that later.
+  * Now using debhelper to improve maintainability.
+  * Added a bitlbee-libpurple package, and split off docs and stuff into
+    bitlbee-common.
+
+ -- Wilmer van der Gaast <wilmer@gaast.net>  Sat, 05 Jun 2010 15:16:38 +0100
+
 bitlbee (1.2.8-1) unstable; urgency=low
 
Index: debian/compat
===================================================================
--- debian/compat	(revision 2bfe976616cc80439257959411c3bb20f2ab2e6b)
+++ debian/compat	(revision 2bfe976616cc80439257959411c3bb20f2ab2e6b)
@@ -0,0 +1,1 @@
+6
Index: debian/conffiles
===================================================================
--- debian/conffiles	(revision e693ac27b08e60c325223a0797dbf49ed2dbc3b3)
+++ 	(revision )
@@ -1,3 +1,0 @@
-/etc/bitlbee/motd.txt
-/etc/bitlbee/bitlbee.conf
-/etc/init.d/bitlbee
Index: debian/config
===================================================================
--- debian/config	(revision e693ac27b08e60c325223a0797dbf49ed2dbc3b3)
+++ 	(revision )
@@ -1,25 +1,0 @@
-#!/bin/sh -e
-
-. /usr/share/debconf/confmodule
-[ -f /etc/default/bitlbee ] && . /etc/default/bitlbee
-
-db_title BitlBee
-
-if [ -n "$BITLBEE_PORT" ]; then
-	db_set bitlbee/serveport "$BITLBEE_PORT"
-else
-	db_get bitlbee/serveport
-	if [ "$RET" = "stillhavetoask" ]; then
-		listens=$(netstat -ltn | awk '{print $4}')
-		for port in 6667 6666 6668 6669; do
-			if [ $(expr "$listens " : ".*:$port\s") = "0" ]; then
-				break
-			fi
-		done
-		db_set bitlbee/serveport $port;
-	fi
-fi
-
-if db_input medium bitlbee/serveport; then
-	db_go;
-fi
Index: debian/control
===================================================================
--- debian/control	(revision e693ac27b08e60c325223a0797dbf49ed2dbc3b3)
+++ debian/control	(revision 2bfe976616cc80439257959411c3bb20f2ab2e6b)
@@ -4,6 +4,6 @@
 Maintainer: Wilmer van der Gaast <wilmer@gaast.net>
 Uploaders: Jelmer Vernooij <jelmer@samba.org>
-Standards-Version: 3.8.0
-Build-Depends: libglib2.0-dev (>= 2.4), libevent-dev, libgnutls-dev | libnss-dev (>= 1.6), debconf-2.0, po-debconf
+Standards-Version: 3.8.4
+Build-Depends: libglib2.0-dev (>= 2.4), libevent-dev, libgnutls-dev | libnss-dev (>= 1.6), po-debconf, libpurple-dev, debhelper (>= 6)
 Homepage: http://www.bitlbee.org/
 Vcs-Bzr: http://code.bitlbee.org/bitlbee/
@@ -12,14 +12,46 @@
 Package: bitlbee
 Architecture: any
-Depends: ${shlibs:Depends}, adduser, net-tools, ${debconf-depends}, debianutils (>= 1.16)
-Description: An IRC to other chat networks gateway
+Depends: ${shlibs:Depends}, adduser, debianutils (>= 1.16), bitlbee-common (= ${bee:Version})
+Conflicts: bitlbee-libpurple
+Replaces: bitlbee-libpurple
+Description: An IRC to other chat networks gateway (default version)
  This program can be used as an IRC server which forwards everything you
  say to people on other chat networks: Jabber, ICQ, AIM, MSN, Yahoo! and
  Twitter.
 
+Package: bitlbee-libpurple
+Architecture: any
+Depends: ${shlibs:Depends}, adduser, debianutils (>= 1.16), bitlbee-common (= ${bee:Version})
+Conflicts: bitlbee
+Replaces: bitlbee
+Description: An IRC to other chat networks gateway (using libpurple)
+ This program can be used as an IRC server which forwards everything you
+ say to people on other chat networks: Jabber, ICQ, AIM, MSN, Yahoo! and
+ Twitter.
+ .
+ This package contains a version of BitlBee that uses the libpurple instant
+ messaging library instead of built-in code, which adds support for more IM
+ protocols (all protocols supported by Pidgin/Finch) and features (like file
+ transfers), at the price of being less lightweight.
+ .
+ This variant may not be very suitable for BitlBee instances used by many
+ (tens or hundreds) of clients.
+
+Package: bitlbee-common
+Architecture: all
+Depends: ${misc:Depends}, net-tools
+Replaces: bitlbee
+Description: An IRC to other chat networks gateway (common files/docs)
+ This program can be used as an IRC server which forwards everything you
+ say to people on other chat networks: Jabber, ICQ, AIM, MSN, Yahoo! and
+ Twitter.
+ .
+ This package contains common files (mostly documentation) for bitlbee and
+ bitlbee-libpurple.
+
 Package: bitlbee-dev
 Architecture: all
-Depends: bitlbee (>= ${source:Version}), bitlbee (<< ${source:Version}.1~)
-Description: An IRC to other chat networks gateway
+Depends: ${misc:Depends}, bitlbee (>= ${bee:Version}), bitlbee (<< ${bee:Version}.1~)
+Description: An IRC to other chat networks gateway (dev files)
  This program can be used as an IRC server which forwards everything you
  say to people on other chat networks: Jabber, ICQ, AIM, MSN, Yahoo! and
Index: debian/patches/bitlbee.conf.diff
===================================================================
--- debian/patches/bitlbee.conf.diff	(revision e693ac27b08e60c325223a0797dbf49ed2dbc3b3)
+++ debian/patches/bitlbee.conf.diff	(revision 2bfe976616cc80439257959411c3bb20f2ab2e6b)
@@ -1,4 +1,4 @@
---- debian/bitlbee/etc/bitlbee/bitlbee.conf	2009-06-01 00:20:24.000000000 +0100
-+++ debian/bitlbee/etc/bitlbee/bitlbee.conf	2009-06-07 21:16:19.000000000 +0100
+--- bitlbee.conf	2009-06-01 00:20:24.000000000 +0100
++++ bitlbee.conf	2009-06-07 21:16:19.000000000 +0100
 @@ -23,13 +23,18 @@
  ## If BitlBee is started by root as a daemon, it can drop root privileges,
Index: debian/po/POTFILES.in
===================================================================
--- debian/po/POTFILES.in	(revision e693ac27b08e60c325223a0797dbf49ed2dbc3b3)
+++ debian/po/POTFILES.in	(revision 2bfe976616cc80439257959411c3bb20f2ab2e6b)
@@ -1,1 +1,1 @@
-[type: gettext/rfc822deb] templates
+[type: gettext/rfc822deb] bitlbee-common.templates
Index: debian/postinst
===================================================================
--- debian/postinst	(revision e693ac27b08e60c325223a0797dbf49ed2dbc3b3)
+++ 	(revision )
@@ -1,102 +1,0 @@
-#!/bin/sh -e
-
-. /usr/share/debconf/confmodule
-
-db_get bitlbee/serveport
-PORT="$RET"
-
-CONFDIR=/var/lib/bitlbee/
-
-update-rc.d bitlbee defaults > /dev/null 2>&1
-
-## Load default option. Don't want to put this in debconf (yet?)
-BITLBEE_OPTS=-F
-BITLBEE_DISABLED=0
-BITLBEE_UPGRADE_DONT_RESTART=0
-[ -r /etc/default/bitlbee ] && . /etc/default/bitlbee
-
-if [ "$BITLBEE_DISABLED" = "0" ] && type update-inetd > /dev/null 2> /dev/null &&
-   ( expr "$2" : '0\..*' > /dev/null || expr "$2" : '1\.0\..*' > /dev/null ); then
-	## Make sure the inetd entry is gone (can still be there from a
-	## previous version.
-	update-inetd --remove '.*/usr/sbin/bitlbee'
-	if grep -q /usr/sbin/bitlbee /etc/inetd.conf 2> /dev/null; then
-		# Thanks for breaking update-inetd! (bugs.debian.org/311111)
-		# I hope that it works at least with xinetd, because this
-		# emergency hack doesn't:
-		perl -pi -e 's:^[^#].*/usr/sbin/bitlbee$:## Now using daemon mode\: # $&:' /etc/inetd.conf
-		killall -HUP inetd || true
-	fi
-fi
-
-cat<<EOF>/etc/default/bitlbee
-## /etc/default/bitlbee: Auto-generated/updated script.
-##
-## If running in (fork)daemon mode, listen on this TCP port.
-BITLBEE_PORT="$PORT"
-
-## Use single-process or forking daemon mode? Can't be changed from debconf,
-## but maintainer scripts will save your changes here.
-BITLBEE_OPTS="$BITLBEE_OPTS"
-
-## In case you want to stick with inetd mode (or if you just want to disable
-## the init scripts for some other reason), you can disable the init script
-## here. (Just set it to 1)
-BITLBEE_DISABLED=$BITLBEE_DISABLED
-
-## As a server operator, you can use the RESTART command to restart only the
-## master process while keeping all the child processes and their IPC
-## connections. By enabling this, the maintainer scripts won't restart
-## BitlBee during upgrades so you can restart the master process by hand.
-BITLBEE_UPGRADE_DONT_RESTART=$BITLBEE_UPGRADE_DONT_RESTART
-EOF
-
-## Bye-bye DebConf, we don't need you anymore.
-db_stop
-
-## Restore the helpfile in case we weren't upgrading but just reconfiguring:
-if [ -e /usr/share/bitlbee/help.upgrading ]; then
-	if [ -e /usr/share/bitlbee/help.txt ]; then
-		rm -f /usr/share/bitlbee/help.upgrading
-	else
-		mv /usr/share/bitlbee/help.upgrading /usr/share/bitlbee/help.txt
-	fi
-fi
-
-if [ -n "$2" -a "$BITLBEE_UPGRADE_DONT_RESTART" != "1" ]; then
-	if which invoke-rc.d >/dev/null 2>&1; then
-		invoke-rc.d bitlbee restart
-	else
-		/etc/init.d/bitlbee restart
-	fi
-fi
-
-## If we're upgrading, we'll probably skip this next part
-if [ -d $CONFDIR ] && chown -R bitlbee: $CONFDIR; then
-	echo 'BitlBee (probably) already installed, skipping user/configdir installation'
-	exit 0
-fi
-
-adduser --system --group --disabled-login --disabled-password --home /var/lib/bitlbee/ bitlbee
-chmod 700 /var/lib/bitlbee/
-
-## Can't do this in packaging phase: Don't know the UID yet. Access to
-## the file should be limited, now that it stores passwords. Added
-## --group later for a little more security, but have to see if I can
-## apply this change to existing installations on upgrades. Will think
-## about that later.
-if getent group bitlbee > /dev/null; then
-	chmod 640 /etc/bitlbee/bitlbee.conf
-	chown root:bitlbee /etc/bitlbee/bitlbee.conf
-else
-	chmod 600 /etc/bitlbee/bitlbee.conf
-	chown bitlbee /etc/bitlbee/bitlbee.conf
-fi
-
-if [ -z "$2" ]; then
-	if which invoke-rc.d >/dev/null 2>&1; then
-		invoke-rc.d bitlbee start
-	else
-		/etc/init.d/bitlbee start
-	fi
-fi
Index: debian/postrm
===================================================================
--- debian/postrm	(revision e693ac27b08e60c325223a0797dbf49ed2dbc3b3)
+++ 	(revision )
@@ -1,14 +1,0 @@
-#!/bin/sh -e
-
-[ "$1" = "purge" ] || exit 0
-
-if [ -e /usr/share/debconf/confmodule ]; then
-	. /usr/share/debconf/confmodule;
-	db_purge;
-fi
-
-update-rc.d bitlbee remove > /dev/null 2>&1 || true
-rm -f /etc/default/bitlbee
-
-deluser --system --remove-home bitlbee || true
-rm -rf /var/lib/bitlbee ## deluser doesn't seem to do this for homedirs in /var
Index: debian/prerm
===================================================================
--- debian/prerm	(revision e693ac27b08e60c325223a0797dbf49ed2dbc3b3)
+++ 	(revision )
@@ -1,17 +1,0 @@
-#!/bin/sh -e
-
-if [ "$1" = "upgrade" ]; then
-	## To prevent the help function from breaking in currently running
-	## BitlBee processes. Have to do it like this because dpkg-reconfigure
-	## looks a lot like an upgrade and we don't want to lose help.txt...
-	if [ -e /usr/share/bitlbee/help.txt ]; then
-		rm -f /usr/share/bitlbee/help.upgrading
-		mv /usr/share/bitlbee/help.txt /usr/share/bitlbee/help.upgrading
-	fi
-else
-	if which invoke-rc.d >/dev/null 2>&1; then
-		invoke-rc.d bitlbee stop || exit 0
-	else
-		/etc/init.d/bitlbee stop || exit 0
-	fi
-fi
Index: debian/rules
===================================================================
--- debian/rules	(revision e693ac27b08e60c325223a0797dbf49ed2dbc3b3)
+++ debian/rules	(revision 2bfe976616cc80439257959411c3bb20f2ab2e6b)
@@ -1,113 +1,104 @@
 #!/usr/bin/make -f
+#
+# Finally switching to debhelper.
+#
+# Not using debhelper was an exercise suggested to me by my AM (Gergely
+# Nagy). It was educating at the time but I finally decided that the
+# exercise is over now.
+#
 
+BITLBEE_CONFIGURE_FLAGS ?=
 DEBUG ?= 0
 
-ifdef BITLBEE_VERSION
-BITLBEE_FORCE_VERSION=1
-else
+ifndef BITLBEE_VERSION
 # Want to use the full package version number instead of just the release.
-BITLBEE_VERSION ?= "$(shell dpkg-parsechangelog | grep ^Version: | awk '{print $$2}')"
-export BITLBEE_VERSION
+BITLBEE_CONFIGURE_VERSION ?= BITLBEE_VERSION=\"$(shell dpkg-parsechangelog | grep ^Version: | awk '{print $$2}')\"
 endif
 
-build-arch: build-arch-stamp
-build-arch-stamp:
-	[ -d debian ]
-	./configure --debug=$(DEBUG) --prefix=/usr --etcdir=/etc/bitlbee --events=libevent
-	$(MAKE)
-#	$(MAKE) -C doc/ all
-	touch build-arch-stamp
+build: build-stamp
+build-stamp:
+	dh_testdir
+
+	mkdir -p debian/build-native
+	ROOT=$$PWD; cd debian/build-native; $(BITLBEE_CONFIGURE_VERSION) $$ROOT/configure --debug=$(DEBUG) --prefix=/usr --etcdir=/etc/bitlbee --events=libevent $(BITLBEE_CONFIGURE_FLAGS)
+	$(MAKE) -C debian/build-native
+
+	mkdir -p debian/build-libpurple
+	ROOT=$$PWD; cd debian/build-libpurple; $(BITLBEE_CONFIGURE_VERSION) $$ROOT/configure --debug=$(DEBUG) --prefix=/usr --etcdir=/etc/bitlbee --purple=1 $(BITLBEE_CONFIGURE_FLAGS)
+	$(MAKE) -C debian/build-libpurple
+
+	$(MAKE) -C doc
+
+	touch build-stamp
 
 clean:
-	[ "`whoami`" = "root" -a -d debian ]
-	rm -rf build-arch-stamp debian/bitlbee debian/*.substvars debian/files debian/bitlbee-dev
+	dh_testdir
+	dh_testroot
+	rm -f build-stamp
+
+	rm -rf build-arch-stamp debian/build-*
 	$(MAKE) distclean
-#	-$(MAKE) -C doc/ clean
-				
 
-install-arch: build-arch
-	[ "`whoami`" = "root" -a -d debian ]
-	mkdir -p debian/bitlbee/DEBIAN/
-	$(MAKE) install install-etc DESTDIR=`pwd`/debian/bitlbee
+	dh_clean
 
-	mkdir -p debian/bitlbee/usr/share/doc/bitlbee/
-	cp doc/user-guide/user-guide.txt debian/bitlbee/usr/share/doc/bitlbee/
-	cp doc/user-guide/user-guide.html debian/bitlbee/usr/share/doc/bitlbee/
+install: build
+	dh_testdir
+	dh_testroot
+	dh_clean -k
+	dh_installdirs
 
-install-indep: install-arch
-	[ "`whoami`" = "root" -a -d debian ]
-	mkdir -p debian/bitlbee-dev/DEBIAN/
-	$(MAKE) install-dev DESTDIR=`pwd`/debian/bitlbee-dev
+	$(MAKE) -C debian/build-native install install-etc DESTDIR=`pwd`/debian/bitlbee
+	$(MAKE) -C debian/build-libpurple install install-etc DESTDIR=`pwd`/debian/bitlbee-libpurple
+	$(MAKE) -C debian/build-native install-dev DESTDIR=`pwd`/debian/bitlbee-dev
 
-	mkdir -p debian/bitlbee-dev/usr/share/doc/bitlbee-dev/
+	patch debian/bitlbee/etc/bitlbee/bitlbee.conf debian/patches/bitlbee.conf.diff
+	patch debian/bitlbee-libpurple/etc/bitlbee/bitlbee.conf debian/patches/bitlbee.conf.diff
 
-binary-arch: build-arch install-arch
-	[ "`whoami`" = "root" -a -d debian ]
+	mkdir -p debian/bitlbee-common/usr
+	mv debian/bitlbee/usr/share debian/bitlbee-common/usr
+	rm -rf debian/bitlbee-libpurple/usr/share
 
-	chmod 755 debian/post* debian/pre* debian/config debian/bitlbee.init
+binary-common:
+	dh_testdir
+	dh_testroot
 
-	mkdir -p debian/bitlbee/usr/share/doc/bitlbee/examples/ debian/bitlbee/etc/init.d/
-	-cp doc/RELEASE-SPEECH* debian/bitlbee/usr/share/doc/bitlbee/ && gzip -9 debian/bitlbee/usr/share/doc/bitlbee/RELEASE-SPEECH*
-	cp doc/CREDITS doc/AUTHORS doc/README doc/FAQ debian/README.Debian debian/bitlbee/usr/share/doc/bitlbee/
-	cp debian/changelog debian/bitlbee/usr/share/doc/bitlbee/changelog.Debian
-	cp debian/copyright debian/bitlbee/usr/share/doc/bitlbee/copyright
-	cp doc/CHANGES debian/bitlbee/usr/share/doc/bitlbee/changelog
-	cp utils/* debian/bitlbee/usr/share/doc/bitlbee/examples/
-	cp debian/bitlbee.init debian/bitlbee/etc/init.d/bitlbee
-	patch -p0 < debian/patches/bitlbee.conf.diff
-	cd debian/bitlbee/usr/share/; \
-		gzip -9 doc/bitlbee/changelog.Debian doc/bitlbee/changelog doc/bitlbee/user-guide.txt \
-		        doc/bitlbee/examples/* man/man8/bitlbee.8 man/man5/bitlbee.conf.5
-	
-	chown -R root:root debian/bitlbee/
-	find debian/bitlbee/usr/share/ -type d -exec chmod 755 {} \;
-	find debian/bitlbee/usr/share/ -type f -exec chmod 644 {} \;
-	
-	cp debian/prerm debian/bitlbee/DEBIAN/
-	cp debian/postinst debian/bitlbee/DEBIAN/
-	cp debian/postrm debian/bitlbee/DEBIAN/
-	cp debian/config debian/bitlbee/DEBIAN/
+	dh_installchangelogs doc/CHANGES
+	dh_installexamples
+	dh_installdocs #--link-doc=bitlbee-common
+	# TODO: Restore --link-doc up here and remove the hack below once
+	# Hardy and Lenny are deprecated.
+	for p in bitlbee bitlbee-libpurple bitlbee-dev; do rm -rf debian/$$p/usr/share/doc/$$p; ln -s bitlbee-common debian/$$p/usr/share/doc/$$p; done
+	dh_installdebconf
+	dh_installinit
+ifeq ($(DH_OPTIONS),-a)
+	cp -a debian/bitlbee/etc debian/bitlbee-libpurple
+endif
+	dh_installman
+	dh_strip
+	dh_link
+	dh_compress
+	dh_fixperms
+	dh_installdeb
+ifeq ($(DH_OPTIONS),-a)
+	cp -a debian/bitlbee/DEBIAN/post* debian/bitlbee/DEBIAN/pre* debian/bitlbee-libpurple/DEBIAN
+endif
+	dh_shlibdeps
+ifdef BITLBEE_VERSION
+	dh_gencontrol -- -v1:$(BITLBEE_VERSION)-0  -Vbee:Version=1:$(BITLBEE_VERSION)-0
+else
+	dh_gencontrol -- -Vbee:Version=$(shell dpkg-parsechangelog | grep ^Version: | awk '{print $$2}' | sed -e 's/+[^+]*$$//')
+endif
+	dh_md5sums
+	dh_builddeb
 
-	po2debconf debian/templates > debian/bitlbee/DEBIAN/templates
-	cp debian/conffiles debian/bitlbee/DEBIAN/
-	
-	if [ "$(DEBUG)" = "0" ]; then strip -R .comment -R .note debian/bitlbee/usr/sbin/bitlbee; fi
+binary-indep: build install
+	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common
 
-	cd debian/bitlbee; \
-		find usr -type f -exec md5sum {} \; > DEBIAN/md5sums
-	dpkg-shlibdeps -Tdebian/bitlbee.substvars -dDepends debian/bitlbee/usr/sbin/bitlbee
-ifdef BITLBEE_FORCE_VERSION
-	dpkg-gencontrol -ldebian/changelog -isp -pbitlbee -Tdebian/bitlbee.substvars -Pdebian/bitlbee -v1:$(BITLBEE_VERSION)-0 -V'debconf-depends=debconf (>= 1.2.0) | debconf-2.0'
-else
-	dpkg-gencontrol -ldebian/changelog -isp -pbitlbee -Tdebian/bitlbee.substvars -Pdebian/bitlbee -V'debconf-depends=debconf (>= 1.2.0) | debconf-2.0'
-endif
+binary-arch: build install
+	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common
 
-	dpkg --build debian/bitlbee ..
+binary-%: build install
+	make -f debian/rules binary-common DH_OPTIONS=-p$*
 
-binary-indep: install-indep
-	[ "`whoami`" = "root" -a -d debian ]
-
-	chown -R root.root debian/bitlbee-dev/
-	find debian/bitlbee-dev/usr/share/ -type d -exec chmod 755 {} \;
-	find debian/bitlbee-dev/usr/share/ -type f -exec chmod 644 {} \;
-
-	cp debian/changelog debian/bitlbee-dev/usr/share/doc/bitlbee-dev/changelog.Debian
-	gzip -9 debian/bitlbee-dev/usr/share/doc/bitlbee-dev/changelog.Debian
-	cp debian/copyright debian/bitlbee-dev/usr/share/doc/bitlbee-dev/copyright
-
-	cd debian/bitlbee-dev; \
-		find usr -type f -exec md5sum {} \; > DEBIAN/md5sums
-
-ifdef BITLBEE_FORCE_VERSION
-	dpkg-gencontrol -ldebian/changelog -isp -pbitlbee-dev -Pdebian/bitlbee-dev -v1:$(BITLBEE_VERSION)-0
-else
-	dpkg-gencontrol -ldebian/changelog -isp -pbitlbee-dev -Pdebian/bitlbee-dev
-endif
-
-	dpkg --build debian/bitlbee-dev ..
-
-binary: binary-arch binary-indep
-build: build-arch
-install: install-arch install-indep
-
-.PHONY: build-arch build clean binary-arch binary install-arch install binary-indep install-indep
+binary: binary-indep binary-arch
+.PHONY: build clean binary-indep binary-arch binary-common binary install
Index: debian/templates
===================================================================
--- debian/templates	(revision e693ac27b08e60c325223a0797dbf49ed2dbc3b3)
+++ 	(revision )
@@ -1,8 +1,0 @@
-Template: bitlbee/serveport
-Type: string
-Default: stillhavetoask
-_Description: On what TCP port should BitlBee listen for connections?
- BitlBee normally listens on the regular IRC port, 6667. This might not be
- a very good idea when you're running a real IRC daemon as well. 6668 might
- be a good alternative. Leaving this value blank means that BitlBee will not
- be run automatically.
