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

# A hack to get it compile without errors concerning MNU_NEW
CPPFLAGS:=-I$(shell pg_config --includedir-server)

# 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 -g2
CXXFLAGS = -Wall -g2

ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
	CXXFLAGS += -O0
else
	CFLAGS += -O2
	CXXFLAGS += -O2
endif
ifeq (,$(filter nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    MAKEFLAGS += -j$(NUMJOBS)
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

config.status: configure
	dh_testdir
	# Add here commands to configure the package.
	cp -f /usr/share/misc/config.sub config.sub
	cp -f /usr/share/misc/config.guess config.guess
	CPPFLAGS="$(CPPFLAGS)" 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 \
		--disable-dependency-tracking \
		--with-wx=/usr

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp

build-stamp: config.status
	dh_testdir

	# Add here commands to compile the package.
	$(MAKE)
	pod2man --center "pgAdmin III" --release "" debian/pgadmin3.pod > pgadmin3.1

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp pgadmin3.1

	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) clean
	[ ! -f Makefile ] || $(MAKE) distclean || true
	# not cleaned up because of distclean b0rkage
	rm -f Makefile config.log stamp-h1 config.h
	rm -f config.status config.cache config.guess config.sub

	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_prep
	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

	# 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-data)/usr/share/locale/$${LOC}/LC_MESSAGES ; \
	   mv $${loc} debian/$(pkg-data)/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 -XDocs.vcproj


# binary-arch/binary-indep
# in another 'make' thread.
spec-binary-indep:
	dh_testdir
	dh_testroot
	dh_installchangelogs CHANGELOG
	dh_installdocs
	dh_link
	dh_strip --dbg-package=pgadmin3-dbg
	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
	ln -s pgadmin3-data debian/pgadmin3/usr/share/doc/pgadmin3
	ln -s pgadmin3-data debian/pgadmin3-dbg/usr/share/doc/pgadmin3-dbg
	dh_installchangelogs 
	dh_installdocs
	dh_installmenu
	dh_installman
	dh_link
	dh_strip --dbg-package=pgadmin3-dbg
	dh_compress
	dh_fixperms
	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 
