#!/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)

# Setting these disables autodetection.
FORCE_VENDOR ?=
FORCE_RELEASE_VER ?=

ifneq ($(FORCE_VENDOR),)
  ifneq ($(FORCE_RELEASE_VER),)
    VENDOR_DEBIAN := $(shell if [ 'debian' = '$(FORCE_VENDOR)' ] || [ '$(FORCE_VENDOR)' = 'raspbian' ]; then echo 'yes'; fi)
    VENDOR_UBUNTU := $(shell if [ 'ubuntu' = '$(FORCE_VENDOR)' ]; then echo 'yes'; fi)
    RELEASE_VER := $(FORCE_RELEASE_VER)

    ifeq ($(VENDOR_UBUNTU),yes)
      RELEASE_VER_MAJOR := $(shell printf '%s\n' '$(RELEASE_VER)' | /usr/bin/cut -d '.' -f '1')
      RELEASE_VER_MINOR := $(shell printf '%s\n' '$(RELEASE_VER)' | /usr/bin/cut -d '.' -f '2')
    endif
  else
    $(error FORCE_VENDOR passed, but FORCE_RELEASEVER empty.)
  endif
else
  VENDOR_DEBIAN := $(shell { dpkg-vendor --is 'Debian' && echo 'yes'; } || { dpkg-vendor --is 'Raspbian' && echo 'yes'; })

  ifeq ($(VENDOR_DEBIAN),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')

    # Newer Debian versions might report "n/a" for testing and unstable.
    ifeq ($(RELEASE_VER),n/a)
      # On these platforms, the best way to determine the system version is by
      # going through "apt-cache policy".
      # Note that this should only be the case for either testing or unstable.
      # Other systems should have a proper version number.
      # This is also why we can just drop any suites/archive names (this is
      # what a= means) containing a dash character (e.g., "stable-updates")
      # and only pick the first match.
      RELEASE_VER := $(shell /usr/bin/apt-cache policy | grep -E 'o=(De|Rasp)bian,' | grep -E 'l=(De|Rasp)bian,' | grep -F 'c=main,' | grep -Eo 'a=[^, ]*' | sed -e 's/^a=//' | grep -v -- '-' | head -n '1')

      # Do error checking.
      ifneq ($(RELEASE_VER),testing)
        ifneq ($(RELEASE_VER),unstable)
          $(error Release version could not be determined, sorry. Extracted value: $(RELEASE_VER))
        endif
      endif
    endif

    # 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
  else
    VENDOR_UBUNTU := $(shell dpkg-vendor --is 'Ubuntu' && echo 'yes')

    ifeq ($(VENDOR_UBUNTU),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')
    endif
  endif
endif

# Actual version switch.
ifeq ($(VENDOR_DEBIAN),yes)
  ifeq ($(shell /bin/bash -c '(( $(RELEASE_VER) >= 8 )) && echo '"'"'yes'"'"),yes)
    SUBSTVARS = $(SUBSTVARS_MULTI_ARCH_HINT)
  endif
else
  ifeq ($(VENDOR_UBUNTU),yes)
    # Example of how to use major and minor as a selector, we currently won't need
    # this as the transition happened on a major version.
    #ifeq ($(shell /bin/bash -c '(( $(RELEASE_VER_MAJOR) == 16 )) && echo '"'"'yes'"'"),yes)
      #ifeq ($(shell /bin/bash -c '(( $(RELEASE_VER_MINOR) >= 4 )) && echo '"'"'yes'"'"),yes)
        #SUBSTVARS = $(SUBSTVARS_MULTI_ARCH_HINT)
      #endif
    #endif
    ifeq ($(shell /bin/bash -c '(( $(RELEASE_VER_MAJOR) >= 16 )) && echo '"'"'yes'"'"),yes)
      SUBSTVARS = $(SUBSTVARS_MULTI_ARCH_HINT)
    endif
  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)
