#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# <rip>
# #       This piece of code ripped from doc-linux-fr debian package
# #       We use this to generate our hacked .orig.tar.gz by invoking
# #       manually the "orig" target
TGZ = tar -z
TBZ2 = tar -j
source = $(shell dpkg-parsechangelog | grep '^Source:' | sed 's/^Source: //')
src_version = $(shell dpkg-parsechangelog | grep '^Version:' | \
	sed 's/^Version: *//')
upstream = $(shell dpkg-parsechangelog | grep '^Version:' | \
	sed -e 's/^Version: //' -e 's/-.*//')
# </rip>


pkg=pgadmin3
pkg-data=$(pkg)-data

#
#
orig: clean
	$(TGZ) -C .. --exclude $(source)-$(upstream)/debian \
		--exclude $(source)-$(upstream)/pkg/debian  -cvf \
		../$(source)_$(upstream).orig.tar.gz $(source)-$(upstream)

#
doPgA3Wx=localwx
_PGA3_WXVERSION = 2.7.0-1
_PGA3_WXSNAPBASE = wxWidgets
_PGA3_WXTAR = $(shell pwd)/wxWidgets-$(_PGA3_WXVERSION).tar.gz
_PGA3_WXDEST = $(shell pwd)/$(_PGA3_WXSNAPBASE)-$(_PGA3_WXVERSION)
_PGA3_WXTOP = $(_PGA3_WXDEST)/$(_PGA3_WXSNAPBASE)-$(_PGA3_WXVERSION)
_PGA3_WXLOCALINST = $(_PGA3_WXTOP)/localinst
_PGA3_WXCONFIG ?= wx-config
#


# The name of the wxWidgets config script we invoke later in configure
_PGA3_WXCONFIG ?= wx-config

# A hack to get it compile without errors concerning MNU_NEW
_pgsql_inc:="$(shell pg_config --includedir) -I./include"

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_BUILD_ARCH   ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

CFLAGS = -Wall -g
CXXFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
	CXXFLAGS += -O0
else
	CFLAGS += -O2
	CXXFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

# Backed out MIPS specific part of the patch from Thiemo Seufer (see #274090)
#ifneq (,$(filter mips mipsel,$(DEB_BUILD_ARCH)))
	#CFLAGS += -Wa,-xgot
	#CXXFLAGS += -Wa,-xgot
#endif




# Build a new .orig.tar.gz.
orig: clean

	$(TGZ) -C .. --exclude $(source)-$(upstream)/debian \
		--exclude $(source)-$(upstream)/pkg/debian  -cvf \
		../$(source)_$(upstream).orig.tar.gz $(source)-$(upstream)

# Here goes the wxWidgets untar/configure/build
$(doPgA3Wx)-clean:
	[ -d $(_PGA3_WXDEST) ] && rm -rf $(_PGA3_WXDEST) || true
	-rm $(doPgA3Wx)-stamp
	-rm $(doPgA3Wx)-untar-stamp
	-rm $(doPgA3Wx)-configure-stamp
	-rm $(doPgA3Wx)-build-stamp
	-rm $(doPgA3Wx)-install-stamp
                                                                                                                             
$(doPgA3Wx): $(doPgA3Wx)-stamp

$(doPgA3Wx)-stamp: $(doPgA3Wx)-untar $(doPgA3Wx)-configure $(doPgA3Wx)-build $(doPgA3Wx)-install
	touch $@

$(doPgA3Wx)-untar: $(doPgA3Wx)-untar-stamp

$(doPgA3Wx)-untar-stamp:
	mkdir -p $(_PGA3_WXDEST)
	cd $(_PGA3_WXDEST) && $(TGZ) -xf $(_PGA3_WXTAR)
	# Here we eventually apply wxWindows patches
	cat debian/wxWidgets.patch | ( cd $(_PGA3_WXTOP) && patch -p0 )
	touch $@

$(doPgA3Wx)-configure: $(doPgA3Wx)-configure-stamp

