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

# Disable ceph dependent packages for architectures without ceph
# This list should match the Build-Depends on ceph libraries in debian/control
#
# Ceph requires boost-context and boost-coroutine
# These are only available on these Debian architectures:
#   i386 hurd-i386 amd64 hurd-amd64 armel armhf arm64 loong64
#   mips mipsel mips64el powerpc ppc64el riscv64 s390x
#
# Ceph requires Java
# Java is not available on these Debian architectures:
#   hppa hurd-i386 hurd-amd64
#
# Ceph only supports 64 bit architectures
ifneq ($(filter $(DEB_HOST_ARCH),amd64 arm64 loong64 mips64el ppc64el riscv64 s390x),)
CEPH = ON
else
CEPH = OFF
endif

ifneq ($(filter $(DEB_HOST_ARCH_OS),linux kfreebsd),)
FUSE = ON
else
FUSE = OFF
endif

ifneq ($(filter $(DEB_HOST_ARCH),amd64 arm64 mips64el ppc64el riscv64 s390x alpha hurd-amd64 loong64 ppc64 sparc64),)
EC = ON
else
EC = OFF
endif

ifneq ($(filter $(DEB_HOST_ARCH),armel armhf hppa loong64 m68k powerpc sh4),)
POSIX_TEST_BROKEN = 1
else
POSIX_TEST_BROKEN = 0
endif

export DEB_BUILD_MAINT_OPTIONS = hardening=+bindnow

BUILD_DOC = $(if $(filter xrootd-doc,$(shell dh_listpackages)),TRUE,FALSE)

%:
	dh $@ --with python3 --buildsystem cmake

override_dh_auto_configure:
	dh_auto_configure -- \
	    -DFORCE_ENABLED:BOOL=ON \
	    -DENABLE_TESTS:BOOL=ON \
	    -DENABLE_FUSE:BOOL=$(FUSE) \
	    -DENABLE_CEPH:BOOL=$(CEPH) \
	    -DENABLE_XRDEC:BOOL=$(EC) \
	    -DPIP_OPTIONS="--no-deps --use-pep517 --no-build-isolation --disable-pip-version-check --verbose"

override_dh_auto_build:
	dh_auto_build
	[ "$(BUILD_DOC)" = "FALSE" ] || ( \
	doxygen Doxyfile && \
	sed 's!src=".*/xrootd-logo.png"!src="xrootd-logo.png"!' \
	    -i doxydoc/html/index.html && \
	cp -p docs/images/xrootd-logo.png doxydoc/html )

override_dh_auto_test:
	[ -r tests/XRootD/common.cfg.save ] || \
	    cp -p tests/XRootD/common.cfg tests/XRootD/common.cfg.save

	# Reduce socket path lengths used during tests
	# Tests fail with sockets in the build directory if path is too long
	mktemp -d -p /var/tmp > adminpath

	sed "s!all.adminpath .*!all.adminpath $$(cat adminpath)/XRootD!" \
	    -i tests/XRootD/common.cfg

	for x in authenticated_cluster badredir cluster TPCTests xcachewithcsi ; do \
	    sed "s!all.adminpath .*!all.adminpath $$(cat adminpath)/$${x}!" \
		-i obj-$(DEB_HOST_GNU_TYPE)/tests/$${x}/common.cfg ; \
	done

	# The XRootD::cluster::test fails the http protocol tests on some hosts
	# https://github.com/xrootd/xrootd/issues/2464

	# The badredir test fails when there is no network - exclude

	# The posix test is broken for 32 bit archs with 64 bit time_t ...
	# https://github.com/xrootd/xrootd/issues/2559
	# ... and for loong64
	# https://github.com/xrootd/xrootd/issues/2560

	touch testfile
	if ( setfattr -n user.testattr -v testvalue testfile ) ; then \
	    if [ $(POSIX_TEST_BROKEN) -eq 1 ] ; then \
		POSIXTEST='XRootD::posix::test|' ; \
	    fi ; \
	    HOSTNAME=localhost \
	    dh_auto_test -- ARGS+="-E '\
$${POSIXTEST}\
XRootD::authenticated_cluster::test|\
XRootD::badredir|\
XRootD::cluster::test'" ; \
	else \
	    echo "Extended file attributes not supported by file system" ; \
	    echo "Don't run tests that require them" ; \
	    HOSTNAME=localhost \
	    dh_auto_test -- ARGS+="-E '\
XrdCl::FileCopyTest|\
XrdCl::FileSystemTest.PlugInTest|\
XrdCl::FileSystemTest.ServerQueryTest|\
XrdCl::FileSystemTest.XAttrTest|\
XrdCl::FileTest.XAttrTest|\
XrdCl::LocalFileHandlerTest.XAttrTest|\
XrdCl::ThreadingTest|\
XrdCl::WorkflowTest.CheckpointTest|\
XrdCl::WorkflowTest.XAttrWorkflowTest|\
XrdEc::XrdEcTests|\
XRootD::authenticated_cluster::test|\
XRootD::badredir|\
XRootD::cluster::test|\
XRootD::http::test|\
XRootD::posix::test|\
XRootD::tpc::test'" ; \
	fi

	rm testfile
	mv tests/XRootD/common.cfg.save tests/XRootD/common.cfg
	rm -rf $$(cat adminpath)
	rm adminpath

