#!/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

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

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

# A hack to get it compile without errors concerning MNU_NEW
_pgsql_inc:="/usr/include/postgresql -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

# Here goes the pgAdmin3 stuff
configure:
	sh bootstrap

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

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:
	dh_testdir
	dh_testroot
	rm -f build-stamp 

	# Add here commands to clean up after the build process.
	-$(MAKE) clean
	-$(MAKE) distclean
	-rm configure
	-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/pgadmin3/development.php#todo" > 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/ui/\(.*\)/$(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


# 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 
