#!/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 :-)
# It makes copious use of the debhelper utilities written by
# Joey Hess and others.

#export DH_VERBOSE=1
export DH_COMPAT=2
export DH_OPTIONS

release:=$(shell dpkg-parsechangelog | sed -n 's/^Source: wxwindows//p')
python_dir:=python$(shell python -c "import sys;print sys.version[:3]")

# Packages to build:
package_wxbase_lib=libwxbase$(release)
package_wxbase_dev=libwxbase$(release)-dev
package_wxbase_dbg=libwxbase$(release)-dbg
package_gtk_lib=libwxgtk$(release)
package_gtk_dev=libwxgtk$(release)-dev
package_gtk_dbg=libwxgtk$(release)-dbg
package_gtk_py=libwxgtk$(release)-python
package_gtk_contrib=libwxgtk$(release)-contrib
package_gtk_contrib_dev=libwxgtk$(release)-contrib-dev
package_headers=wxwin$(release)-headers
package_i18n=wxwin$(release)-i18n
package_doc=wxwin$(release)-doc
package_examples=wxwin$(release)-examples

# Build directories:
objdir_wxbase_shared=objs_wxbase_sh
objdir_wxbase_static=objs_wxbase_st
objdir_wxbase_debug=objs_wxbase_d
objdir_gtk_shared=objs_gtk_sh
objdir_gtk_static=objs_gtk_st
objdir_gtk_debug=objs_gtk_d
objdir_doc_cruft=objs_doc_con
objdir_doc=docs/wxWindows-manual.html
objdir_examples=docs/examples
objdir_i18n=locale
objdirs=$(objdir_wxbase_shared) $(objdir_wxbase_static) $(objdir_wxbase_debug) \
	$(objdir_gtk_shared) $(objdir_gtk_static) $(objdir_gtk_debug) \
	$(objdir_doc) $(objdir_examples)

#	note that the i18n package is actually arch indep (once built)
#	but must be built (and installed) during the arch any phase as
#	it's pulled out of the wxGtk shared lib package.

# Build stamps:
build_arch_stamps=build-wxbase-shared-stamp build-wxbase-static-stamp \
                  build-wxbase-debug-stamp build-gtk-shared-stamp \
                  build-gtk-static-stamp build-gtk-debug-stamp \
                  build-contrib-shared-stamp build-contrib-static-stamp \
                  build-gtk-py-stamp build-i18n-stamp

build_indep_stamps=build-examples-stamp build-doc-stamp

build_stamps=$(build_arch_stamps) $(build_indep_stamps)

# Install targets:
install_all_arch=install-wxbase-lib install-wxbase-dev install-wxbase-dbg \
                 install-gtk-lib install-gtk-dev install-gtk-dbg \
                 install-gtk-contrib install-gtk-contrib-dev install-gtk-py \
                 install-headers install-i18n

install_all_indep=install-examples install-doc

install_all=$(install_all_arch) $(install_all_indep)


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

control-files-stamp: debian/control debian/wxwin-doc.doc-base
	dh_testdir
	@for f in dirs postinst; do \
		echo "generating control file $(package_wxbase_lib).$$f"; \
		cp debian/libwxbase.$$f debian/$(package_wxbase_lib).$$f; \
	done;
	@for f in dirs files links postinst prerm; do \
		echo "generating control file $(package_wxbase_dev).$$f"; \
		cp debian/libwxbase-dev.$$f debian/$(package_wxbase_dev).$$f; \
	done;
	@for f in dirs links postinst prerm; do \
		echo "generating control file $(package_wxbase_dbg).$$f"; \
		cp debian/libwxbase-dbg.$$f debian/$(package_wxbase_dbg).$$f; \
	done;
	@for f in dirs postinst; do \
		echo "generating control file $(package_gtk_lib).$$f"; \
		cp debian/libwxgtk.$$f debian/$(package_gtk_lib).$$f; \
	done;
	@for f in dirs files links postinst prerm; do \
		echo "generating control file $(package_gtk_dev).$$f"; \
		cp debian/libwxgtk-dev.$$f debian/$(package_gtk_dev).$$f; \
	done;
	@for f in dirs links postinst prerm; do \
		echo "generating control file $(package_gtk_dbg).$$f"; \
		cp debian/libwxgtk-dbg.$$f debian/$(package_gtk_dbg).$$f; \
	done;
	@for f in dirs docs files postinst prerm; do \
		echo "generating control file $(package_gtk_py).$$f"; \
		sed -e 's/=PY/$(python_dir)/g;s/=V/$(release)/g' < debian/libwxgtk-python.$$f \
			> debian/$(package_gtk_py).$$f; \
	done;
	@for f in dirs postinst; do \
		echo "generating control file $(package_gtk_contrib).$$f"; \
		cp debian/libwxgtk-contrib.$$f debian/$(package_gtk_contrib).$$f; \
	done;
	@for f in dirs files; do \
		echo "generating control file $(package_gtk_contrib_dev).$$f"; \
		cp debian/libwxgtk-contrib-dev.$$f debian/$(package_gtk_contrib_dev).$$f; \
	done;
	@for f in dirs files; do \
		echo "generating control file $(package_headers).$$f"; \
		cp debian/wxwin-headers.$$f debian/$(package_headers).$$f; \
	done;
	@for f in dirs files; do \
		echo "generating control file $(package_i18n).$$f"; \
		cp debian/wxwin-i18n.$$f debian/$(package_i18n).$$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;
	@for f in examples; do \
		echo "generating control file $(package_examples).$$f"; \
		cp debian/wxwin-examples.$$f debian/$(package_examples).$$f; \
	done;
	touch $@

