#!/usr/bin/make -f
# debian/rules file to build packages from wxWindows CVS source
#
# Sculpted 13/2/2000 by Ron Lee <ron@debian.org> from new and
# variously stolen code :-)
# (including a debhelper template, GNU copyright 1997 to 1999 by
# Joey Hess, and some ideas found in the ncurses rules file that
# I quite liked.  Thanks!)

#export DH_VERBOSE=1
export DH_COMPAT=2

release:=$(shell dpkg-parsechangelog | sed -n 's/^Source: wxwindows//p')

# Packages to build:
package_gtk_lib=libwxgtk$(release)
package_gtk_dev=libwxgtk$(release)-dev
package_gtk_py=libwxgtk$(release)-python
package_doc=wxwin$(release)-doc

# Build directories:
objdir_gtk_shared=objs_gtk_sh
objdir_gtk_static=objs_gtk_st
objdir_doc_cruft=objs_doc_con
objdir_doc=docs/wxWindows-manual.html
objdirs=$(objdir_gtk_shared) $(objdir_gtk_static) $(objdir_doc)

build_stamps=build-gtk-shared-stamp build-gtk-static-stamp \
             build-gtk-python-stamp build-doc-stamp

wxconfig:=$(shell pwd)/$(objdir_gtk_shared)/wx-config \
		--prefix=$(shell pwd) \
		--exec-prefix=$(shell pwd)/$(objdir_gtk_shared)


# The Rules:

debian/control: debian/control.in
	sed -e 's/=V/$(release)/g' < debian/control.in > debian/control

debian/wxwin-doc.doc-base: debian/wxwin-doc.doc-base.in
	sed -e 's/=V/$(release)/g' < debian/wxwin-doc.doc-base.in > debian/wxwin-doc.doc-base

build: debian/control debian/wxwin-doc.doc-base $(build_stamps)

build-gtk-shared-stamp:
	dh_testdir
	mkdir $(objdir_gtk_shared)
	cd $(objdir_gtk_shared) \
		&& ../configure --prefix=/usr --with-gtk --enable-burnt_name --disable-newgrid \
		&& $(MAKE)
	touch $@

build-gtk-static-stamp:
	dh_testdir
	mkdir $(objdir_gtk_static)
	cd $(objdir_gtk_static) \
		&& ../configure --prefix=/usr --with-gtk --disable-shared \
		&& $(MAKE)
	touch $@

build-gtk-python-stamp: build-gtk-shared-stamp
	dh_testdir
	cd utils/wxPython/src \
		&& touch gtk/*.cpp gtk/*.py \
		&& ../distrib/build.py -b WXDIR=../../.. WXCONFIG='$(wxconfig)'
	cd utils/wxPython/modules/html \
		&& ../../distrib/build.py -b WXDIR=../../../.. WXCONFIG='$(wxconfig)'
	cd utils/wxPython/modules/utils \
		&& ../../distrib/build.py -b WXDIR=../../../.. WXCONFIG='$(wxconfig)'
	touch $@

build-doc-stamp: build-gtk-shared-stamp
	dh_testdir
	cd $(objdir_gtk_shared)/utils/tex2rtf/src \
		&& $(MAKE)
	mkdir $(objdir_doc)
	mkdir $(objdir_doc_cruft)
	cd $(objdir_doc_cruft) \
		&& LD_LIBRARY_PATH=../$(objdir_gtk_shared)/lib \
		   ../$(objdir_gtk_shared)/utils/tex2rtf/src/tex2rtf \
		   ../docs/latex/wx/manual.tex ../$(objdir_doc)/wxwin -twice -html
	cp docs/latex/wx/*.gif $(objdir_doc)
	rm -rf $(objdir_doc_cruft)
	rm -f $(objdir_doc)/wxwin.con $(objdir_doc)/wxwin.hh* \
	      $(objdir_doc)/wxwin.htx $(objdir_doc)/wxwin.ref
	touch $@

clean: debian/control
	dh_testdir
	dh_testroot
	rm -rf $(build_stamps) $(objdirs)
	-cd utils/wxPython/src && $(MAKE) clean
	-cd utils/wxPython/modules/html && $(MAKE) clean
	-cd utils/wxPython/modules/utils && $(MAKE) clean
	dh_clean
	rm -f debian/$(package_gtk_lib).*
	rm -f debian/$(package_gtk_dev).*
	rm -f debian/$(package_gtk_py).*
	rm -f debian/$(package_doc).*

install: build
	dh_testdir
	dh_testroot
	@for f in dirs docs files postinst; do \
		echo "generating control file $(package_gtk_lib).$$f"; \
		cp debian/libwxgtk.$$f debian/$(package_gtk_lib).$$f; \
	done;
	@for f in dirs docs files; do \
		echo "generating control file $(package_gtk_dev).$$f"; \
		cp debian/libwxgtk-dev.$$f debian/$(package_gtk_dev).$$f; \
	done;
	@for f in dirs docs files postinst prerm; do \
		echo "generating control file $(package_gtk_py).$$f"; \
		cp debian/libwxgtk-python.$$f debian/$(package_gtk_py).$$f; \
	done;
	@for f in dirs docs doc-base; do \
		echo "generating control file $(package_doc).$$f"; \
		cp debian/wxwin-doc.$$f debian/$(package_doc).$$f; \
	done;
	dh_clean -k
	dh_installdirs
	cd $(objdir_gtk_shared) \
		&& $(MAKE) install prefix=`pwd`/../debian/tmp/usr
	cp $(objdir_gtk_static)/lib/*.a debian/tmp/usr/lib
	cd utils/wxPython/src \
		&& ../distrib/build.py -i \
			TARGETDIR=../../../debian/tmp/usr/lib/python1.5/site-packages/wxPython \
			HELPERLIBDIR=../../../debian/tmp/usr/lib \
			WXDIR=../../..
	cd utils/wxPython/modules/html \
		&& ../../distrib/build.py -i \
			TARGETDIR=../../../../debian/tmp/usr/lib/python1.5/site-packages/wxPython \
			WXDIR=../../../..
	cd utils/wxPython/modules/utils \
		&& ../../distrib/build.py -i \
			TARGETDIR=../../../../debian/tmp/usr/lib/python1.5/site-packages/wxPython \
			WXDIR=../../../..
	find debian/tmp/usr/lib/python1.5/site-packages/wxPython \
		-name '*.py?' -exec rm '{}' ';'
	dh_movefiles


# Build architecture-independent files here.
binary-indep: build install
# nothing to do.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installmanpages -p$(package_gtk_dev) \
		ansi2knr.1 \
		jpegtran.1 \
		libpng.3 \
		libpngpf.3 \
		zlib.3 \
		png.5
	dh_installchangelogs 
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps -ldebian/$(package_gtk_lib)/usr/lib:debian/$(package_gtk_py)/usr/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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