#!/usr/bin/make -f

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

FB_MAJOR:= $(shell awk '/FB_MAJOR_VER/ { v=$$3; gsub("\"", "", v); print v}' < src/jrd/build_no.h )
FB_MINOR:= $(shell awk '/FB_MINOR_VER/ { v=$$3; gsub("\"", "", v); print v}' < src/jrd/build_no.h )
FB_REV	:= $(shell awk '/FB_REV_NO/    { v=$$3; gsub("\"", "", v); print v}' < src/jrd/build_no.h )
FB_VER	:= $(FB_MAJOR).$(FB_MINOR)
FB_FULL_VER:= $(FB_VER).$(FB_REV)

EMBED_SOVER=$(FB_VER)
CLIENT_SOVER=$(FB_MAJOR)
UTIL_SOVER=

ifndef FB_MAJOR
$(error FB_MAJOR not defined)
else ifndef FB_MINOR
$(error FB_MINOR not defined)
else ifndef FB_REV
$(error FB_REV not defined)
else ifndef FB_VER
$(error FB_VER not defined)
else ifndef FB_FULL_VER
$(error FB_FULL_VER not defined)
else ifndef EMBED_SOVER
$(error EMBED_SOVER not defined)
else ifndef CLIENT_SOVER
$(error CLIENT_SOVER not defined)
endif

# Use the following lines to set the compiler if
# the default Debian one can't be used (or you want to try
# the bleeding edge)
#export CC=gcc-4.3
#export CXX=g++-4.3
#export CPP=cpp-4.3
#export CXXPP=cpp-4.3


export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
export DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	confflags += --build $(DEB_HOST_GNU_TYPE)
else
	confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

DOC=/usr/share/doc/firebird${FB_VER}-common-doc
ULFB=/usr/lib/firebird/${FB_VER}
USFB=/usr/share/firebird/${FB_VER}
VAR=/var/lib/firebird/${FB_VER}
RUN=/var/run/firebird/${FB_VER}

confflags += --libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH) \
	     --disable-static --with-gpre-cobol --with-system-icu \
	     --with-system-editline --prefix=/usr --with-fbsbin=/usr/sbin \
	     --with-fbconf=/etc/firebird/${FB_VER} --with-fbdoc=${DOC} \
	     --with-fbudf=${ULFB}/UDF --with-fbsample=${DOC}/examples \
	     --with-fbsample-db=${DOC}/examples/empbuild \
	     --with-fbhelp=${VAR}/system --with-fbintl=${ULFB}/intl \
	     --with-fbmisc=${ULFB}/misc --with-fbsecure-db=${VAR}/system \
	     --with-fbmsg=${USFB} --with-fblog=/var/log --with-fbglock=${RUN} \
	     --with-fbplugins=${ULFB}/plugins \
	     $(shell dpkg-buildflags --export=configure)

# see #575564
export FIREBIRD_LOCK=$(CURDIR)/debian/tmp-lock

# We're building 2 flavours resulting in 9 binary packages:
# * Super server
#   -super
#   libfbclient2
# * Classic server
#   -classic
#   libfbembed2
# * Common
#   -common
#   -server-common
#   -dev
#   -examples
#   -doc

autogen: autogen-stamp
autogen-stamp:
	NOCONFIGURE=1 sh autogen.sh
	touch $@

.PHONY: configure-super
configure-super: configure-super-stamp
configure-super-stamp: autogen-stamp
	@echo =====================================================
	@echo ==== Starting configure architecture SuperServer ====
	@echo =====================================================
	dh_testdir

	-sh debian/clean-after-build.sh

	chmod u+x ./configure
	CFLAGS= CXXFLAGS= ./configure $(confflags) --enable-superserver

	touch $@

.PHONY: configure-classic
configure-classic: configure-classic-stamp
configure-classic-stamp: autogen-stamp build-super-stamp
	@echo =======================================================
	@echo ==== Starting configure architecture ClassicServer ====
	@echo =======================================================
	dh_testdir

	-sh debian/clean-after-build.sh

	chmod u+x ./configure
	CFLAGS= CXXFLAGS= ./configure $(confflags)

	touch $@