build_arch: control-files-stamp $(build_arch_stamps)
build_all: control-files-stamp $(build_stamps)

# do nothing until we know which binary target is called
build:


build-wxbase-shared-stamp:
	dh_testdir
	mkdir $(objdir_wxbase_shared)
	cd $(objdir_wxbase_shared) \
		&& ../configure --prefix=/usr \
				--cache-file=../config_deb.cache \
				--disable-gui \
				--enable-burnt_name \
				--with-zlib=sys \
		&& $(MAKE)
	touch $@

build-wxbase-static-stamp:
	dh_testdir
	mkdir $(objdir_wxbase_static)
	cd $(objdir_wxbase_static) \
		&& ../configure --prefix=/usr \
				--cache-file=../config_deb.cache \
				--disable-gui \
				--disable-shared \
				--with-zlib=sys \
		&& $(MAKE)
	touch $@

build-wxbase-debug-stamp:
	dh_testdir
	mkdir $(objdir_wxbase_debug)
	cd $(objdir_wxbase_debug) \
		&& ../configure --prefix=/usr \
				--cache-file=../config_deb.cache \
				--disable-gui \
				--enable-debug \
				--enable-burnt_name \
				--with-zlib=sys \
		&& $(MAKE)
	touch $@

build-gtk-shared-stamp:
	dh_testdir
	mkdir $(objdir_gtk_shared)
	cd $(objdir_gtk_shared) \
		&& ../configure --prefix=/usr \
				--cache-file=../config_deb.cache \
				--with-gtk \
				--with-opengl \
				--enable-burnt_name \
				--with-zlib=sys \
				--with-libjpeg=sys \
				--with-libpng=sys \
				--with-libtiff=sys \
		&& $(MAKE)
	touch $@

build-gtk-static-stamp:
	dh_testdir
	mkdir $(objdir_gtk_static)
	cd $(objdir_gtk_static) \
		&& ../configure --prefix=/usr \
				--cache-file=../config_deb.cache \
				--with-gtk \
				--with-opengl \
				--disable-shared \
				--with-zlib=sys \
				--with-libjpeg=sys \
				--with-libpng=sys \
				--with-libtiff=sys \
		&& $(MAKE)
	touch $@

build-gtk-debug-stamp:
	dh_testdir
	mkdir $(objdir_gtk_debug)
	cd $(objdir_gtk_debug) \
		&& ../configure --prefix=/usr \
				--cache-file=../config_deb.cache \
				--with-gtk \
				--with-opengl \
				--enable-debug \
				--enable-burnt_name \
				--with-zlib=sys \
				--with-libjpeg=sys \
				--with-libpng=sys \
				--with-libtiff=sys \
		&& $(MAKE)
	touch $@

build-contrib-shared-stamp: build-gtk-shared-stamp
	dh_testdir
	cd $(objdir_gtk_shared)/contrib/src \
		&& $(MAKE)
	touch $@

build-contrib-static-stamp: build-gtk-static-stamp
	dh_testdir
	cd $(objdir_gtk_static)/contrib/src \
		&& $(MAKE)
	touch $@

build-gtk-py-stamp: build-gtk-shared-stamp
	dh_testdir
	touch docs/lgpl.txt
	cd wxPython \
		&& ./setup.py build IN_CVS_TREE=1 WX_CONFIG='$(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.htm -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 $@

