#!/usr/bin/make -f
# -*- makefile -*-

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

include /usr/share/dpkg/default.mk

export GEM2DEB_TEST_RUNNER = --check-dependencies

ifneq (,$(findstring $(DEB_BUILD_ARCH), armel m68k mips mipsel powerpc powerpcspe sh4))
export DEB_LDFLAGS_MAINT_APPEND  = -latomic
endif

PHP_VERSIONS=7.1-zend 7.2-zend 7.3-zend 7.4-zend 8.0-zend

PACKAGEVERSION = $(shell debian/get_ver.sh 'libprotobuf-zendphp' '24.2')
# From now on SONAME as such is not in use, using full version instead
SONAME=$(PBLD_COMPVER)
API_VERSION=$(SONAME)-0

%:
	dh $@ --with autoreconf || dh $@

ifneq ($(DEB_BUILD_ARCH),$(DEB_HOST_ARCH))
override_dh_auto_configure:
	# build for the native (i.e. build) architecture, as we need
	# the protoc compiler for the native arch.
	dpkg-architecture -f -a$(DEB_BUILD_ARCH) -c dh_auto_configure

endif

ifeq (0,1)
CONFIGURE_TARGETS = $(addprefix configure-,$(addsuffix -stamp,$(PHP_VERSIONS)))
BUILD_TARGETS     = $(addprefix build-,$(addsuffix -stamp,$(PHP_VERSIONS)))
INSTALL_TARGETS   = $(addprefix install-,$(addsuffix -stamp,$(PHP_VERSIONS)))
endif
PREFIX		  = /opt/zend/zendphp

override_dh_auto_configure: $(CONFIGURE_TARGETS) configure-lib
override_dh_auto_build: $(BUILD_TARGETS) build-lib

configure-%-stamp: SOURCE_DIR = build-$(*)
configure-%-stamp:
	cp -a php/ext/google/protobuf $(SOURCE_DIR)
	rm -f $(SOURCE_DIR)/third_party
	ln -s ../third_party $(SOURCE_DIR)
	cd $(SOURCE_DIR) && phpize$(*)
	dh_auto_configure --sourcedirectory=$(SOURCE_DIR) -- --enable-protobuf --with-php-config=/usr/bin/php-config$*
	touch configure-$*-stamp

configure-lib:
	export LDFLAGS= ; \
	export CFLAGS= ; \
	export CPPFLAGS= ; \
	export CXXFLAGS= ; \
cmake -B build \
	-G Ninja \
	-DCMAKE_INSTALL_PREFIX=$(PREFIX) \
	-DCMAKE_PREFIX_PATH=$(PREFIX) \
	-DBUILD_SHARED_LIBS=OFF \
	-DCMAKE_CXX_STANDARD=17 \
	-DCMAKE_BUILD_TYPE=MinSizeRel \
	-DCMAKE_INSTALL_LIBDIR=lib \
	-Dprotobuf_WITH_ZLIB=ON \
	-Dprotobuf_BUILD_PROTOC_BINARIES=ON \
	-Dprotobuf_BUILD_TESTS=OFF \
	-DCMAKE_POSITION_INDEPENDENT_CODE=ON \
	.

build-%-stamp: SOURCE_DIR = build-$(*)
build-%-stamp:
	dh_auto_build --sourcedirectory=$(SOURCE_DIR)
	touch build-$*-stamp

build-lib:
	cmake --build build

override_dh_auto_clean:
	dh_auto_clean || true

override_dh_auto_test:
	:

override_dh_auto_install: $(INSTALL_TARGETS) install-lib

install-lib:
	cd build && DESTDIR=$(CURDIR)/debian/tmp ninja install
	mkdir -p $(CURDIR)/debian/tmp/usr/lib && mv $(CURDIR)/debian/tmp$(PREFIX)/lib/pkgconfig $(CURDIR)/debian/tmp/usr/lib
	dh_install

	# Convert doc dir to symlink for the -dev packages
	$(RM) -r $(CURDIR)/debian/libprotobuf-zendphp-dev/usr/share/doc/libprotobuf-zendphp-dev
	ln -s libprotobuf$(SONAME) $(CURDIR)/debian/libprotobuf-zendphp-dev/usr/share/doc/libprotobuf-zendphp-dev
	mv $(CURDIR)/debian/tmp/$(PREFIX)/bin/protoc-* $(CURDIR)/debian/tmp/$(PREFIX)/bin/protoc

	# Remove compiler headers from libprotobuf-dev
	$(RM) -r $(CURDIR)/debian/libprotobuf-zendphp-dev$(PREFIX)/include/google/protobuf/compiler

install-%-stamp: SOURCE_DIR = build-$(*)
install-%-stamp:
	/usr/bin/php-config$(*) --extension-dir|sed 's@^/@@'>$(CURDIR)/debian/php$(*)-protobuf.install
	echo "etc/php/$(*)/mods_available/protobuf.ini">$(CURDIR)/debian/php$(*)-protobuf.conffiles
	mkdir -p $(CURDIR)/debian/php$(*)-protobuf/etc/php/$(*)/mods_available/
	cp debian/php-protobuf.ini $(CURDIR)/debian/php$(*)-protobuf/etc/php/$(*)/mods_available/protobuf.ini
	dh_auto_install -p php$(*)-protobuf --sourcedirectory=$(SOURCE_DIR) -- INSTALL_ROOT=$(CURDIR)/debian/tmp
	touch install-$*-stamp


override_dh_installdocs:
	dh_installdocs
	# Remove examples
	rm -rf $(CURDIR)/debian/protobuf-compiler/usr/share/doc/protobuf-compiler/examples

override_dh_gencontrol:
	dh_gencontrol -- -Vpb:API=${API_VERSION} -v$(PACKAGEVERSION)

override_dh_dwz:
	echo GCC bug

override_dh_shlibdeps:
	dh_shlibdeps --dpkg-shlibdeps-params=--ignore-missing-info -l$(CURDIR)/debian/tmp/$(PREFIX)/lib -l$(PREFIX)/lib

#override_dh_usrlocal:
#	echo Skip processing /usr/local	

