#!/usr/bin/make -f
# Simple set of debian/rules that uses debhelper.
# Based on an example by Joey Hess.
# extended by Paul Corner, Alex Joni, Chris Radek, Jeff Epler, John
# Morris (in cronological order)

# Copyright (C) 2006 Jeff Epler
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# When used to produce a debian package, this file is a script "used to
# control compilation and installation of the executable"

# For cross-compiling
include /usr/share/dpkg/architecture.mk
ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
DEB_BUILD_OPTIONS += nocheck
endif
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# dpkg-shlibdeps warning exclusions
SHLIBDEPS_X :=  # add patterns here
#
# The name 'libtk8.6.so' can't be parsed by dpkg-shlibdeps, it seems:
#     "can't extract name and version from library name 'libtk8.6.so'
SHLIBDEPS_X += linuxcnc.so _togl.so
#
# libcanterp.so.0 and librs274.so.0: hack for now:  see
# https://github.com/machinekit/machinekit/issues/324
#     "symbol ... used by ... found in none of the libraries"
SHLIBDEPS_X += libcanterp.so.0 librs274.so.0
#
# halmeter classicladder halscope link to $(GTK_LIBS), which includes
# several libs that aren't used (but several that are needed)
SHLIBDEPS_X += halmeter classicladder halscope

# Enable/disable thread flavors; all flavors disabled by default
THREADS_POSIX = --without-posix
THREADS_RT_PREEMPT = --without-rt-preempt
THREADS_XENOMAI = --without-xenomai

# Kernel threads need a list of configured header source directories
# for each architecture
#
# Multiples should be space-separated
#
DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH)

# Parallel make
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    MAKEFLAGS += -j$(NUMJOBS)
endif
# Only set 'CC' if explicitly defined in environment; otherwise 'make'
# chooses CC=cc and CXX=g++ for defaults
ifneq ($(origin CC),default)
    COMPILE_ENV += CC="$(CC)"
endif
ifneq ($(origin CXX),default)
    COMPILE_ENV += CXX="$(CXX)"
endif


debian/control: debian/configure
#	# if debian/configure has not been run, error out
	if ! test -f debian/configure.cmdline; then \
	    @echo "Please run debian/configure" \
	        "to generate the debian/control file."; \
	    false; \
	fi
	@echo rebuilding debian/control


build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp: debian/control
	dh_testdir

# Add here commands to compile the package.
	cd src && ./autogen.sh
	cd src && env $(COMPILE_ENV) \
	    ./configure --prefix=/usr \
	    --build=$(DEB_BUILD_MULTIARCH) \
	    --host=$(DEB_HOST_MULTIARCH) \
	    $(THREADS_POSIX) \
	    $(THREADS_RT_PREEMPT) \
	    $(THREADS_XENOMAI) \
	    --sysconfdir=/etc
	cd src && $(MAKE) $(COMPILE_ENV) $(MAKEFLAGS) V=$(DH_VERBOSE)
	touch build-stamp

clean: debian/control
	dh_testdir
	dh_testroot
	rm -f build-stamp

#	# Only run configure if the tree looks dirty. Running
#	# ./configure in qemu is expensive.
ifneq ($(wildcard src/configure src/Makefile.inc),)
	cd src && ./autogen.sh
	cd src && env $(COMPILE_ENV) \
	    ./configure --prefix=/usr \
	    --build=$(DEB_BUILD_MULTIARCH) \
	    --host=$(DEB_HOST_MULTIARCH) \
	    $(THREADS_POSIX) \
	    $(THREADS_RT_PREEMPT) \
	    $(THREADS_XENOMAI) \
	    --sysconfdir=/etc
	cd src && $(MAKE) clean -s
#	# Manually remove things left by './configure'
	rm -f \
	    lib/python/machinekit/config.py \
	    lib/python/nf.py \
	    scripts/check-build-vs-configure-sha \
	    scripts/gen-rtapi.ini.sh \
	    scripts/gen-rtapi.ini.sh.in \
	    scripts/halrun \
	    scripts/machinekit.ini.tmpl \
	    scripts/realtime \
	    scripts/rip-environment \
	    src/Makefile.inc \
	    src/Makefile.modinc \
	    src/aclocal.m4 \
	    src/config.h \
	    src/config.h.in \
	    src/config.log \
	    src/config.status \
	    src/configure \
	    src/machinekitcfg.py-tmp
	rm -rf src/autom4te.cache etc