build-examples-stamp:
	dh_testdir
	mkdir $(objdir_examples)
	cp -a samples $(objdir_examples)
	cp -a demos $(objdir_examples)
	cp -a wxPython/demo $(objdir_examples)/wxPython
	@for d in $(objdir_examples)/demos $(objdir_examples)/samples; do \
		(cd $$d \
			&& mv Makefile.in Makefile \
			&& rm -f configure* \
			&& find -name 'Makefile.in' -exec rm -f '{}' ';' \
			&& for f in $$(find -type d); do \
				if [ -f $$f/makefile.unx ]; then \
					mv $$f/makefile.unx $$f/Makefile; \
				fi; \
			done) \
	done;
	touch $@

build-i18n-stamp: build-gtk-shared-stamp
	dh_testdir
	cd $(objdir_i18n) \
		&& $(MAKE) allmo
	touch $@

clean: debian/control
	dh_testdir
	dh_testroot
	rm -rf config_deb.cache control-files-stamp $(build_stamps) $(objdirs)
	rm -f docs/lgpl.txt
	cd wxPython && ./setup.py clean
	dh_clean
	rm -f debian/$(package_wxbase_lib).*
	rm -f debian/$(package_wxbase_dev).*
	rm -f debian/$(package_wxbase_dbg).*
	rm -f debian/$(package_gtk_lib).*
	rm -f debian/$(package_gtk_dev).*
	rm -f debian/$(package_gtk_dbg).*
	rm -f debian/$(package_gtk_py).*
	rm -f debian/$(package_gtk_contrib).*
	rm -f debian/$(package_gtk_contrib_dev).*
	rm -f debian/$(package_headers).*
	rm -f debian/$(package_i18n).*
	rm -f debian/$(package_doc).*
	rm -f debian/$(package_examples).*

install_arch: build_arch $(install_all_arch)

install: build_all $(install_all)

