#!/usr/bin/make -f

RELEASE_VER = 0
RELEASE_VER_MAJOR = 0
RELEASE_VER_MINOR = 0
SUBSTVARS_LEGACY = -Vdist:Depends="perl"
SUBSTVARS_MULTI_ARCH_HINT = -Vdist:Depends="perl:any"
SUBSTVARS = $(SUBSTVARS_LEGACY)
USE_GTK = 2.0

ifeq ($(shell { dpkg-vendor --is Debian && echo yes; } || { dpkg-vendor --is Raspbian && echo yes; }),yes)
        RELEASE_VER = $(shell /usr/bin/lsb_release -r | /bin/sed -e 's/[         ]*//g' | /usr/bin/cut -d ':' -f '2' | /usr/bin/cut -d '.' -f '1')
# Let's fake testing's and unstable's "release version"...
ifeq ($(RELEASE_VER),testing)
        RELEASE_VER = 999
endif
ifeq ($(RELEASE_VER),unstable)
        RELEASE_VER = 9999
endif
ifeq ($(shell /bin/bash -c '(( $(RELEASE_VER) >= 8 )) && echo yes'),yes)
        SUBSTVARS = $(SUBSTVARS_MULTI_ARCH_HINT)
endif
else ifeq ($(shell dpkg-vendor --is Ubuntu && echo yes),yes)
        RELEASE_VER_MAJOR = $(shell /usr/bin/lsb_release -r | /bin/sed -e 's/[   ]*//g' | /usr/bin/cut -d ':' -f '2' | /usr/bin/cut -d '.' -f '1')
        RELEASE_VER_MINOR = $(shell /usr/bin/lsb_release -r | /bin/sed -e 's/[   ]*//g' | /usr/bin/cut -d ':' -f '2' | /usr/bin/cut -d '.' -f '2')
ifeq ($(shell /bin/bash -c '(( $(RELEASE_VER_MAJOR) >= 16 )) && echo yes'),yes)
        SUBSTVARS = $(SUBSTVARS_MULTI_ARCH_HINT)
endif
endif

export NXLIBDIR='/usr/lib/$(shell dpkg-architecture -qDEB_HOST_MULTIARCH)/nx'
export LIBDIR='/usr/lib/x2go'

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

%:
	PREFIX='/usr' NXLIBDIR=$(NXLIBDIR) LIBDIR=$(LIBDIR) dh ${@} --with=systemd || PREFIX='/usr' NXLIBDIR=$(NXLIBDIR) LIBDIR=$(LIBDIR) dh ${@}

override_dh_auto_build:
	PREFIX='/usr' NXLIBDIR=$(NXLIBDIR) LIBDIR=$(LIBDIR) PERL_INSTALLDIRS=vendor dh_auto_build

override_dh_auto_install:
	if [ -f ChangeLog.gitlog ]; then cp ChangeLog.gitlog ChangeLog; fi
	$(MAKE) -f Makefile build-arch PREFIX='/usr' NXLIBDIR=$(NXLIBDIR) LIBDIR=$(LIBDIR)
	PREFIX='/usr' NXLIBDIR=$(NXLIBDIR) LIBDIR=$(LIBDIR) dh_auto_install

override_dh_missing:
	dh_missing --fail-missing

override_dh_auto_clean:
	rm -f ChangeLog
	rm -Rfv x2goserver/.build_html
	rm -Rfv x2goserver-extensions/.build_html
	rm -f ChangeLog.gitlog
	rm -f MYMETA.yml
	rm -f Makefile.perl.old
	PREFIX='/usr' NXLIBDIR=$(NXLIBDIR) LIBDIR=$(LIBDIR) dh_auto_clean
	rm -f Makefile.perl

override_dh_gencontrol:
	dh_gencontrol -- $(SUBSTVARS)