$(doPgA3Wx)-configure-stamp:
	cd $(_PGA3_WXTOP) && \
	./configure --build=$(DEB_BUILD_GNU_TYPE) \
	--prefix=$(_PGA3_WXLOCALINST) \
	--mandir=\$${prefix}/share/man \
	--infodir=\$${prefix}/share/info \
	--with-gtk --enable-gtk2 --enable-unicode \
	--enable-debug \
	--disable-shared
	touch $@

$(doPgA3Wx)-build: $(doPgA3Wx)-build-stamp

$(doPgA3Wx)-build-stamp:
	cd $(_PGA3_WXTOP) && $(MAKE)
	cd $(_PGA3_WXTOP)/contrib/ && $(MAKE)
	touch $@

$(doPgA3Wx)-install: $(doPgA3Wx)-install-stamp

$(doPgA3Wx)-install-stamp:
	cd $(_PGA3_WXTOP) && $(MAKE) install
	cd $(_PGA3_WXTOP)/contrib/ && $(MAKE) install
	touch $@


# Here goes the pgAdmin3 stuff

config.status: configure $(doPgA3Wx)
	dh_testdir
	# Add here commands to configure the package.
	CFLAGS="$(CFLAGS)" ./configure --host=$(DEB_HOST_GNU_TYPE) \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=/usr \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		--disable-dependency-tracking \
		--with-wx=$(_PGA3_WXLOCALINST) \
		--with-wx-config=$(_PGA3_WXCONFIG) \
		--with-pgsql-include=$(_pgsql_inc) \
		--enable-gtk2 \
		--enable-unicode \
		--enable-debug --enable-static --with-libxml2=/usr --with-libxslt=/usr

build: build-stamp

build-stamp: config.status
	dh_testdir

	# Add here commands to compile the package.
	$(MAKE)
	#/usr/bin/docbook-to-man debian/pgadmin3.sgml > pgadmin3.1

	touch build-stamp

clean: $(doPgA3Wx)-clean
	dh_testdir
	dh_testroot
	rm -f build-stamp 

	# Add here commands to clean up after the build process.
	-$(MAKE) clean
	-$(MAKE) distclean
	-rm config.status
	-rm config.cache
ifneq "$(wildcard /usr/share/misc/config.sub)" ""
	cp -f /usr/share/misc/config.sub config.sub
endif
ifneq "$(wildcard /usr/share/misc/config.guess)" ""
	cp -f /usr/share/misc/config.guess config.guess
endif


	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs

	# Add here commands to install the package into debian/pgadmin3.
	# Finally install all in debian/tmp. Files are dispatched into packages by dh_install
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp
	[ ! -f TODO.txt ] && echo "Please consult: http://www.pgadmin.org/development/todo.php" > TODO.txt || true

	# move pgadmin3.mo files to locales directory
	# this part was written by Andreas Tille <tille@debian.org>
	for loc in `find debian/tmp -name $(pkg).mo` ; do \
	   LOC=`echo $${loc} | sed "s?.*pgadmin3/i18n/\(.*\)/$(pkg).mo?\1?"` ; \
	   mkdir -p debian/$(pkg)/usr/share/locale/$${LOC}/LC_MESSAGES ; \
	   mv $${loc} debian/$(pkg)/usr/share/locale/$${LOC}/LC_MESSAGES/. ; \
	done
	
	# moved dh_install here as it is common to indep and arch
	# by being there dh_install can warn about forgotten files as it is
	# acting on both pgadmin3 and pgadmin3-data packages. (RE)
	# (note that we could also use --fail-missing instead of --list-mising)
	dh_install --list-missing


# binary-arch/binary-indep
# in another 'make' thread.
spec-binary-indep:
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	# dh_installexamples
	#dh_install
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Must not depend on anything. This is to be called by
# binary-arch/binary-indep
# in another 'make' thread.
spec-binary-arch:
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	dh_installexamples
	#dh_install --list-missing
	dh_installmenu
#	dh_installdebconf	
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
#	dh_installcron
#	dh_installinfo
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_perl
#	dh_python
#	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

	#make -f debian/rules $(doPgA3Wx)-clean

# Build architecture independant packages using the common target.
binary-indep: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-i spec-binary-indep

# Build architecture dependant packages using the common target.
binary-arch: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-a spec-binary-arch

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