install-wxbase-lib: DH_OPTIONS=-p$(package_wxbase_lib)
install-wxbase-lib: build-wxbase-shared-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	cp -a $(objdir_wxbase_shared)/lib/* debian/$(package_wxbase_lib)/usr/lib
	cp $(objdir_wxbase_shared)/wxbase-config debian/$(package_wxbase_lib)/usr/bin/

install-wxbase-dev: DH_OPTIONS=-p$(package_wxbase_dev)
install-wxbase-dev: build-wxbase-static-stamp install-wxbase-lib
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	dh_movefiles --sourcedir=debian/$(package_wxbase_lib)
	cp $(objdir_wxbase_static)/lib/*.a debian/$(package_wxbase_dev)/usr/lib

install-wxbase-dbg: DH_OPTIONS=-p$(package_wxbase_dbg)
install-wxbase-dbg: build-wxbase-debug-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	cp -a $(objdir_wxbase_debug)/lib/* debian/$(package_wxbase_dbg)/usr/lib
	cp $(objdir_wxbase_debug)/wxbased-config debian/$(package_wxbase_dbg)/usr/bin/
	cp debian/lintian-override debian/$(package_wxbase_dbg)/usr/share/lintian/overrides/$(package_wxbase_dbg)

install-gtk-lib: DH_OPTIONS=-p$(package_gtk_lib)
install-gtk-lib: build-gtk-shared-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs 
	cd $(objdir_gtk_shared) \
		&& $(MAKE) install prefix=`pwd`/../debian/$(package_gtk_lib)/usr
	rm -f debian/$(package_gtk_lib)/usr/bin/wx-config

install-gtk-dev: DH_OPTIONS=-p$(package_gtk_dev)
install-gtk-dev: build-gtk-static-stamp install-gtk-lib
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	dh_movefiles --sourcedir=debian/$(package_gtk_lib)
	cp $(objdir_gtk_static)/lib/libwx_gtk*.a debian/$(package_gtk_dev)/usr/lib

install-gtk-dbg: DH_OPTIONS=-p$(package_gtk_dbg)
install-gtk-dbg: build-gtk-debug-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	cp -a $(objdir_gtk_debug)/lib/* debian/$(package_gtk_dbg)/usr/lib
	cp $(objdir_gtk_debug)/wxgtkd-config debian/$(package_gtk_dbg)/usr/bin/
	cp debian/lintian-override debian/$(package_gtk_dbg)/usr/share/lintian/overrides/$(package_gtk_dbg)

install-gtk-contrib: DH_OPTIONS=-p$(package_gtk_contrib)
install-gtk-contrib: build-contrib-shared-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	cd $(objdir_gtk_shared)/contrib/src \
		&& $(MAKE) install prefix=`pwd`/../../../debian/$(package_gtk_contrib)/usr

install-gtk-contrib-dev: DH_OPTIONS=-p$(package_gtk_contrib_dev)
install-gtk-contrib-dev: build-contrib-static-stamp install-gtk-dev
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	dh_movefiles --sourcedir=debian/$(package_gtk_contrib)
	cp $(objdir_gtk_static)/lib/*.a debian/$(package_gtk_contrib_dev)/usr/lib
	rm -f debian/$(package_gtk_contrib_dev)/usr/lib/libwx_gtk*.a

install-gtk-py: DH_OPTIONS=-p$(package_gtk_py)
install-gtk-py: build-gtk-py-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	cd wxPython \
		&& ./setup.py install --prefix=`pwd`/../debian/$(package_gtk_py)/usr
	find debian/$(package_gtk_py)/usr/lib/$(python_dir)/site-packages/wxPython \
		-name '*.py?' -exec rm '{}' ';'

install-headers: DH_OPTIONS=-p$(package_headers)
install-headers: install-gtk-lib
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	dh_movefiles --sourcedir=debian/$(package_gtk_lib)
	dh_installmanpages \
		ansi2knr.1 \
		jpegtran.1 \
		libpng.3   \
		libpngpf.3 \
		zlib.3     \
		png.5

install-i18n: DH_OPTIONS=-p$(package_i18n)
install-i18n: build-i18n-stamp install-gtk-lib
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	dh_movefiles --sourcedir=debian/$(package_gtk_lib)

install-doc: DH_OPTIONS=-p$(package_doc)
install-doc: build-doc-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

install-examples: DH_OPTIONS=-p$(package_examples)
install-examples: build-examples-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

binary-common:
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installchangelogs
	dh_installexamples
	dh_link
	dh_strip -N$(package_gtk_dbg) -N$(package_wxbase_dbg)
	dh_compress
	dh_fixperms
	dh_makeshlibs -N$(package_gtk_py) -V
	dh_installdeb
	dh_shlibdeps -ldebian/$(package_gtk_lib)/usr/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture-independent files here.
# Note that you currently can't build the indep packages without first
# building the arch specific package files needed to create them.
binary-indep: build_all install
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build just the architecture-dependent files here.
binary-arch: build_arch install_arch
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

# Build all packages target.
binary: binary-arch binary-indep


############################################################################
#
# Some rules to build a subset of the complete list of packages that can be
# built from CVS.  Beware that packages produced with these rules may *NOT*
# be compatible with packages built using the 'binary' target or even with
# Debian policy.  Do not distribute packages built with these rules, they
# are currently useful for rapid in-house testing by developers only.

# libwxgtk shared lib package
binary-gtk: control-files-stamp install-gtk-lib install-headers
	$(MAKE) -f debian/rules DH_OPTIONS="-p$(package_gtk_lib) -p$(package_headers)" binary-common

binary-gtk-dev: control-files-stamp install-gtk-dev install-headers
	$(MAKE) -f debian/rules \
	  DH_OPTIONS="-p$(package_gtk_lib) -p$(package_gtk_dev) -p$(package_headers)" \
	  binary-common

binary-gtk-dbg: control-files-stamp install-gtk-dbg install-headers
	$(MAKE) -f debian/rules DH_OPTIONS="-p$(package_gtk_dbg) -p$(package_headers)" binary-common

# libwxbase shared lib package
binary-wxbase: control-files-stamp install-wxbase-lib install-headers
	$(MAKE) -f debian/rules DH_OPTIONS="-p$(package_wxbase_lib) -p$(package_headers)" binary-common

binary-wxbase-dev: control-files-stamp install-wxbase-dev install-headers
	$(MAKE) -f debian/rules \
	  DH_OPTIONS="-p$(package_wxbase_lib) -p$(package_wxbase_dev) -p$(package_headers)" \
	  binary-common

binary-wxbase-dbg: control-files-stamp install-wxbase-dbg install-headers
	$(MAKE) -f debian/rules DH_OPTIONS="-p$(package_wxbase_dbg) -p$(package_headers)" binary-common

# docs package
binary-doc: control-files-stamp install-doc
	$(MAKE) -f debian/rules DH_OPTIONS=-p$(package_doc) binary-common


.PHONY: build build_all build_arch clean binary-indep binary-arch binary binary-common \
        binary-gtk binary-gtk-dev binary-gtk-dbg binary-wxbase binary-wxbase-dev \
	binary-wxbase-dbg binary-doc install install_arch install-wxbase-lib \
        install-wxbase-dev install-wxbase-dbg install-gtk-lib install-gtk-dev \
        install-gtk-dbg install-gtk-contrib install-gtk-contrib-dev \
        install-gtk-py install-headers install-i18n install-doc install-examples