#!/usr/bin/make -f

export CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
export CFLAGS:=$(shell dpkg-buildflags --get CFLAGS)
export CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS)
export LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)

RELEASE_VER := 0
RELEASE_VER_MAJOR := 0
RELEASE_VER_MINOR := 0
VENDOR_DEBIAN :=
VENDOR_UBUNTU :=

QT_VERSION := 4

# 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) >= 11 )) && echo '"'"'yes'"'"),yes)
    QT_VERSION = 5
  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) == 20 )) && echo '"'"'yes'"'"),yes)
      #ifeq ($(shell /bin/bash -c '(( $(RELEASE_VER_MINOR) >= 4 )) && echo '"'"'yes'"'"),yes)
        #QT_VERSION = 5
      #endif
    #else
    ifeq ($(shell /bin/bash -c '(( $(RELEASE_VER_MAJOR) >= 20 )) && echo '"'"'yes'"'"),yes)
      QT_VERSION = 5
    endif
  endif
endif

# lrelease fails to run when calling it via user-mode emulation, because the
# lprodump binary tries to fetch the default mkspec by calling "qmake -query"
# and parsing its output, but does so via QProcess. QProcess fails to work in
# user-mode emulation, at least when using the default vfork-like clone ()
# call, so no data can be queried, leading to a hardcoded value being used,
# which can't be resolved and ultimately leads to lprodump and release
# failing.
# Thankfully, we can work around this issue by specifying QMAKESPEC directly.
LRELEASE_WORKAROUND ?= auto
LRELEASE_OVERRIDE :=

ifeq ($(LRELEASE_WORKAROUND),auto)
  LRELEASE_WORKAROUND := 0
  ifeq ($(VENDOR_DEBIAN),yes)
    ifeq ($(shell /bin/bash -c '(( $(RELEASE_VER) >= 12 )) && echo '"'"'yes'"'"),yes)
      ifeq ($(DEB_BUILD_GNU_TYPE),aarch64-linux-gnu)
        LRELEASE_WORKAROUND := 1
      else
        ifeq ($(DEB_BUILD_GNU_TYPE),arm-linux-gnueabihf)
          LRELEASE_WORKAROUND := 1
        endif
      endif
    endif
  endif
endif

ifneq ($(LRELEASE_WORKAROUND),0)
  LRELEASE_OVERRIDE := "LRELEASE_BINARY=QMAKESPEC=$$(qmake -query 'QT_HOST_DATA')/mkspecs/$$(qmake -query 'QMAKE_SPEC') lrelease"
endif

# The preprocessing magic is highly inspired by the firefox package.
IN_FILES := $(wildcard debian/*.in)
preprocessed_filename = $(1:.in=)
define preprocess
$(call preprocessed_filename,$(1)): $(1)
PREPROCESSED_FILES += $(call preprocessed_filename,$(1))
endef
$(foreach f,$(IN_FILES),$(eval $(call preprocess, $(f))))

$(PREPROCESSED_FILES): VARS = QT_VERSION

$(PREPROCESSED_FILES):
	debian/preprocess.sh $< $(foreach var,$(VARS),'$(var)=$($(var))' )

%:
	dh $@ --parallel

override_dh_auto_configure:
	cp debian/changelog res/txt/changelog
	if [ -f ChangeLog.gitlog ]; then cp ChangeLog.gitlog res/txt/git-info; fi
	dh_auto_configure

override_dh_auto_build:
	dh_auto_build -- 'QT_VERSION=$(QT_VERSION)' $(LRELEASE_OVERRIDE)

override_dh_auto_clean:
	# Try to regenerate debian/control if necessary, but don't force it.
	# sbuild will run the clean operation non-chrooted before building the
	# source package, meaning that we're not running in the target system,
	# so forcefully regenerating it will lead to wrong results.
	debian/rules 'debian/control'
	dh_auto_clean
	rm -f res/txt/changelog

override_dh_strip:
	dh_strip -p x2goclient --dbg-package=x2goclient-dbg