override_dh_auto_clean:
	rm -f testfile
	[ -r tests/XRootD/common.cfg.save ] && \
	    mv tests/XRootD/common.cfg.save tests/XRootD/common.cfg || :
	[ -r adminpath ] && rm -rf $$(cat adminpath) || :
	rm -f adminpath
	dh_auto_clean

override_dh_auto_install:
	ln -sf libxrootd-private-dev.install.ec-$(EC) \
	   debian/libxrootd-private-dev.install

	DEB_PYTHON_INSTALL_LAYOUT=deb \
	_PYTHON_HOST_PLATFORM=$(DEB_HOST_ARCH_OS)-$(DEB_HOST_GNU_CPU) \
	dh_auto_install

	rm -f debian/tmp/usr/lib/*/libXrdCephPosix.so

	rm -f debian/tmp/usr/bin/xrdshmap

	rm -f debian/tmp/usr/lib/python3/dist-packages/xrootd-*.*-info/direct_url.json

	rm -f debian/tmp/usr/lib/*/cmake/XRootD/uninstall.cmake

	[ "$(BUILD_DOC)" = "FALSE" ] || ( \
	LD_LIBRARY_PATH=$${LD_LIBRARY_PATH}:$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH) \
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages \
	PYTHONDONTWRITEBYTECODE=1 \
	make -C bindings/python/docs html && \
	mv bindings/python/docs/build/html bindings/python/docs/build/python )

	# Service unit files
	mkdir -p debian/tmp/usr/lib/systemd/system
	install -m 644 packaging/common/xrootd@.service debian/tmp/usr/lib/systemd/system
	install -m 644 packaging/common/xrootd@.socket debian/tmp/usr/lib/systemd/system
	install -m 644 packaging/common/xrdhttp@.socket debian/tmp/usr/lib/systemd/system
	install -m 644 packaging/common/cmsd@.service debian/tmp/usr/lib/systemd/system
	install -m 644 packaging/common/frm_xfrd@.service debian/tmp/usr/lib/systemd/system
	install -m 644 packaging/common/frm_purged@.service debian/tmp/usr/lib/systemd/system
	mkdir -p debian/tmp/usr/lib/tmpfiles.d
	install -m 644 packaging/rhel/xrootd.tmpfiles debian/tmp/usr/lib/tmpfiles.d/xrootd.conf
	mkdir -p debian/tmp/usr/lib/sysusers.d
	install -m 644 packaging/rhel/xrootd-sysusers.conf debian/tmp/usr/lib/sysusers.d/xrootd.conf

	# Server config
	mkdir -p debian/tmp/etc/xrootd
	install -m 644 -p packaging/common/xrootd-clustered.cfg \
	    debian/tmp/etc/xrootd/xrootd-clustered.cfg
	install -m 644 -p packaging/common/xrootd-standalone.cfg \
	    debian/tmp/etc/xrootd/xrootd-standalone.cfg
	install -m 644 -p packaging/common/xrootd-filecache-clustered.cfg \
	    debian/tmp/etc/xrootd/xrootd-filecache-clustered.cfg
	install -m 644 -p packaging/common/xrootd-filecache-standalone.cfg \
	    debian/tmp/etc/xrootd/xrootd-filecache-standalone.cfg
	sed 's!/usr/lib64/!!' packaging/common/xrootd-http.cfg > \
	    debian/tmp/etc/xrootd/xrootd-http.cfg

	# Client config
	mkdir -p debian/tmp/etc/xrootd/client.plugins.d
	install -m 644 -p packaging/common/client.conf \
	    debian/tmp/etc/xrootd/client.conf
	sed 's!/usr/lib/!!' packaging/common/client-plugin.conf.example > \
	    debian/tmp/etc/xrootd/client.plugins.d/client-plugin.conf.example
	sed -e 's!/usr/lib64/!!' -e 's!-5!!' packaging/common/recorder.conf > \
	    debian/tmp/etc/xrootd/client.plugins.d/recorder.conf
	sed 's!/usr/lib64/!!' packaging/common/http.client.conf.example > \
	    debian/tmp/etc/xrootd/client.plugins.d/xrdcl-http-plugin.conf

	chmod 644 debian/tmp/usr/share/xrootd/utils/XrdCmsNotify.pm
	sed 's!/usr/bin/env perl!/usr/bin/perl!' -i \
	    debian/tmp/usr/share/xrootd/utils/netchk \
	    debian/tmp/usr/share/xrootd/utils/XrdCmsNotify.pm \
	    debian/tmp/usr/share/xrootd/utils/XrdOlbMonPerf

	sed 's!/usr/bin/env bash!/bin/bash!' -i \
	    debian/tmp/usr/bin/xrootd-config

	mkdir -p debian/tmp/etc/xrootd/config.d

	mkdir -p debian/tmp/var/log/xrootd
	mkdir -p debian/tmp/var/spool/xrootd

	mkdir -p debian/tmp/etc/logrotate.d
	install -m 644 -p packaging/common/xrootd.logrotate \
	    debian/tmp/etc/logrotate.d/xrootd
