#!/usr/bin/make -f

export DH_VERBOSE=1

# 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)
DEB_HOST_MULTIARCH  ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

GSSDIR=$(shell krb5-config --prefix)

soname := c-client
shlibname := lib$(soname).so.2007
ssldir := /etc/pki/tls
_prefix := /opt/alt/lib$(soname)
_bindir := $(_prefix)/bin
_sbindir := $(_prefix)/sbin
_libdir := $(_prefix)/lib/$(DEB_HOST_MULTIARCH)
_sysconfdir := $(_prefix)/etc
_includedir := $(_prefix)/include
buildroot := $(CURDIR)/debian/tmp

# SSL setup, probably legacy-only, but shouldn't hurt -- Rex
EXTRACFLAGS = $(shell pkg-config --cflags openssl 2>/dev/null)
# $RPM_OPT_FLAGS
EXTRACFLAGS += -fPIC $(shell dpkg-buildflags --get CFLAGS)
# jorton added these, I'll assume he knows what he's doing. :) -- Rex
EXTRACFLAGS += -fno-strict-aliasing
EXTRACFLAGS += -Wno-pointer-sign
EXTRACFLAGS += -Wno-implicit-function-declaration -Wno-incompatible-pointer-types
EXTRALDFLAGS := $(shell dpkg-buildflags --get LDFLAGS) $(shell pkg-config --libs openssl 2>/dev/null)
CFLAGS := $(deb_cflags) $(deb_cppflags)
LDFLAGS := $(deb_ldflags)

CFLAGS += -D_REENTRANT -DDISABLE_POP_PROXY
CFLAGS += -Wno-parentheses -Wno-strict-aliasing
SHLIBCFLAGS = -Wl,-z,defs

%:
	dh $@

override_dh_auto_configure:
	dh_testdir
	patch -p1 < debian/patches/imap-2007f-cclient-only.patch
	patch -p1 < debian/patches/fix-build-ip.patch
	patch -p1 < debian/patches/2014_openssl1.1.1_sni.patch

override_dh_auto_build:
	$(MAKE) lnp \
		IP=6 \
		EXTRACFLAGS="$(EXTRACFLAGS)" \
		EXTRALDFLAGS="$(EXTRALDFLAGS)" \
		EXTRAAUTHENTICATORS=gss \
		CCLIENTLIB=$(pwd)/c-client/$(shlibname) \
		SPECIALS="GSSDIR=${GSSDIR} LOCKPGM=$(_sbindir)/mlock SSLCERTS=$(ssldir)/certs SSLKEYS=$(ssldir)/private SSLDIR=/usr/lib/$(DEB_HOST_MULTIARCH) SSLINCLUDE=/usr/include/openssl" \
		SHLIBBASE=$(soname) \
		SHLIBNAME=$(shlibname)

override_dh_auto_install:
	mkdir -p $(buildroot)$(_libdir)/

	install -p -m644 ./c-client/c-client.a $(buildroot)$(_libdir)/
	ln -s c-client.a $(buildroot)$(_libdir)/libc-client.a

	install -p -m755 ./c-client/$(shlibname) $(buildroot)$(_libdir)/

	# %ghost'd c-client.cf
	touch c-client.cf
	install -p -m644 -D c-client.cf $(buildroot)$(_sysconfdir)/c-client.cf

	: Installing development components
	ln -s $(shlibname) $(buildroot)$(_libdir)/lib$(soname).so

	mkdir -p $(buildroot)$(_includedir)/c-client/
	install -m644 ./c-client/*.h $(buildroot)$(_includedir)/c-client/
	# Added linkage.c to fix (#34658) <mharris>
	install -m644 ./c-client/linkage.c $(buildroot)$(_includedir)/c-client/
	install -m644 ./src/osdep/tops-20/shortsym.h $(buildroot)$(_includedir)/c-client/

	dh_movefiles -palt-php-lib$(soname) \
		.$(_sysconfdir)/c-client.cf \
		.$(_libdir)/lib$(soname).so.*

	dh_movefiles -palt-php-lib$(soname)-devel \
		.$(_includedir)/c-client/ \
		.$(_libdir)/lib$(soname).so

	dh_movefiles -palt-php-lib$(soname)-static \
		.$(_libdir)/c-client.a \
		.$(_libdir)/libc-client.a

override_dh_auto_clean:

override_dh_builddeb:
	dh_builddeb
	if [ $(shell find $(buildroot) -type f | wc -l) -gt 0 ]; then \
		echo "Installed but unpackaged:"; \
		find $(buildroot) -type f -exec echo "{}" \; | sed -e 's#$(buildroot)##g'; \
		exit 1; \
	fi
