#! /usr/bin/make -f
#
#	Build script for the kvirc debian package.
#

export DH_VERBOSE=1
export DH_COMPAT=3

SHELL=/bin/sh -e

CWD=$(shell pwd)

OPTS=--with-kde-support --with-ipv6-support --with-charset-translation --with-new-pics
OPTS+=--disable-mt --with-font-antialiasing

export CXXFLAGS=-fno-exceptions -Wall

export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

OPTS+=--build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE)

#
# Due to compiler problems, don't build with optimizations on Alpha
#
ifneq (,$(findstring alpha,$(DEB_BUILD_GNU_CPU)))
	export CXXFLAGS+=-O0 -mieee
endif

#
# Turn on optimizations on x86 machines
#
ifneq (,$(findstring 86,$(DEB_BUILD_GNU_CPU)))
	export CXXFLAGS+=-march=$(DEB_BUILD_GNU_CPU)
	OPTS+=--with-i386-asm --with-o3-optimisation
endif

#
# If debug is specified, turn it on
#
ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	export CXXFLAGS+=-g
	OPTS+=--with-debug-symbols --enable-debug
else
	OPTS+=--without-debug-symbols --disable-debug
endif

configure: configure-stamp
configure-stamp:
	dh_testdir
	#
	# Prevent time-skew issues on some systems
	#
	touch configure.in		\
		&& sleep 2s			\
		&& touch aclocal.m4	\
		&& sleep 2s			\
		&& touch configure
	#
	# Configure it
	# If font anti-aliasing is not compiled into Qt, we configure it
	# differently and don't check for compilation.
	# If libqt-mt is not installed, fall back on libqt
	#
	   ./configure --with-pipes --prefix=/usr $(OPTS)
	#
	# Put the right prefix in the man page
	#
	perl -pi -e 's,\\fB\$$PREFIX\\fP,\/usr,g' data/man/kvirc.1
	touch configure-stamp

build: configure build-stamp
build-stamp:
	dh_testdir
	$(MAKE)
	[ -f changelog ] || cp -a ChangeLog changelog
	touch build-stamp

clean: autotools
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	-$(MAKE) clean
	rm -rf $(CWD)/debian/kvirc
	rm -rf $(CWD)/debian/kvirc-doc
	rm -rf $(CWD)/debian/kvirc-dev
	dh_clean

autotools:
	OLDDATESUB=`./config.sub -t | tr -d -`;						\
	OLDDATEGUESS=`./config.guess -t | tr -d -`;					\
	NEWDATESUB=`/usr/share/misc/config.sub -t | tr -d -`;		\
	NEWDATEGUESS=`/usr/share/misc/config.guess -t | tr -d -`;	\
	if [ $$OLDDATESUB   -lt $$NEWDATESUB -o						\
		 $$OLDDATEGUESS -lt $$NEWDATEGUESS ];					\
	then														\
		dch -a -p "GNU config automated update: config.sub\
		 ($$OLDDATESUB to $$NEWDATESUB), config.guess\
		 ($$OLDDATEGUESS to $$NEWDATEGUESS)";					\
		cp -f /usr/share/misc/config.sub config.sub;			\
		cp -f /usr/share/misc/config.guess config.guess;		\
		echo "WARNING: GNU config scripts updated!" 1>&2;		\
	fi

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	$(MAKE) install DESTDIR=$(CWD)/debian/kvirc
	$(MAKE) docs    DESTDIR=$(CWD)/debian/kvirc-doc
	#
	# Fix Lintian warnings about man pages in the wrong location
	#
	mv $(CWD)/debian/kvirc/usr/man $(CWD)/debian/kvirc/usr/share/
	#
	# Fix Lintian warnings about missing manpages
	#
	set -e; \
	for F in kvi_make_scriptdist.sh kvi_run_netscape.sh kvi_search_help.sh kvirc-config; \
	do \
		ln -s kvirc.1.gz $(CWD)/debian/kvirc/usr/share/man/man1/$$F.1.gz; \
	done
	#
	# Fix Lintian warnings about .so library names
	#
	mkdir -p $(CWD)/debian/kvirc-dev/usr/lib
	mv $(CWD)/debian/kvirc/usr/lib/*.so $(CWD)/debian/kvirc-dev/usr/lib/
	#
	# Move the header files to the -dev package
	#
	mkdir -p $(CWD)/debian/kvirc-dev/usr
	mv $(CWD)/debian/kvirc/usr/include $(CWD)/debian/kvirc-dev/usr/
	#
	# We want to have kvirc-config be part of kvirc-dev, not kvirc proper
	#
	mkdir -p $(CWD)/debian/kvirc-dev/usr/bin $(CWD)/debian/kvirc-dev/usr/share/man/man1
	mv $(CWD)/debian/kvirc/usr/bin/kvirc-config $(CWD)/debian/kvirc-dev/usr/bin/
	mv $(CWD)/debian/kvirc/usr/share/man/man1/kvirc-config.1.gz $(CWD)/debian/kvirc-dev/usr/share/man/man1/
	#
	# Move static libraries to the -dev package as well
	#
	mkdir -p $(CWD)/debian/kvirc-dev/usr/lib/ $(CWD)/debian/kvirc-dev/usr/share/kvirc/plugins/
	mv $(CWD)/debian/kvirc/usr/lib/*.la $(CWD)/debian/kvirc-dev/usr/lib/
	mv $(CWD)/debian/kvirc/usr/share/kvirc/plugins/*.la $(CWD)/debian/kvirc-dev/usr/share/kvirc/plugins/

binary-indep:

binary-arch: install
	dh_testdir
	dh_testroot
	dh_installdocs -pkvirc changelog IDEAS README TODO
	dh_installdocs -pkvirc-dev doc/Plugins-devel-HOWTO doc/basicplugin.tar.gz
	dh_installdocs -pkvirc-doc doc/CURIOSITY doc/Etiquette doc/FAQ doc/IrcManual doc/TaoOfIrc doc/alt-irc-faq doc/rfc2810.txt doc/rfc2811.txt doc/rfc2812.txt doc/rfc2813.txt
	dh_installmenu
	dh_installchangelogs
ifeq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	dh_strip
endif
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_makeshlibs
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install configure autotools
