#!/usr/bin/make -f
# Sample debian/rules that uses debhelper. 
# GNU copyright 1997 by Joey Hess.
#
# This version is for a hypothetical package that builds an
# architecture-dependant package, as well as an architecture-independent
# package.

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

# When an updated module is included in the source, change the
# appropriate variable here
# For dbs:
TAR_DIR = postgresql-7.4.13
include /usr/share/dbs/dbs-build.mk
# Other version numbers in this file
LIBPGTCL_SO_MAJOR=libpgtcl.so.2
TCL_VER=8.4

# convenient way to set architecture specification strings
# the ifeq condition is here to allow them to be overridden
# from the command line
ifeq (,$(DEB_BUILD_GNU_TYPE))
  include /usr/share/dbs/dpkg-arch.mk
endif


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


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

# additional compiler flags and definitions
CFLAGS += -DCHECK_RLIMIT_NOFILE

export CFLAGS

HERE :=		$(shell pwd)

DESTDIR =	$(HERE)/debian/postgresql
PGCLIENT =	$(HERE)/debian/postgresql-client
CONTRIBDIR =	$(HERE)/debian/postgresql-contrib
PGDOC=		$(HERE)/debian/postgresql-doc
DOCTMP=		$(PGDOC)/usr/share/doc/postgresql-doc
DEVTMP=		$(HERE)/debian/postgresql-dev
ECPGDEV=	$(HERE)/debian/libecpg-dev
LIBECPG4=	$(HERE)/debian/libecpg4
LIBPGTCL=	$(HERE)/debian/libpgtcl
LIBPGTCLDEV=	$(HERE)/debian/libpgtcl-dev
LIBPQ3=	$(HERE)/debian/libpq3

DOCSRC = 	$(HERE)/$(BUILD_TREE)/doc
SRCDIR =        $(HERE)/$(BUILD_TREE)
export SRCDIR

USERPROGS=	createdb \
		createuser \
		createlang \
		dropdb \
		dropuser \
		droplang \
		clusterdb \
		vacuumdb \
		pg_dump \
		pg_dumpall \
		pg_restore \
		pgtclsh \
		pgtksh \
		psql


############################## TARGETS ################################

config: $(BUILD_TREE)/config.status
$(BUILD_TREE)/config.status: $(patched)
	@echo debian/rules target $(BUILD_TREE)/config.status
	dh_testdir

	cd $(BUILD_TREE) && \
	    echo /usr/include/ncurses /usr/include/readline | \
	    	DOCBOOKSTYLE=/usr/share/sgml/docbook/stylesheet/dsssl/modular \
		./configure --host=$(DEB_HOST_GNU_TYPE) \
		    --build=$(DEB_BUILD_GNU_TYPE) \
		    --with-template=linux \
		    --prefix=/usr \
		    --mandir=\$${prefix}/share/man \
		    --docdir=\$${prefix}/share/doc \
		    --bindir=\$${prefix}/lib/postgresql/bin \
		    --libdir=\$${prefix}/lib \
		    --includedir=\$${prefix}/include/postgresql \
		    --enable-thread-safety \
		    --enable-nls \
		    --enable-integer-datetimes \
		    --enable-debug \
		    --disable-rpath \
		    --with-tcl \
		    --with-perl \
		    --with-python \
		    --with-pam \
		    --without-krb5 \
		    --with-openssl \
		    --with-gnu-ld \
		    --with-tclconfig=/usr/lib/tcl$(TCL_VER) \
		    --with-tkconfig=/usr/lib/tk$(TCL_VER) \
		    --with-includes=/usr/include/tcl$(TCL_VER):/usr/lib/R/include \
		    --with-libs=/usr/lib/R/bin \
	    	    --with-maxbackends=64 \
	    	    --with-pgport=5432
	cp debian/Makefile.custom $(BUILD_TREE)/src

docs-build: docs-build-stamp
	@echo debian/rules target docs-build
