#!/usr/bin/make -f

# 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"

# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1

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

# Support more robust code and makes code modifications more difficult
# Compare https://wiki.debian.org/Hardening
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# Trigger other tools to use the timestamp from debian/changelog
# Compare https://reproducible-builds.org/docs/source-date-epoch/
export SOURCE_DATE_EPOCH ?= $(shell dpkg-parsechangelog -STimestamp)

# Set time & date to the most recent release, for repeatable builds.
TIMESTAMP=$(shell dpkg-parsechangelog -S timestamp)
export DATE=$(shell LANG=C date --date='@$(TIMESTAMP)' '+%d\ %b\ %Y')
export TIME=$(shell LANG=C date --date='@$(TIMESTAMP)' '+%T')

kernel_version = uspace
configure_realtime_arg = --with-realtime=uspace
enable_build_documentation = --enable-build-documentation=pdf
DESTDIR=$(CURDIR)/debian/tmp
DEV_PACKAGE_NAME=linuxcnc-uspace-dev

%:
	dh $@

override_dh_auto_configure:
	cd src && ./autogen.sh
	cd src && PYTHON=/usr/bin/python3 ./configure \
	    --prefix=/usr --sysconfdir=/etc \
	    --mandir=/usr/share/man \
	    $(configure_realtime_arg) \
	    $(enable_build_documentation) \
	    --disable-check-runtime-deps

override_dh_auto_build-arch:
	$(MAKE) PYTHON=/usr/bin/python3 -C src build-software

override_dh_auto_build-indep:
ifneq "$(enable_build_documentation)" ""
	$(MAKE) PYTHON=/usr/bin/python3 -C src manpages
	$(MAKE) PYTHON=/usr/bin/python3 -C src translateddocs
	$(MAKE) PYTHON=/usr/bin/python3 -C src docs
endif

override_dh_auto_clean:
	dh_auto_clean
	py3clean .
	if [ -r src/Makefile.inc -a -r src/config.status ]; then $(MAKE) -C src clean -s; fi
	rm -f Makefile.inc
	rm -f src/config.log src/config.status
	rm -f $(for i in $(find . -name "*.in"); do basename $i .in; done)
	dh_clean


override_dh_auto_install-arch:
	# Install all architecture-dependent libraries and executables
	# in DESTDIR (the staging dir for the main package).
	DESTDIR=$(DESTDIR) $(MAKE) -C src install-software
	py3clean .

	desktop-file-validate $(shell find debian/extras/ share/applications/ -name *.desktop);
	
	# some clean-up
	rm -f $(DESTDIR)/usr/share/doc/linuxcnc-uspace/examples/sample-configs/*/*position*.txt

override_dh_installdocs-arch:
	# Sample configs go in `usr/share/doc/linuxcnc` (not
	# `.../doc/linuxcnc-uspace`) because that's where the
	# `linuxcnc` launcher script looks for them, and that's
	# inconvenient to change.
	dh_installdocs --doc-main-package=linuxcnc-uspace --package=linuxcnc-uspace
	mkdir -p debian/linuxcnc-uspace/usr/share/doc/linuxcnc
	mv debian/linuxcnc-uspace/usr/share/doc/linuxcnc-uspace/examples debian/linuxcnc-uspace/usr/share/doc/linuxcnc
	
	dh_installdocs --doc-main-package=linuxcnc-uspace --package=linuxcnc-uspace-dev


override_dh_auto_install-indep:
	DESTDIR=$(DESTDIR) $(MAKE) -C src install-docs install-doc
	# Remove the docs we just built that we don't have debs for yet...
	rm -f $(DESTDIR)/usr/share/doc/linuxcnc/*_nb.pdf

override_dh_installdocs-indep:
	# The G-code quick reference html docs are built as
	# `docs/html/${LANG}/gcode.html`.  We install them
	# into `/usr/share/doc/linuxcnc-uspace/` in each
	# linuxcnc-doc-${LANG} package, then rename them there (except
	# for the English one) to include the language in the filename,
	# so they don't collide when we install all the doc debs.
	#
	# Rename the "main" doc directories from
	# `usr/share/doc/linuxcnc-uspace` to `usr/share/doc/linuxcnc`,
	# because that's where the `.desktop` files look for the docs.
	dh_installdocs --doc-main-package=linuxcnc-uspace --package=linuxcnc-doc-de
	mv debian/linuxcnc-doc-de/usr/share/doc/linuxcnc-uspace/gcode.html debian/linuxcnc-doc-de/usr/share/doc/linuxcnc-uspace/gcode_de.html
	mv debian/linuxcnc-doc-de/usr/share/doc/linuxcnc-uspace debian/linuxcnc-doc-de/usr/share/doc/linuxcnc
	
	dh_installdocs --doc-main-package=linuxcnc-uspace --package=linuxcnc-doc-en
	mv debian/linuxcnc-doc-en/usr/share/doc/linuxcnc-uspace debian/linuxcnc-doc-en/usr/share/doc/linuxcnc
	
	dh_installdocs --doc-main-package=linuxcnc-uspace --package=linuxcnc-doc-es
	mv debian/linuxcnc-doc-es/usr/share/doc/linuxcnc-uspace/gcode.html debian/linuxcnc-doc-es/usr/share/doc/linuxcnc-uspace/gcode_es.html
	mv debian/linuxcnc-doc-es/usr/share/doc/linuxcnc-uspace debian/linuxcnc-doc-es/usr/share/doc/linuxcnc
	
	dh_installdocs --doc-main-package=linuxcnc-uspace --package=linuxcnc-doc-fr
	mv debian/linuxcnc-doc-fr/usr/share/doc/linuxcnc-uspace/gcode.html debian/linuxcnc-doc-fr/usr/share/doc/linuxcnc-uspace/gcode_fr.html
	mv debian/linuxcnc-doc-fr/usr/share/doc/linuxcnc-uspace debian/linuxcnc-doc-fr/usr/share/doc/linuxcnc
	
	dh_installdocs --doc-main-package=linuxcnc-uspace --package=linuxcnc-doc-zh-cn
	mv debian/linuxcnc-doc-zh-cn/usr/share/doc/linuxcnc-uspace/gcode.html debian/linuxcnc-doc-zh-cn/usr/share/doc/linuxcnc-uspace/gcode_zh_CN.html
	mv debian/linuxcnc-doc-zh-cn/usr/share/doc/linuxcnc-uspace debian/linuxcnc-doc-zh-cn/usr/share/doc/linuxcnc


override_dh_compress:
	dh_compress -X.pdf -X.txt -X.hal -X.ini -X.clp -X.var -X.nml -X.tbl -X.xml -Xsample-configs

override_dh_fixperms:
	dh_fixperms -X/linuxcnc_module_helper -X/rtapi_app
	# In case that only the indep packages are built
	if [ -r  "$(DESTDIR)/usr/lib/tcltk/linuxcnc/linuxcnc.tcl" ]; then \
		chmod -x $(DESTDIR)/usr/lib/tcltk/linuxcnc/linuxcnc.tcl; \
	fi
	# override_dh_python3: # not executed, so we attach it to fixperms
	DEB_HOST_ARCH=`dpkg-architecture -qDEB_HOST_ARCH` dh_python3

override_dh_shlibdeps:
	cat debian/linuxcnc-uspace/DEBIAN/shlibs debian/shlibs.pre > debian/shlibs.local
	dh_shlibdeps -u--warnings=0 -l debian/linuxcnc-uspace/usr/lib
