#!/usr/bin/make -f

DEB_LDFLAGS_MAINT_APPEND=-Wl,--as-needed
DEB_BUILD_MAINT_OPTIONS=hardening=+all

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

export PKG_CONFIG_PATH=${CURDIR}/pkgconfig

# this is precisely what dh_auto_configure does
CONFIGURE_ARGS:=	--build=${DEB_BUILD_GNU_TYPE} \
			--prefix=/usr \
			--includedir=\$${prefix}/include \
			--mandir=\$${prefix}/share/man \
			--infodir=\$${prefix}/share/info \
			--sysconfdir=/etc \
			--localstatedir=/var \
			--disable-silent-rules \
			--libdir=\$${prefix}/lib/${DEB_HOST_MULTIARCH} \
			--libexecdir=\$${prefix}/lib/${DEB_HOST_MULTIARCH} \
			--disable-maintainer-mode \
			--disable-dependency-tracking

ifneq (${DEB_BUILD_GNU_TYPE},${DEB_HOST_GNU_TYPE})
CONFIGURE_ARGS+=	--host=${DEB_HOST_GNU_TYPE}
endif

ifeq (i386,${DEB_HOST_ARCH_CPU})
# The following files must be rewritten to use PIC if -DPIC is passed:
# - librfxcodec/src/x86/rfxcodec_encode_dwt_shift_x86_sse2.asm
# - librfxcodec/src/x86/rfxcodec_encode_dwt_shift_x86_sse41.asm
# - xorgxrdp/module/x86/a8r8g8b8_to_a8b8g8r8_box_x86_sse2.asm
# - xorgxrdp/module/x86/a8r8g8b8_to_nv12_box_x86_sse2.asm
# - xorgxrdp/module/x86/i420_to_rgb32_x86_sse2.asm
# - xorgxrdp/module/x86/uyvy_to_rgb32_x86_sse2.asm
# - xorgxrdp/module/x86/yuy2_to_rgb32_x86_sse2.asm
# - xorgxrdp/module/x86/yv12_to_rgb32_x86_sse2.asm
# Documentation: http://www.nasm.us/doc/nasmdoc9.html#section-9.2
# Unfortunately, this requires reserving the EBX register, which
# is used extensively by this code; to avoid crashes or security
# issues in xrdp, we disable the code on any-i386 for stretch and
# will revisit this later and with upstream; amd64 isn’t affected
# (as it uses RIP-relative addressing), x32 and other architectures
# don’t use the assembly code at all.
# When fixed, change Build-Depends’ nasm component to:
# nasm [amd64 hurd-i386 i386 kfreebsd-amd64 kfreebsd-i386],
CONFIGURE_ARGS+=	--without-simd
endif

ifeq (x32,${DEB_HOST_ARCH})
# autodetection fails on x32 (wrongly recognised as amd64)
CONFIGURE_ARGS+=	--without-simd
endif

ifeq (hurd,${DEB_HOST_ARCH_OS})
# fuse_lowlevel.h is not yet usable
CONFIGURE_ARGS+=	--disable-fuse
endif

%:
	dh $@

override_dh_auto_configure:
	./bootstrap
	cd xorgxrdp && ./bootstrap
	cd librfxcodec && ./bootstrap
	./configure \
	    --enable-ipv6 \
	    --enable-jpeg \
	    --enable-fuse \
	    --enable-rfxcodec \
	    --enable-opus \
	    --enable-painter \
	    ${CONFIGURE_ARGS}
	cd xorgxrdp && ./configure \
	    ${CONFIGURE_ARGS}
	cd librfxcodec && ./configure \
	    --disable-shared \
	    --enable-static \
	    ${CONFIGURE_ARGS}
	cd libpainter && ./configure \
	    --disable-shared \
	    --enable-static \
	    ${CONFIGURE_ARGS}
	find . -name Makefile -print0 | \
	    xargs -0 perl -pi -e 's!XRDP_PID_PATH.*?/run!$$&/xrdp!g' --

override_dh_auto_install:
	dh_auto_install -v --destdir=debian/xrdp
	# remove unused/confusing files
	rm -f debian/xrdp/etc/xrdp/xrdp.sh
	rm -f debian/xrdp/etc/xrdp/startwm.sh
	rm -f debian/xrdp/etc/xrdp/rsakeys.ini
	rm -f debian/xrdp/usr/bin/rfxcodectest
	rm -f debian/xrdp/etc/xrdp/cert.pem
	rm -f debian/xrdp/etc/xrdp/key.pem
	# Policy
	find debian/xrdp/ -name \*.la -print0 | xargs -0 rm -f --
	find debian/xrdp/usr/lib/ -name \*.a -print0 | \
	    xargs -0 strip --strip-debug -R .comment --
	# package split
	mkdir -p debian/xorgxrdp/etc debian/xorgxrdp/usr/lib
	mv debian/xrdp/etc/X11 debian/xorgxrdp/etc/
	mv debian/xrdp/usr/lib/xorg debian/xorgxrdp/usr/lib/

override_dh_installchangelogs:
	dh_installchangelogs -pxrdp -- NEWS.md
	dh_installchangelogs --remaining-packages

override_dh_installinit:
	dh_installinit --no-restart-on-upgrade

override_dh_systemd_start:
	dh_systemd_start --no-restart-on-upgrade

override_dh_shlibdeps:
	# That's a plugin, use appropriate warning level:
	dh_shlibdeps -pxorgxrdp -- --warnings=6
	# Use default levels for everything else:
	dh_shlibdeps --remaining-packages

override_dh_gencontrol:
	echo >>debian/xorgxrdp.substvars "xdriver:Depends=$$( \
	    (cd /usr/share/xserver-xorg && cat videodrvdep xinputdep) | \
	    sed 's/, /\n/g' | sort -u | \
	    perl -0e 'print join(", ", split(/\n/, <>));')"
	dh_gencontrol

override_dh_clean:
	dh_clean
	chmod +x xorgxrdp/tests/xorg-test-run.sh