endif

#	# Remove package artifacts
	dh_clean


install: build

#	# have to test after build or Makefile.inc will not exist ##
	cp debian/machinekit-hal-posix.install.in debian/machinekit-hal-posix.install
	cp debian/machinekit-hal-rt-preempt.install.in debian/machinekit-hal-rt-preempt.install
	cp debian/machinekit-hal-xenomai.install.in debian/machinekit-hal-xenomai.install

	if (grep ^USERMODE_PCI=yes src/Makefile.inc -q); then \
	    echo "usr/include/linuxcnc/userpci/*.h" >> debian/machinekit-hal-posix.install; \
	    echo "usr/include/linuxcnc/userpci/*.h" >> debian/machinekit-hal-rt-preempt.install; \
	    echo "usr/include/linuxcnc/userpci/*.h" >> debian/machinekit-hal-xenomai.install; \
	fi

#	# only want this for armhf builds ##
	if [ ! -f debian/platform_pc ] ; then \
	    echo "usr/lib/linuxcnc/prubin/*" >> debian/machinekit-hal-posix.install; \
	    echo "usr/lib/linuxcnc/prubin/*" >> debian/machinekit-hal-rt-preempt.install; \
	    echo "usr/lib/linuxcnc/prubin/*" >> debian/machinekit-hal-xenomai.install; \
	fi

	dh_testdir
	dh_testroot
	dh_prep

	mkdir -p debian/tmp/usr/lib/python2.7/dist-packages/gladevcp
	dh_installdirs
# start the install
	mkdir -p debian/tmp
	(cd debian/extras && cp -a * ../tmp)
	(cd src; export DESTDIR=`pwd`/../debian/tmp; $(MAKE) V=$(DH_VERBOSE) $@)
	mkdir -p debian/tmp/usr/lib debian/tmp/usr/include/linuxcnc \
	    debian/tmp/usr/include/linuxcnc/machinetalk/nanopb \
	    debian/tmp/usr/include/linuxcnc/machinetalk/protobuf 


#ifneq (,$(wildcard debian/machinekit-*-kernel-*.install))
	mkdir -p debian/tmp/etc/udev/rules.d
	cp src/rtapi/shmdrv/shmdrv.rules \
	    debian/tmp/etc/udev/rules.d/50-shmdrv.rules
#endif
	mkdir -p debian/tmp/etc/security/limits.d
	cp src/rtapi/shmdrv/limits.d-machinekit.conf \
	    debian/tmp/etc/security/limits.d/machinekit.conf

#	# only want this for armhf builds too ##
	if [ -f debian/platform_pc ] ; then \
	    rm -f debian/platform_pc; \
	else \
	    mkdir -p debian/tmp/usr/lib/linuxcnc/prubin; \
	    cp rtlib/prubin/* debian/tmp/usr/lib/linuxcnc/prubin; \
	    cat debian/posix-postinst.add >> debian/machinekit-hal-posix.postinst; \
	    cat debian/rt-preempt-postinst.add >> debian/machinekit-hal-rt-preempt.postinst; \
	    cat debian/xenomai-postinst.add >> debian/machinekit-hal-xenomai.postinst; \
	fi

	dh_install --sourcedir=debian/tmp --fail-missing -Xusr/bin/pasm

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installexamples
	dh_installman
	dh_installmime
	dh_link
	dh_strip
	dh_compress -X.pdf -X.txt -X.hal -X.ini -X.clp -X.var -X.nml \
	    -X.tbl -X.xml -Xsample-configs
	dh_fixperms -X/linuxcnc_module_helper -X/rtapi_app_
	dh_python2 --ignore-shebangs --no-guessing-versions --no-ext-rename
	dh_makeshlibs
	dh_installdeb

	cat debian/machinekit-hal-posix/DEBIAN/shlibs debian/shlibs.pre > \
	    debian/shlibs.local
        #enable buster builds to work with problematic shlib deps in current packages
	dh_shlibdeps --dpkg-shlibdeps-params=--ignore-missing-info \
            $(foreach p, $(SHLIBDEPS_X), -X $(p)) -X hal.so
	dh_gencontrol

	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install
