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

include /usr/share/dpkg/pkg-info.mk
SOURCE_DATE := $(shell date -d@$(SOURCE_DATE_EPOCH) -u +"%Y-%m-%d")

CLIENT_SOVER := 2
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 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-5
#export CXX=g++-5
#export CPP=cpp-5
#export CXXPP=cpp-5


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)

ifneq (,$(filter parallel=%,$(subst $(COMMA), ,$(DEB_BUILD_OPTIONS))))
   NJOBS := -j $(subst parallel=,,$(filter parallel=%,$(subst $(COMMA), ,$(DEB_BUILD_OPTIONS))))
endif

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

ifeq ($(shell dpkg-architecture -qDEB_HOST_ARCH_OS), linux)
	confflags += --enable-raw-devices
else
	confflags += --disable-raw-devices
endif

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

export DEB_BUILD_MAINT_OPTIONS := hardening=+bindnow
export DEB_CXXFLAGS_MAINT_APPEND := -DUCHAR_TYPE=uint16_t -fno-lifetime-dse -fno-strict-aliasing
export DEB_CFLAGS_MAINT_APPEND := -fno-strict-aliasing

DPKG_EXPORT_BUILDFLAGS := 1
include /usr/share/dpkg/buildflags.mk

confflags += --disable-rpath --with-gpre-cobol \
	     --with-system-editline --prefix=${ULFB} --with-fbsbin=/usr/sbin \
	     --with-bin=/usr/bin \
	     --with-fblib=/usr/lib/${DEB_HOST_MULTIARCH} \
	     --with-fbconf=${ULFB} --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-fblog=/var/log/firebird --with-fbglock=${RUN} \
	     --with-fblogfilename=firebird$(FB_VER).log \
	     --with-fbplugins=${ULFB}/plugins \
	     --with-fbmsg=${ULFB}

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

# Packaging is split into several packages:
# * firebirdVV-server (incl libengineXX)
# * libfbclient2
# * libib-util
# * firebirdVV-utils
# * firebirdVV-common (common parts to client and utils)
# * firebirdVV-common-doc (big copyright and changelog one place)
# * firebirdVV-doc
# * firebirdVV-examples
# * firebird-dev

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

.PHONY: configure
configure: configure-stamp
configure-stamp: autogen-stamp
	@echo ================================
	@echo ==== Starting configuration ====
	@echo ================================
	dh_testdir

	-sh debian/clean-after-build.sh

	chmod u+x ./configure
	dpkg-buildflags --status
	./configure $(confflags) || ( id; cat config.log; exit 1 )

	touch $@

ifneq (,$(filter nodoc,$(subst $(COMMA), ,$(DEB_BUILD_OPTIONS))))
    MANUALS_BASE :=
else
MANUALS_BASE := isql-fb gbak gfix gpre gsec fbstat nbackup \
	       fbsvcmgr fbtracemgr fbguard \
	       fb_lock_print firebird fb_config
endif
MANUALS := $(addprefix debian/man/,$(addsuffix .1,$(MANUALS_BASE)))

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

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

.PHONY: build build-arch build-indep
# not really possible to build only arch-dep/arch-indep components
# examples (arch-indep) require building of the engine (arch-dep)
build build-arch build-indep: build-stamp

build-stamp: $(MANUALS) build-firebird-stamp
	touch $@

FB_IMG := debian/firebird-image

build-server-stamp: configure-stamp
	echo Starting build
	dh_testdir
	[ -d $(FIREBIRD_TMP) ] || mkdir $(FIREBIRD_TMP)
	$(MAKE) $(NJOBS) QUIET=1

	rm -rf $(FB_IMG)
	#fakeroot $(MAKE) -C gen -f Makefile.install buildRoot
	#mv ./gen/buildroot ./debian/firebird-build
	cp -a ./gen/Release/firebird $(FB_IMG)
	cp -a src/include/gen $(FB_IMG)/include-gen

	echo "Built image contents"
	cd $(FB_IMG) && ls -lR

	sed -i -e's,-I$$fb_incdir,,'  $(FB_IMG)/bin/fb_config
	sed -i -e's,-L$$fb_libdir ,,' $(FB_IMG)/bin/fb_config

	sh $(FB_IMG)/bin/fb_config --cflags
	sh $(FB_IMG)/bin/fb_config --libs

	touch $@

build-firebird-stamp: build-server-stamp

	touch $@

.PHONY: clean
clean:
	dh_testdir
	
	sh debian/clean-after-build.sh
	
	rm -f $(MANUALS)
	
	rm -f 	build-stamp build-server-stamp configure-stamp \
		autogen-stamp install-stamp build-firebird-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-image
	
	debconf-updatepo
	
	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 CLIENT_SOVER= $(CLIENT_SOVER)
	@echo UTIL_SOVER= $(UTIL_SOVER)

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

	sh debian/make_packages.sh

	touch $@

.PHONY: binary-indep
binary-indep: install-stamp
	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

ICU_VER := $(shell pkg-config icu-i18n --modversion|sed 's/[^0-9]*\([0-9]\+\).*/\1/')
ifeq ($(ICU_VER),)
    $(error Unable to determine ICU version)
endif

.PHONY: binary-arch
# Build architecture-dependent files here.
binary-arch: install-stamp
	dh_testdir -a
	dh_testroot -a
	dh_installdebconf -a
	dh_installexamples -a
	dh_installmenu -a
	dh_installinit -pfirebird$(FB_VER)-server --name=firebird$(FB_VER)
	dh_installsystemd -pfirebird$(FB_VER)-server --name=firebird$(FB_VER)
	dh_installcron -a
	dh_installman -a
	dh_installinfo -a
	dh_installlogrotate -a --name firebird$(FB_VER)
	dh_lintian -a
	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 libib-util$(UTIL_SOVER) -V'libib-util$(UTIL_SOVER) (>= $(FB_FULL_VER)~)'

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

	dh_installdeb -a
	dh_shlibdeps -a -X /plugins/
	dh_gencontrol -a -- -V"icu:Depends=libicu$(ICU_VER)"
	dh_md5sums -a
	dh_builddeb -a

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