docs-build-stamp:
	dh_testdir -i
	dh_clean -i -k
	dh_installdirs
	ln -s ../postgresql-doc/html $(PGDOC)/usr/share/doc/postgresql/html
	-mv  build-tree/$(TAR_DIR)/contrib/ipc_check/README build-tree/$(TAR_DIR)/contrib/ipc_check/README.ipc_check
	-mv  build-tree/$(TAR_DIR)/contrib/mysql/README build-tree/$(TAR_DIR)/contrib/mysql/README.mysql
	-mv  build-tree/$(TAR_DIR)/contrib/pg_upgrade/README build-tree/$(TAR_DIR)/contrib/pg_upgrade/README.pg_upgrade
	-mv  build-tree/$(TAR_DIR)/contrib/reindexdb/README build-tree/$(TAR_DIR)/contrib/reindexdb/README.reindexdb
	touch docs-build-stamp


build-arch:  $(BUILD_TREE)/config.status build-arch-stamp docs-build-stamp
	@echo debian/rules target build-arch
build-arch-stamp:
	@echo debian/rules target build-arch-stamp
	dh_testdir

	# Add here command to compile/build the package.
	cd $(BUILD_TREE) && $(MAKE) DESTDIR=$(DESTDIR)
	cd $(BUILD_TREE)/contrib && $(MAKE) DESTDIR=$(CONTRIBDIR)
	# Debian-specific stuff
	cd debian && gcc -O2 -Wall -o pg_wrapper pg_wrapper.c

	touch build-arch-stamp

build-indep: $(BUILD_TREE)/config.status  build-indep-stamp docs-build-stamp
	@echo debian/rules target build-indep
