#! /usr/bin/make -f

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

# This has to be exported to make some magic below work.
export DH_OPTIONS

# These are locations of quilt stuff
export QUILT_PATCHES=$(shell pwd)/debian/patches

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_BUILD_ARCH ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)
DEB_BUILD_ARCH_OS ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH_OS)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# set the number of build jobs
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    JOBS:=-j$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
CROSS= --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE) --with-random=/dev/urandom
else
CROSS= --build $(DEB_BUILD_GNU_TYPE)
endif

ifneq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
    DO_TEST=no
else
    DO_TEST=no
    ifeq (${DEB_BUILD_GNU_TYPE},${DEB_HOST_GNU_TYPE})
    ifneq (${DEB_BUILD_ARCH},m68k)
    ifneq (${DEB_BUILD_ARCH_OS},kfreebsd)
    ifneq (${DEB_BUILD_ARCH_OS},knetbsd)
    ifneq (${DEB_BUILD_ARCH_OS},hurd)
        DO_TEST=yes
    endif
    endif
    endif
    endif
    endif
endif

# this will catch miss-linking. (e.g. undefined symbols)
# LDFLAGS = -Wl,-z,defs

CONFIGURE_ARGS = $(CROSS) --prefix=/usr --mandir=/usr/share/man	\
		--disable-dependency-tracking --enable-ipv6	\
		--with-lber-lib=lber --enable-manual		\
		--enable-versioned-symbols --enable-debug	\
		--libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH)	\
		$(shell dpkg-buildflags --export=configure)
# this is still not ready
# --enable-ares
# this fixes the ldap warning of dpkg-shlibdebs, but we dont want that.
# --with-ldap-lib=/usr/lib/libldap_r.so

ifneq (${DEB_BUILD_ARCH_OS},hurd)
CONFIGURE_ARGS += --with-gssapi=/usr
endif

build-arch: build-stamp
build-indep: build-stamp

build: build-arch build-indep
#configure-stamp: DH_OPTIONS=
configure-stamp:
	dh_testdir
	mkdir -p debian/build debian/build-gnutls debian/build-nss
	-quilt push -a
	# pop the last patch (nss)
	quilt pop
	# pop the second last patch (gnutls)
	quilt pop
	# get the source without nss and gnutls patches
	tar -cf - --exclude=debian --exclude=.pc . | tar -xf - -C debian/build
	# push the second last patch which must be gnutls
	quilt push
	# get the source with gnutls patch applied
	tar -cf - --exclude=debian --exclude=.pc . | tar -xf - -C debian/build-gnutls
	# push the last patch which must be nss
	quilt push
	# get the source with nss patch applied
	tar -cf - --exclude=debian --exclude=.pc . | tar -xf - -C debian/build-nss
	for flavour in build build-gnutls build-nss; do \
		(cd debian/$$flavour && ./buildconf) \
	done
	cd debian/build && ./configure ${CONFIGURE_ARGS}		\
		--with-librtmp --with-ca-path=/etc/ssl/certs
	cd debian/build-gnutls && ./configure ${CONFIGURE_ARGS}		\
		--with-ca-bundle=/etc/ssl/certs/ca-certificates.crt	\
		--without-ssl --with-gnutls
	cd debian/build-nss && ./configure ${CONFIGURE_ARGS}		\
		--with-ca-bundle=/etc/ssl/certs/ca-certificates.crt	\
		--without-ssl --with-nss
	touch configure-stamp

#build-stamp: DH_OPTIONS=
build-stamp: configure-stamp
	dh_testdir
	${MAKE} $(JOBS) -C debian/build
	${MAKE} $(JOBS) -C debian/build-gnutls
	${MAKE} $(JOBS) -C debian/build-nss
ifeq (${DO_TEST},yes)
	-${MAKE} -C debian/build test
	-${MAKE} -C debian/build-gnutls test
	-${MAKE} -C debian/build-nss test
endif
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	-quilt pop -a
	rm -rf build-stamp configure-stamp debian/build debian/build-gnutls debian/build-nss debian/tmp-gnutls debian/tmp-nss
	dh_clean debian/shlibs.local debian/libcurl3.links \
		debian/libcurl3-gnutls.links debian/libcurl3-nss.links \
		debian/libcurl4-gnutls-dev.links debian/libcurl4-nss-dev.links

#install: DH_OPTIONS=
install: build-stamp
	dh_testdir
	dh_testroot
	dh_prep
	for file in libcurl3.links libcurl3-gnutls.links libcurl3-nss.links \
	            libcurl4-gnutls-dev.links libcurl4-nss-dev.links; \
	do \
		sed -e"s,\$${DEB_HOST_MULTIARCH},$(DEB_HOST_MULTIARCH),g" \
			debian/$${file}.in > debian/$$file; \
	done
	${MAKE} -C debian/build DESTDIR=$(shell pwd)/debian/tmp install
	${MAKE} -C debian/build-gnutls DESTDIR=$(shell pwd)/debian/tmp-gnutls install
	${MAKE} -C debian/build-nss DESTDIR=$(shell pwd)/debian/tmp-nss install
	dh_install -plibcurl3-gnutls -plibcurl4-gnutls-dev --sourcedir=debian/tmp-gnutls
	dh_install -plibcurl3-nss -plibcurl4-nss-dev --sourcedir=debian/tmp-nss
	dh_install -Nlibcurl3-gnutls -Nlibcurl4-gnutls-dev -Nlibcurl3-nss -Nlibcurl4-nss-dev --sourcedir=debian/tmp
	dh_lintian
	for i in `find debian/lib* -name "lib*.la"`; do echo "removing dependency_libs from $$i"; sed -i -e "/dependency_libs/ s/'.*'/''/" $$i; done

# Build architecture-independent files here.
#binary-indep: DH_OPTIONS=-i
binary-indep: build-stamp install
# We have nothing to do here.

# Build architecture-dependent files here.
#binary-arch: DH_OPTIONS=-a
binary-arch: build-stamp install
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installman
	dh_installexamples
	dh_installchangelogs CHANGES
	dh_link
	dh_strip -Nlibcurl3 -Nlibcurl3-gnutls -Nlibcurl3-nss
	dh_strip -plibcurl3 -plibcurl3-gnutls -plibcurl3-nss --dbg-package=libcurl3-dbg
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_md5sums
	dh_gencontrol
	dh_builddeb

binary: binary-indep binary-arch

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