.PHONY: build
build: build-arch build-indep

build-stamp: build-super-and-classic-stamp
	sed 's/\$$(DEB_HOST_MULTIARCH)/$(DEB_HOST_MULTIARCH)/g' debian/libib-util.lintian-overrides.in > debian/libib-util.lintian-overrides
	touch $@

.PHONY: build-indep
build-indep: build-manuals

.PHONY: build-arch
build-arch: build-stamp

build-super-and-classic-stamp:
	$(MAKE) -f debian/rules build-super
	$(MAKE) -f debian/rules build-classic
	touch $@

MANUALS_BASE = isql-fb gbak gdef gfix gpre gsec fbstat qli nbackup \
	       fbsvcmgr fbtracemgr fb_smp_server fb_inet_server fbguard \
	       fbserver fb_lock_print
MANUALS = $(addprefix debian/,$(addsuffix .1,$(MANUALS_BASE)))

.PHONY: build-manuals
build-manuals: $(MANUALS)

%.1 : %.pod
	pod2man -c "Firebird documentation" --release "Firebird 2.5"  $< > $@

%.1 : %.sgml
	docbook-to-man $< > $@

.PHONY: build-super
build-super: build-super-stamp
build-super-stamp: configure-super-stamp
	echo Starting build-super-server
	dh_testdir
	[ -d $(FIREBIRD_TMP) ] || mkdir $(FIREBIRD_TMP)
	$(shell dpkg-buildflags --export=sh); $(MAKE)

	# moving to SuperServer target
	rm -rf ./debian/firebird-super
	fakeroot $(MAKE) -C gen -f Makefile.install buildRoot
	mv ./gen/buildroot ./debian/firebird-super

	touch $@

.PHONY: build-classic
build-classic: build-classic-stamp
build-classic-stamp: configure-classic-stamp
	echo Starting build-classic-server
	dh_testdir
	[ -d $(FIREBIRD_TMP) ] || mkdir $(FIREBIRD_TMP)
	$(shell dpkg-buildflags --export=sh); $(MAKE)

	# moving to ClassicServer target
	rm -rf ./debian/firebird-classic
	fakeroot $(MAKE) -C gen -f Makefile.install buildRoot
	mv ./gen/buildroot ./debian/firebird-classic

	touch $@

.PHONY: clean
clean:
	dh_testdir
	dh_testroot
	
	sh debian/clean-after-build.sh
	
	rm -f $(MANUALS)
	
	rm -f 	build-stamp build-classic-stamp configure-classic-stamp \
		build-super-stamp configure-super-stamp autogen-stamp \
		buils-super-and-classic-stamp
	
	# re-generated in autogen target
	rm -f builds/make.new/config/config		\
	      builds/make.new/config/config.guess	\
	      builds/make.new/config/config.h.in	\
	      builds/make.new/config/config.sub 	\
	      builds/make.new/config/ltmain.sh		\
	      builds/make.new/config/install-sh		\
	      aclocal.m4				\
	      configure
	
	rm -rf	debian/firebird-super debian/firebird-super-debug \
		debian/firebird-classic debian/firebird-classic-debug
	
	debconf-updatepo
	
	# We don't need these in the .diff, they are copies
	rm -f debian/firebird$(FB_VER)-super.templates \
	      debian/firebird$(FB_VER)-classic.templates \
	      debian/firebird$(FB_VER)-superclassic.templates \
	      debian/firebird$(FB_VER)-super.config \
	      debian/firebird$(FB_VER)-classic.config \
	      debian/firebird$(FB_VER)-superclassic.config
	
	dh_clean
	
	@echo FB_MAJOR = $(FB_MAJOR)
	@echo FB_MINOR = $(FB_MINOR)
	@echo FB_REV   = $(FB_REV)
	@echo FB_VER   = $(FB_VER)
	@echo FB_FULL_VER = $(FB_FULL_VER)
	@echo EMBED_SOVER = $(EMBED_SOVER)
	@echo CLIENT_SOVER= $(CLIENT_SOVER)
	@echo UTIL_SOVER= $(UTIL_SOVER)

