#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

#uncomment if debhelper should be verbose
#export DH_VERBOSE=1

LIBGPS_SONAME := $(strip $(shell grep -E '^libgps_version_current' SConscript | sed 's,.*=[^0-9]*\([0-9]*\).*,\1,'))

export QT_SELECT=qt5

LIBGPS_DEBIAN_FILE_SOURCES := $(strip $(shell ls -1 debian/*LIBGPSSONAME* ))
LIBGPS_DEBIAN_FILE_TARGETS := $(strip $(shell echo $(LIBGPS_DEBIAN_FILE_SOURCES) | sed 's,LIBGPSSONAME,$(LIBGPS_SONAME),g'))

#python versions
#PYTHON3 := $(shell py3versions -d)
PYTHON3 := python3

#debhelper version and settings needed for udev
DH_VERSION := $(shell dpkg -s debhelper | grep '^Version' | awk '{print $$2}')
LSB_RELEASE := $(shell lsb_release -si)
UDEV_BREAKS =

ifeq (Debian,$(strip $(LSB_RELEASE))) ## debian ##
ifeq (0, $(strip $(shell dpkg --compare-versions $(DH_VERSION) ge 7.3.15; echo $$?)))
UDEV_BREAKS = 125
endif
else ## not debian ##
ifeq (Ubuntu,$(strip $(LSB_RELEASE))) ## ubuntu ##
ifeq (0, $(strip $(shell dpkg --compare-versions $(DH_VERSION) ge 7.0.17ubuntu2; echo $$?)))
UDEV_BREAKS = 136-1
endif
endif ## ubuntu ##
endif ## debian ##


DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

SCONSOPTS = \
	prefix=/usr \
	systemd=yes \
	nostrip=yes \
	dbus_export=yes \
	docdir=/usr/share/doc/gpsd \
	libdir=/usr/lib/$(DEB_HOST_MULTIARCH) \
	gpsd_user=gpsd \
	gpsd_group=dialout \
	qt_versioned=5 \
	python_libdir=/usr/lib/python3/dist-packages

ifneq (,$(findstring nodoc,$(DEB_BUILD_OPTIONS)))
	SCONSOPTS += manbuild=no
else
	SCONSOPTS += manbuild=yes
endif

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	SCONSOPTS += debug=yes
else
	SCONSOPTS += debug=no
endif

#package names
PACKAGE_GPSD := gpsd
PACKAGE_CLIENTS := gpsd-clients
PACKAGE_TOOLS := gpsd-tools
PACKAGE_LIB := libgps$(LIB_soname)
PACKAGE_QTLIB := libqgpsmm$(LIB_soname)
PACKAGE_LIB_DEV := libgps-dev
PACKAGE_QTLIB_DEV := libqgpsmm-dev
PACKAGE_PY := python-gps
PACKAGE_PY3 := python3-gps


cflags := $(shell dpkg-buildflags --get CFLAGS)
cppflags := $(shell dpkg-buildflags --get CPPFLAGS)
cxxflags := $(shell dpkg-buildflags --get CXXFLAGS)
ldflags := $(shell dpkg-buildflags --get LDFLAGS)

SCONS_ENV := set -e ;\
		export LDFLAGS="$(ldflags)" ;\
		export CFLAGS="$(cflags)" ;\
		export CXXFLAGS="$(cxxflags)" ;\
		export CPPFLAGS="$(cppflags)";


$(LIBGPS_DEBIAN_FILE_TARGETS): $(LIBGPS_DEBIAN_FILE_SOURCES)
	for source in $(LIBGPS_DEBIAN_FILE_SOURCES); do \
	    target=$$(echo "$$source" | sed 's,LIBGPSSONAME,$(LIBGPS_SONAME),g') ;\
	    sed 's,LIBGPSSONAME,$(LIBGPS_SONAME),g' "$$source" > "$$target" ;\
	done

debian/control: debian/control.in
		rm -f $@
		sed 's,LIBGPSSONAME,$(LIBGPS_SONAME),g' $< > $@
		if which git 1>/dev/null && [ -d .git ]; then \
			git add debian/control ;\
			git commit -m 'Updating debian/control from debian/control.in' debian/control ;\
		fi ;\

SConscript.deb_revision: SConscript
	if [ ! -f $@ ]; then \
		cp $< $@ ;\
		sed -i '/^revision=/s/%(.*)/%("$(DEB_VERSION)",)/' $< ;\
	fi
	touch $@

build-indep: build
build-arch: build

build: build-stamp
build-stamp: SConscript.deb_revision build-static-stamp
	$(SCONS_ENV) $(PYTHON3) /usr/bin/scons shared=yes $(SCONSOPTS) target_python=$(PYTHON3)
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	# disable tests on hurd due to weird ipv6 errors
	# http://deb.li/4C46AEAC.7040906@bzed.de
ifeq (,$(findstring hurd,$(DEB_HOST_ARCH_OS)))

	# remove those binary tests we can't patch with quilt.
	rm -f test/daemon/garmin18x-bin.log
	rm -f test/daemon/sirfstarv.log
	rm -f test/daemon/trimble-lasseniq-bin2d.log
	rm -f test/daemon/ublox-ned-m8t-sbfrx3.log
	rm -f test/daemon/ublox-neo-m8t-s.log
	rm -f test/daemon/ublox-neo-m8t.log
	rm -f test/daemon/ublox-zed-f9p-hp.log
	rm -f test/daemon/ublox-zed-f9p_hpg1.11.log
	rm -f test/daemon/ublox-zed-f9t-rtcm3.log

	-$(SCONS_ENV) $(PYTHON3) /usr/bin/scons check
endif
endif
	touch $@

build-static-stamp: SConscript.deb_revision
	$(SCONS_ENV) $(PYTHON3) /usr/bin/scons shared=no $(SCONSOPTS) target_python=$(PYTHON3)
	cp gpsd-*/libgps.a libgps.a
	rm -rf gpsd-*
	touch $@

clean: debian/control
	dh_testdir
	dh_testroot
	# revert revision changes in SConscript
	if [ -f SConscript.deb_revision ]; then \
	    mv SConscript.deb_revision SConscript; \
	fi
	dh_clean
	rm -rf \
		buildtmp \
		gpsd-* \
	    .sconf_temp \
	    .scons-option-cache \
	    .sconsign.dblite \
	    .sconsign.*.dblite \
	    gps/*.pyc \
	    TROUBLESHOOTING \
	    debian/buildinfo.gz \
	    config.log \
	    debian/shlibs.local \
	    leapsecond.pyc \
	    libgps.a \
	    $(LIBGPS_DEBIAN_FILE_TARGETS) \
	    __pycache__
: install-stamp
install-stamp: build-stamp build-static-stamp $(LIBGPS_DEBIAN_FILE_TARGETS)
	dh_testdir
	dh_testroot
	dh_prep
	$(SCONS_ENV) DESTDIR=`pwd`/debian/tmp $(PYTHON3) /usr/bin/scons install $(SCONSOPTS) target_python=$(PYTHON3)
	$(SCONS_ENV) DESTDIR=`pwd`/debian/tmp $(PYTHON3) /usr/bin/scons systemd_install $(SCONSOPTS) target_python=$(PYTHON3)
	# install static lib
	cp libgps.a debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)
	cp INSTALL.adoc TROUBLESHOOTING.adoc
	# we collect our own documentation
	rm -rf debian/tmp/usr/share/doc/gpsd
	# install everything
	dh_install
	# quirk to avoid issues by broken fakeroot misdetecting links as files and
	# then breaking dh_install -> cp -a fatally
	ln -s libgps.so.$(LIBGPS_SONAME).0.0 debian/libgps$(LIBGPS_SONAME)/usr/lib/$(DEB_HOST_MULTIARCH)/libgps.so.$(LIBGPS_SONAME)
	rm debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libgps.so.$(LIBGPS_SONAME)
	dh_missing --fail-missing
	touch $@
: install
install-arch: install

binary: install-stamp
	dh_testdir
	dh_testroot
	dh_installchangelogs  NEWS
	dh_installdocs
	dh_installman
	dh_installmenu
	dh_apparmor --profile-name=usr.sbin.gpsd -pgpsd
	dh_installsystemd -pgpsd --restart-after-upgrade gpsdctl@.service gpsd.socket
	dh_installsystemd -pgpsd --no-enable --no-start gpsd.service
	dh_installinit --no-start
	dh_installsystemd -pgpsd --restart-after-upgrade
	chmod 755 debian/$(PACKAGE_PY3)/usr/lib/python3*/*-packages/gps/gps.py
	dh_python3 -Ngpsd-clients
	dh_python3 -pgpsd-clients usr/bin
	dh_python3 -pgpsd lib/udev/
	cp $(CURDIR)/gpsd-*/gpsd.rules $(CURDIR)/debian/gpsd.udev
	dh_installudev
	dh_link
	dh_strip --dbgsym-migration='gpsd-dbg (<< 3.16-4~)'
	dh_compress
	dh_fixperms  -Xgpsd.hotplug
	dh_lintian
ifeq (,$(DEB_BUILD_OPTIONS))
	dh_makeshlibs -Nlibqgpsmm$(LIBGPS_SONAME) -- -c0
	dh_makeshlibs -plibqgpsmm$(LIBGPS_SONAME) -- -c0
else
	dh_makeshlibs
endif
	dh_shlibdeps
ifneq (,$(UDEV_BREAKS))
	echo "gpsd:Breaks=udev (<< $(UDEV_BREAKS))" >> $(CURDIR)/debian/gpsd.substvars
endif
	dh_gencontrol
	dh_installdeb
	dh_md5sums
	dh_builddeb


binary-indep: binary
binary-arch: binary

.PHONY: binary binary-indep binary-arch \
	install install-arch install-indep \
	clean \
	build debian/control
