#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

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

main_prefix:=usr
REL_DEBDIR=debian
DEBDIR=$(CURDIR)/$(REL_DEBDIR)

JAVA_PATH=/$(main_prefix)/share/java
LIB_NAME=`ls lwgeom/lib*.so.* | head -n1`

include Version.config

DESTDIR=$(DEBDIR)/tmp
REL_DESTDIR=$(REL_DEBDIR)/tmp
PATCHDIR=$(DEBDIR)/patches
PATCHFILE=$(PATCHDIR)/upstream.diff
PATCHLEVEL=`cat $(PATCHDIR)/patch.level`
PATCHREJ=$(PATCHDIR)/rejects
main_bin=$(main_prefix)/bin

pg_majdotmin:=$(shell ${PG_CONFIG_PATH}pg_config --version | \
                awk '{print $$2;}' | awk -F . '{print $$1"."$$2;}')

PG_VAR=PGSQL_SRC=$(PGSQL_SRC) make -f $(DEBDIR)/postgresql.vars.mk
# WARNING: do not use those vars before touching the files in configure:
bindir=$(shell $(PG_VAR) REQVAR=bindir)
datadir=$(shell $(PG_VAR) REQVAR=datadir)
docdir=$(shell $(PG_VAR) REQVAR=docdir)
libdir=$(shell $(PG_VAR) REQVAR=MODULE_INSTALLDIR)
pg_ver=$(shell $(PG_VAR) REQVAR=USE_VERSION)

AWK_PG_REP='{gsub("@pg_datadir@", "$(datadir)"); \
             gsub("@pg_bindir@", "$(bindir)"); \
             gsub("@pg_docdir@", "$(docdir)"); \
             gsub("@pg_libdir@", "$(libdir)"); \
             gsub("@pg_version@", "$(pg_ver)"); \
             gsub("@main_prefix@", "$(main_prefix)"); \
             gsub("@main_bin@", "$(main_bin)"); \
             gsub("@pg_majdotmin@", "$(pg_majdotmin)"); \
             print $$0;}'

CFLAGS += -Wall -g

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

# shared library versions
version=$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION).$(SO_MICRO_VERSION)
EXTRA_VER="-pg$(pg_ver)"
SONAME=$(shell objdump -p $(LIB_NAME) | grep SONAME | \
         awk '{if (match($$2,/\.so\.[0-9]+$$/)) print substr($$2,RSTART+4)}')

PKG_NAME=$(DEBDIR)/libpostgis$(SONAME)$(EXTRA_VER)

TMP_FILENAME=/tmp/$(shell basename $(FILENAME)_$$PPID)
PG_IN_FILES=$(DEBDIR)/control $(DEBDIR)/copyright \
            $(shell ls $(PKG_NAME).* $(DEBDIR)/libpostgis-doc.* $(DEBDIR)/libpostgis-java*)
APPLY_PG_NAMES=awk $(AWK_PG_REP) $(FILENAME) > $(TMP_FILENAME); mv $(TMP_FILENAME) $(FILENAME)

CLEAN_PG_FILES= \
    echo "\nCleaning for $(X_VER): "; \
    make -f $(DEBDIR)/sofiles.mk LIB_NAME=$(LIB_NAME) EXTRA_VER=-pg$(X_VER) clean
PG_VERSIONS=72 73 74 80

# actions

configure: configure-stamp
configure-stamp:
	dh_testdir
	# commands to configure the package.
	$(MAKE) configure
	$(MAKE) Makefile.config
	# patch doesn't necessarily have to succeed
ifneq (,$(wildcard $(PATCHFILE)))
	-patch -s -r $(PATCHREJ) -p$(PATCHLEVEL) -N < $(PATCHFILE)
endif
	rm -f $(PATCHREJ)
	
	touch configure-stamp

build: build-stamp
build-stamp: configure-stamp 
	dh_testdir
	# Add here commands to compile the package.
	$(MAKE)
	@echo finding xsl stylesheets...
#	XSLBASE=$(XSLBASE) $(MAKE) -C doc html
	DEBUGJAR=postgis_debug.fastjar JAR=fastjar $(MAKE) -C jdbc2 jar
	sh $(DEBDIR)/logtop.sh $(version)
	make -f $(DEBDIR)/sofiles.mk LIB_NAME=$(LIB_NAME) EXTRA_VER=$(EXTRA_VER) build
	touch build-stamp

clean: configure-stamp
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	dh_clean
	# Add here commands to clean up after the build process.
	$(foreach X_VER,$(PG_VERSIONS),$(CLEAN_PG_FILES);)
	sh $(DEBDIR)/logtop.sh $(version)
	$(MAKE) maintainer-clean
	sh $(DEBDIR)/mkupstreampatch.sh $(version)

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	$(foreach FILENAME,$(PG_IN_FILES),$(APPLY_PG_NAMES);)
	dh_installdirs -A
	
	# commands to install the package into debian/tmp
	$(MAKE) install DESTDIR=$(DESTDIR)
	DEBUGJAR=postgis_debug.fastjar JAR=fastjar $(MAKE) -C jdbc2 install DESTDIR=$(DESTDIR)$(JAVA_PATH)

	mkdir -p $(DESTDIR)/$(bindir)
#	cp utils/*.pl $(DESTDIR)/$(bindir)
	mkdir -p $(DESTDIR)/$(main_bin)
#	mv $(DESTDIR)/$(bindir)/shp2pgsql $(DESTDIR)/$(main_bin)
#	mv $(DESTDIR)/$(bindir)/pgsql2shp $(DESTDIR)/$(main_bin)
	cat $(DEBDIR)/mktemplate_gis.in | awk $(AWK_PG_REP) > $(DESTDIR)/$(main_bin)/mktemplate_gis
	chmod 755 $(DESTDIR)/$(main_bin)/mktemplate_gis
	cp $(DEBDIR)/rmtemplate_gis $(DESTDIR)/$(main_bin)
	chmod 755 $(DESTDIR)/$(main_bin)/rmtemplate_gis
	cp $(DEBDIR)/createdb.postgis $(DESTDIR)/$(main_bin)
	chmod 755 $(DESTDIR)/$(main_bin)/createdb.postgis

	dh_installchangelogs CHANGES
	dh_installman
	dh_compress
	dh_installdocs
	dh_installexamples regress
	dh_install --sourcedir=$(DESTDIR)
	dh_movefiles --sourcedir=$(REL_DESTDIR) #this style is required for woody
	dh_link
	dh_strip
	find $(REL_DEBDIR) -name .cvsignore -exec rm -f '{}' ';'
	dh_fixperms
	dh_makeshlibs --noscripts


# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installdeb -i
	dh_shlibdeps -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i


# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdeb -s
	dh_shlibdeps -s
	dh_gencontrol -s
	dh_md5sums -s
	dh_builddeb -s


binary: binary-indep binary-arch

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