.PHONY: install
install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	sh debian/make_packages.sh

	cp -a debian/server-templates.master \
	      debian/firebird$(FB_VER)-super.templates

	cp -a debian/server-templates.master \
	      debian/firebird$(FB_VER)-classic.templates

	cp -a debian/server-templates.master \
	      debian/firebird$(FB_VER)-superclassic.templates

	cp -a debian/server-config.master \
	      debian/firebird$(FB_VER)-super.config

	cp -a debian/server-config.master \
	      debian/firebird$(FB_VER)-classic.config

	cp -a debian/server-config.master \
	      debian/firebird$(FB_VER)-superclassic.config

.PHONY: binary-indep
binary-indep: install
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -p firebird$(FB_VER)-common-doc
	dh_installdocs -Xchangelog -Xcopyright -p firebird$(FB_VER)-doc
	dh_installman -i
	dh_installexamples -i
	dh_installlogrotate -i --name firebird$(FB_VER)
	dh_installchangelogs ChangeLog -p firebird$(FB_VER)-common-doc
	dh_lintian -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i

	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i -- -Zxz


.PHONY: binary-arch
# Build architecture-dependent files here.
binary-arch: install
	dh_testdir -a
	dh_testroot -a
	dh_installdebconf -a
	dh_installexamples -a
	dh_installmenu -a
	dh_installinit -pfirebird$(FB_VER)-super
	# classic's doesn't need stop rc.d links
	dh_installinit -pfirebird$(FB_VER)-classic -u'start 20 2 3 4 5 .'
	dh_installinit -pfirebird$(FB_VER)-superclassic
	dh_installcron -a
	dh_installman -a
	dh_installinfo -a
	dh_installlogrotate -a --name firebird$(FB_VER)
	dh_lintian -a
	dh_strip -pfirebird$(FB_VER)-super --dbg-package firebird2.5-super-dbg
	dh_strip -plibfbclient$(CLIENT_SOVER) --dbg-package libfbclient$(CLIENT_SOVER)-dbg
	dh_strip -pfirebird$(FB_VER)-classic --dbg-package firebird2.5-classic-dbg
	dh_strip -pfirebird$(FB_VER)-superclassic --dbg-package firebird2.5-classic-dbg
	dh_strip -pfirebird$(FB_VER)-classic-common --dbg-package firebird2.5-classic-dbg
	dh_strip -pfirebird$(FB_VER)-common --dbg-package libfbclient$(CLIENT_SOVER)-dbg
	dh_strip -plibfbembed$(EMBED_SOVER) --dbg-package firebird2.5-classic-dbg
	dh_strip -a
	dh_link -a -A
	dh_compress -a
	dh_fixperms -a

	dh_makeshlibs -p libfbclient$(CLIENT_SOVER) -V'libfbclient$(CLIENT_SOVER) (>= $(FB_FULL_VER)~)'
	dh_makeshlibs -p libfbembed$(EMBED_SOVER) -V'libfbembed$(EMBED_SOVER) (>= $(FB_FULL_VER)~)'
	dh_makeshlibs -p libib-util$(UTIL_SOVER) -V'libib-util$(UTIL_SOVER) (>= $(FB_FULL_VER)~)'

	dpkg-gensymbols -plibfbclient$(CLIENT_SOVER) -Pdebian/libfbclient$(CLIENT_SOVER)
	dpkg-gensymbols -plibfbembed$(EMBED_SOVER) -Pdebian/libfbembed$(EMBED_SOVER)
	dpkg-gensymbols -plibib-util$(UTIL_SOVER) -Pdebian/libib-util$(UTIL_SOVER)

	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a -- -Zxz

.PHONY: binary
binary: binary-indep binary-arch

.PHONY: get-orig-source
get-orig-source:
	uscan