build-indep-stamp: docs-build-stamp
	@echo debian/rules target build-indep-stamp
	install -d $(PGDOC)/usr/share/doc-base
	cd $(BUILD_TREE) && find src -name 'README*' | \
	    grep -v README.doc | \
	    cpio -pdmv $(DOCTMP)
	install -d $(DOCTMP)/tutorial
	install -m 644 $(BUILD_TREE)/src/tutorial/* $(DOCTMP)/tutorial
	install -m 644 debian/pg_pam.HOWTO $(DOCTMP)
	cd $(BUILD_TREE)/src/test && \
	    find examples -print | \
	    cpio -pdmv $(DOCTMP)/libpq3/libpq
	# clear out CVS directories
	-rm -rf `find $(PGDOC) -type d -name CVS | sort -r`
	# clear out any empty directories
	find $(PGDOC) -depth -type d -exec rmdir --ignore-fail-on-non-empty {} \; 
	find $(PGDOC) -type f | xargs chmod a-x
	rm -rf $(PGDOC)/usr/share/doc/postgresql-doc/src/interfaces/jdbc
	rm -rf $(PGDOC)/usr/share/doc/postgresql-doc/src/sgml
	rm -rf $(PGDOC)/usr/share/doc/postgresql-doc/src/tutorial

	touch build-indep-stamp

build: build-arch build-indep test-stamp
	@echo debian/rules target build

test-stamp:  build-arch-stamp
	-cd $(BUILD_TREE)/src/test/regress && \
		$(MAKE) runcheck
	-cat $(BUILD_TREE)/src/test/regress/regression.diffs
	touch test-stamp

version: build-arch-stamp
	@echo debian/rules target 
	grep VERSION $(BUILD_TREE)/src/Makefile.global | cut -f2 -d= | sed -e 's!^[ 	]*\([0-9][0-9]*\.[0-9][0-9]*\).*!\1!' >PG_VERSION
	sed -e "s/%PG_VERSION%/`cat ./PG_VERSION`/" -f debian/genscript.sed debian/enable_lang.in > debian/enable_lang
	sed -e "s/%PG_VERSION%/`cat ./PG_VERSION`/" -f debian/genscript.sed debian/postgresql.config.in > debian/postgresql.config
	sed -e "s/%PG_VERSION%/`cat ./PG_VERSION`/" -f debian/genscript.sed debian/preinst.in > debian/preinst
	sed -e "s/%PG_VERSION%/`cat ./PG_VERSION`/" -f debian/genscript.sed debian/postinst.in > debian/postinst
	sed -e "s/%PG_VERSION%/`cat ./PG_VERSION`/" -f debian/genscript.sed debian/postgresql-startup.in > debian/postgresql-startup
	sed -e "s/%PG_VERSION%/`cat ./PG_VERSION`/" -f debian/genscript.sed debian/postgresql-client.preinst.in > debian/postgresql-client.preinst
	sed -e "s/%PG_VERSION%/`cat ./PG_VERSION`/" -f debian/genscript.sed debian/postgresql-dump.in > debian/postgresql-dump
	sed -e "s/%PG_VERSION%/`cat ./PG_VERSION`/" -f debian/genscript.sed debian/prerm.in > debian/prerm
	sed -e "s/%PG_VERSION%/`cat ./PG_VERSION`/" -f debian/genscript.sed debian/postgresql-client.prerm.in > debian/postgresql-client.prerm
	rm ./PG_VERSION

install-stamp: install
	touch install-stamp

install: DH_OPTIONS= 
install: build version
	@echo debian/rules target install
	dh_testdir
	dh_clean -a -k
	dh_installdirs

	# Add here commands to install the package into debian/postgresql.
	cd $(BUILD_TREE) && \
		$(MAKE) install DESTDIR=$(DESTDIR)
	cd $(BUILD_TREE)/src/include && \
		make DESTDIR=$(DEVTMP) install-all-headers
	cd $(BUILD_TREE)/contrib && \
		$(MAKE) install DESTDIR=$(CONTRIBDIR)
	for f in `grep -l '# -\*- perl -\*-'  $(CONTRIBDIR)/usr/lib/postgresql/bin/*`; \
	do \
		sed -e '1s|^# -\*- perl -\*-.*|#!/usr/bin/perl|' $$f > $$$$; \
		mv $$$$ $$f; \
		chmod a+x $$f; \
		echo $$f - added perl invocation; \
	done
	install -m 755 $(BUILD_TREE)/contrib/adddepend/adddepend $(CONTRIBDIR)/usr/lib/postgresql/bin/adddepend

	# install Debian-specific stuff
	install -m 755 debian/pg_wrapper $(DESTDIR)/usr/bin
	cd $(DESTDIR)/usr/bin && \
		for prog in $(USERPROGS); \
		do \
			echo linking pg_wrapper to $${prog}; \
			ln -s pg_wrapper $${prog}; \
		done
	for manualpage in $(DESTDIR)/usr/share/man/manl/*.l ;\
	do \
		if [ -f $${manualpage} ]; then \
			bname=`basename $${manualpage} .l` ;\
			mv $$manualpage $(DESTDIR)/usr/share/man/man7/$${bname}.7pg ;\
		fi; \
	done
	install -m 644 debian/initdb.1 $(DESTDIR)/usr/share/man/man1/initdb.1
	install -m 644 debian/postgresql.env $(DESTDIR)/etc/postgresql
	install -m 644 debian/pg_wrapper.1 $(DESTDIR)/usr/share/man/man1
	install -m 664 debian/postgresql.conf.in $(DESTDIR)/usr/share/postgresql/
	install -m 664 debian/postmaster.conf.in $(DESTDIR)/usr/share/postgresql/
	install -m 664 $(BUILD_TREE)/COPYRIGHT $(CONTRIBDIR)/usr/share/doc/postgresql-contrib/copyright
	install -m 664 $(BUILD_TREE)/COPYRIGHT debian/postgresql-client/usr/share/doc/postgresql-client/copyright
	install -m 664 $(BUILD_TREE)/COPYRIGHT $(LIBECPG4)/usr/share/doc/libecpg4/copyright
	install -m 664 debian/README.ecpg $(LIBECPG4)/usr/share/doc/libecpg4/README
	install -m 664 debian/README.postgresql-client $(PGCLIENT)/usr/share/doc/postgresql-client/README.Debian
	install -m 755 debian/enable_lang $(DESTDIR)/usr/lib/postgresql/bin
	install -m 755 debian/postgresql-startup $(DESTDIR)/usr/lib/postgresql/bin
	install -m 755 debian/startup-checks-root.sh $(DESTDIR)/usr/share/postgresql/
	install -m 775 debian/do.maintenance $(DESTDIR)/usr/lib/postgresql/bin
	install -m 744 debian/postgresql-dump $(DESTDIR)/usr/lib/postgresql/bin
	install -m 664 debian/README.postgresql.conf $(DESTDIR)/usr/share/doc/postgresql/
	install -m 664 debian/findoidjoins.1 $(CONTRIBDIR)/usr/share/man/man1
	install -m 755 $(BUILD_TREE)/contrib/reindexdb/reindexdb $(CONTRIBDIR)/usr/lib/postgresql/bin/reindexdb
	install -m 755 -d $(DESTDIR)/etc/cron.d
	install -m 644 debian/cron.d $(DESTDIR)/etc/cron.d/postgresql
	install -d $(LIBPQ3)/DEBIAN
	install -m 644 debian/libpq3.shlibs $(LIBPQ3)/DEBIAN/shlibs
	install -d $(LIBPGTCL)/DEBIAN
	install -m 644 debian/libpgtcl.shlibs $(LIBPGTCL)/DEBIAN/shlibs
	install -d $(LIBECPG4)/DEBIAN
	install -m 644 debian/libecpg4.shlibs $(LIBECPG4)/DEBIAN/shlibs
	install -m 755 $(BUILD_TREE)/src/interfaces/libpq/libpq3-config $(DEVTMP)/usr/lib/postgresql/bin
	ln -s ../lib/postgresql/bin/libpq3-config $(DEVTMP)/usr/bin/libpq3-config
	ln -s ../lib/postgresql/bin/pg_config $(DEVTMP)/usr/bin/pg_config
	install -m 644 debian/libpq3-config.1 $(DEVTMP)/usr/share/man/man1
	cd $(LIBPGTCLDEV)/usr/lib && \
		ln -s $(LIBPGTCL_SO_MAJOR) libpgtcl.so
	cd $(DEVTMP)/usr/lib && \
		ln -s libpq.so.2 libpq.so
	install -d $(ECPGDEV)/DEBIAN
	install -m 755 debian/convert.pg_hba.conf $(DESTDIR)/usr/lib/postgresql/bin
	install -m 755 debian/psql-wm $(PGCLIENT)/usr/lib/postgresql/bin
	install -m 644 debian/libpq3.README $(LIBPQ3)/usr/share/doc/libpq3/README.Debian
	install -m 644 debian/postgresql-dev.README $(DEVTMP)/usr/share/doc/postgresql-dev/README.Debian
	install -m 644 debian/README.Debian.migration $(DESTDIR)/usr/share/doc/postgresql
	install -m 644 $(BUILD_TREE)/src/backend/libpq/pg_hba.conf.sample $(DESTDIR)/etc/postgresql/pg_hba.conf
	install -m 644 $(BUILD_TREE)/src/backend/libpq/pg_ident.conf.sample $(DESTDIR)/etc/postgresql/pg_ident.conf
	install -m 644 $(BUILD_TREE)/src/pgcluster/pglb/pglb.conf.sample $(DESTDIR)/etc/postgresql/pglb.conf
	install -m 644 $(BUILD_TREE)/src/pgcluster/pgrp/pgreplicate.conf.sample $(DESTDIR)/etc/postgresql/pgreplicate.conf
	install -m 644 $(BUILD_TREE)/src/backend/utils/pgcluster/cluster.conf.sample $(DESTDIR)/etc/postgresql/cluster.conf
	install -m 644 debian/postgresql-dump.8 $(DESTDIR)/usr/share/man/man8
	install -m 644 debian/postgresql.xpm $(PGCLIENT)/usr/share/pixmaps/
	ln -s ../lib/postgresql/bin/ecpg $(ECPGDEV)/usr/bin/ecpg
	install -m 755 debian/readpgenv debian/postgresql-client/usr/lib/postgresql/bin
	cd $(DESTDIR)/usr/include && find . -print  | cpio -pdmvl $(DEVTMP)/usr/include
	rm -rf  $(DESTDIR)/usr/include
	# clean out some stuff we don't want in the distribution
	cd $(CONTRIBDIR)/usr/bin && \
		rm -f pginsert pgnulltest pgwordcount 
	# pgcluster puts the config files into /usr/etc -> ugly
	rm -rf $(DESTDIR)/usr/etc
	# this script has unsafe temp file handling and normal people do not need it anyway
	rm $(CONTRIBDIR)/usr/lib/postgresql/bin/make_oidjoins_check
	dh_installdocs -p libpgtcl $(BUILD_TREE)/src/interfaces/libpgtcl/README
	dh_installdocs -p libpq3
	dh_installdocs -p postgresql $(BUILD_TREE)/README $(BUILD_TREE)/INSTALL 
	dh_installdocs -p postgresql-pgcluster $(BUILD_TREE)/INSTALL_PGCLUSTER $(BUILD_TREE)/README_PGCLUSTER
	dh_installdocs -p postgresql-dev
	dh_installdocs -p postgresql-contrib
	install -m 755 $(BUILD_TREE)/contrib/adddepend/README.adddepend $(CONTRIBDIR)/usr/share/doc/postgresql-contrib
	install -m 664 $(BUILD_TREE)/HISTORY $(DESTDIR)/usr/share/doc/postgresql/changelog
	install -m 664 debian/logcheck.ignore $(DESTDIR)/etc/logcheck/ignore.d.paranoid/postgresql 
	install -m 664 debian/logcheck.ignore $(DESTDIR)/etc/logcheck/ignore.d.server/postgresql 
	install -m 664 debian/logcheck.ignore $(DESTDIR)/etc/logcheck/ignore.d.workstation/postgresql 
	install -m 664 debian/logcheck.violations.ignore $(DESTDIR)/etc/logcheck/violations.ignore.d/logcheck-postgresql
	ln -s $(LIBPGTCL_SO_MAJOR) $(LIBPGTCL)/usr/lib/libpgtcl.so 
	ln -s ../$(LIBPGTCL_SO_MAJOR) $(LIBPGTCL)/usr/lib/postgresql/libpgtcl.so
	dh_movefiles --sourcedir=debian/postgresql
	if [ -f $(HERE)/debian/pkgIndex.tcl ]; then \
		cp $(HERE)/debian/pkgIndex.tcl $(LIBPGTCL)/usr/lib/postgresql/; \
	else \
		pushd $(LIBPGTCL)/usr/lib/postgresql; \
		echo "pkg_mkIndex . libpgtcl.so" | tclsh; \
		cp pkgIndex.tcl $(HERE)/debian/; \
		popd; \
	fi
	mv $(DEVTMP)/usr/include/postgresql/ecpg* $(ECPGDEV)/usr/include/postgresql/
	mv $(DEVTMP)/usr/include/postgresql/libpgtcl.h $(LIBPGTCLDEV)/usr/include/postgresql/
	install -m 644 debian/postgresql-contrib.overrides debian/postgresql-contrib/usr/share/lintian/overrides/postgresql-contrib
	install -m 644 debian/postgresql.overrides debian/postgresql/usr/share/lintian/overrides/postgresql
	mv $(LIBPQ3)/usr/lib/libpq.so $(DEVTMP)/usr/lib/
	rm $(LIBPGTCL)/usr/lib/libpgtcl.so
	rm $(DESTDIR)/usr/lib/libecpg.so
	rm $(DESTDIR)/usr/lib/libpgtypes.so
	rm $(DESTDIR)/usr/lib/libecpg_compat.so
	# build man pages out of available PODs in perl scripts
	pod2man build-tree/$(TAR_DIR)/contrib/mysql/my2pg.pl > $(CONTRIBDIR)/usr/share/man/man1/my2pg.pl.1
	# clear out any empty directories
	find debian -type d -depth -exec rmdir --ignore-fail-on-non-empty {} \; || true       
	install -d -m 755 debian/postgresql/var/run/postgresql
	# files for -doc package from $(BUILD_TREE)/doc/* 
	install -d $(DOCTMP)/faq $(DOCTMP)/TODO.detail
	install -m 644 $(BUILD_TREE)/doc/FAQ* $(DOCTMP)/faq/
	install -m 644 $(BUILD_TREE)/doc/src/FAQ/* $(DOCTMP)/faq/
	install -m 644 $(BUILD_TREE)/doc/TODO.detail/* $(DOCTMP)/TODO.detail/
	install -m 644 $(BUILD_TREE)/doc/TODO $(BUILD_TREE)/doc/README* $(BUILD_TREE)/doc/bug.template $(DOCTMP)/

# Only targets below here are run as root(fakeroot) by the autobuilders; so
# nothing above here must require fakeroot
clean:
	@echo debian/rules target clean
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp build-indep-stamp install-stamp test-stamp
	rm -f docs-build-stamp
	-cd ${BUILD_TREE}/src/tutorial && make clean
	# remove files which are produced from .in templates
	cd debian && \
	    rm -f enable_lang libpq3.preinst libpq3.prerm \
		  postgresql-client.preinst postgresql-client.prerm \
		  postgresql-dump postgresql-startup postinst preinst prerm postgresql.config
	-rm debian/pg_wrapper
	-rm PG_VERSION
	-rm -rf .tmp

	# Commands to clean up after the build process.
	rm -rf $(STAMP_DIR) $(SOURCE_DIR)

	dh_clean

# Build architecture-independent files here.
# Pass -i to all debhelper commands in this target to reduce clutter.
binary-indep: install-stamp
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -i -ppostgresql-doc debian/README*
	dh_installexamples -i
	dh_installmenu -i
	dh_installinfo -i
	dh_installchangelogs  -i
	dh_link -i
	dh_compress -i -X.c -X.source -XMakefile
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: install-stamp
	rm -rf $(PGDOC)/usr/share/doc/postgresql/html
	dh_testdir -a
	dh_testroot -a
	dh_installdebconf -a
	dh_installdocs -a
	dh_installexamples -a
	dh_installmenu -a
	dh_installlogrotate -a
	dh_installpam -a
	dh_installinit -a
	dh_installcron -a
#	dh_installman -a
	dh_installinfo -a
	dh_installchangelogs  -a
	dh_strip -a
	dh_link -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
#	dh_perl -a
	LD_LIBRARY_PATH=`pwd`/src/interfaces/libpq:$$LD_LIBRARY_PATH dh_shlibdeps -a
	for f in debian/*substvars; do \
		if [ -r $$f ]; then \
			sed 's/,* *libpq3 [^,]*//' $$f >$$$$; \
			mv $$$$ $$f; \
		fi; \
	done
	sed -e 's/, tcl$(TCL_VER) [^,]*//' -e 's/, tk$(TCL_VER) [^,]*//' -e 's/libpgtcl [^,]*,//' debian/libpgtcl.substvars >$$$$; \
		mv $$$$ debian/libpgtcl.substvars
	sed 's/libpq3 [^,]*,//' debian/libpq3.substvars >$$$$; \
		mv $$$$ debian/libpq3.substvars
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch
	@echo debian/rules target binary
.PHONY: build clean binary-indep binary-arch binary install 
