#!/usr/bin/make -f
#
# Copyright 2006 - 2018 Ron Lee.

SHELL = /bin/bash

NUM_CPUS = $(shell getconf _NPROCESSORS_ONLN 2>/dev/null)
PARALLEL = $(subst parallel=,,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
NJOBS    = -j$(or $(PARALLEL),$(NUM_CPUS),1)


export DH_OPTIONS

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
    # No optimisation, with debug syms
    DEBUG_OPTS = --enable-debug
else
    # Optimise, but include debug syms, detached in the -dbg package
    DEBUG_OPTS = --enable-debug-syms
endif


# Name of the source package
psource:=vpb-driver-source

# The short upstream name, used for the module source directory
sname:=vpb-driver

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)


### KERNEL SETUP
### Setup the stuff needed for making kernel module packages
### taken from /usr/share/kernel-package/sample.module.rules

# prefix of the target package name
PACKAGE=vpb-driver-modules
# modifieable for experiments or debugging m-a
MA_DIR ?= /usr/share/modass
# load generic variable handling
-include $(MA_DIR)/include/generic.make
# load default rules, including kdist, kdist_image, ...
-include $(MA_DIR)/include/common-rules.make

# module assistant calculates all needed things for us and sets
# following variables:
# KSRC (kernel source directory), KVERS (kernel version string), KDREV
# (revision of the Debian kernel-image package), CC (the correct
# compiler), VERSION (the final package version string), PKGNAME (full
# package name with KVERS included), DEB_DESTDIR (path to store DEBs)

# The kdist_configure target is called by make-kpkg modules_config and
# by kdist* rules by dependency. It should configure the module so it is
# ready for compilation (mostly useful for calling configure).
# prep-deb-files from module-assistant creates the neccessary debian/ files
kdist_configure: prep-deb-files

# the kdist_clean target is called by make-kpkg modules_clean and from
# kdist* rules. It is responsible for cleaning up any changes that have
# been made by the other kdist_commands (except for the .deb files created)
# NOTE that removing debian control here is dangerous if this is ever called
# from outside the module source dist.  Don't do that.  We need to update it
# if the kernel version changes though and this is one simple way to ensure
# that happens.  There may be a better one.
pre-clean:
	$(RM) debian/control

kdist_clean: pre-clean clean
	$(MAKE) -f debian/rules clean
#	rm -f driver/*.o driver/*.ko
#
### end  KERNEL SETUP

clean: debian/control
	dh_testdir

#	Drop the old binary blob from the debian source during initial export.
	$(RM) -r firmware

	[ ! -e Makefile ] || $(MAKE) distclean
	[ ! -e obj_shared/Makefile ] || $(MAKE) -C obj_shared distclean
	$(RM) build-arch-stamp build-indep-stamp
	$(RM) -r obj_shared obj_static

	dh_clean

#	We don't need to run this in the module build, there are no templates
#	for that package and this will error out if it doesn't find the po dir.
	[ ! -d debian/po ] || debconf-updatepo


debian/control:
	sed -e 's/_KVERS_/$(KVERS)/' debian/control.modules.in > debian/control


obj_shared/config.status: configure
	dh_testdir

	mkdir obj_shared
	cd obj_shared && ../configure --host=$(DEB_HOST_GNU_TYPE)   \
				      --build=$(DEB_BUILD_GNU_TYPE) \
				      --prefix=/usr                 \
				      --enable-shared               \
				      $(DEBUG_OPTS)

obj_static/config.status: configure
	dh_testdir

	mkdir obj_static
	cd obj_static && ../configure --host=$(DEB_HOST_GNU_TYPE)   \
				      --build=$(DEB_BUILD_GNU_TYPE) \
				      --prefix=/usr                 \
				      $(DEBUG_OPTS)

build: build-arch

build-arch: build-arch-stamp
build-arch-stamp: obj_shared/config.status obj_static/config.status
	dh_testdir

	$(MAKE) $(NJOBS) -C obj_shared/src/libtoneg
	$(MAKE) $(NJOBS) -C obj_shared/src/libvpb
	$(MAKE) $(NJOBS) -C obj_shared/src/utils
	$(MAKE) $(NJOBS) -C obj_shared/vpb-detect

	$(MAKE) $(NJOBS) -C obj_static/src/libtoneg
	$(MAKE) $(NJOBS) -C obj_static/src/libvpb
	touch $@

build-indep:  build-indep-stamp
build-indep-stamp: build-arch-stamp obj_shared/config.status
	dh_testdir

	$(MAKE) $(NJOBS) -C obj_shared doc
	touch $@


install: install-arch install-indep

install-arch: install-libvpb install-libvpb-dev install-vpb-utils
install-indep: install-vpb-driver-source install-libvpb-doc

# The explicit export here avoids SOURCE_DATE_EPOCH being hidden from dpkg-deb.
# It should normally be set in the environment, but set it here if it was not.
# We need to test if --sort=name is supported, the tar 1.28 release added that.
install-vpb-driver-source: export SOURCE_DATE_EPOCH ?= $(shell dpkg-parsechangelog | grep -Po '^Date: \K.*' | date -f- +%s)
install-vpb-driver-source: TAR_OPTS  = $(shell tar --sort=name -cf - /dev/null >/dev/null 2>&1 && echo "--sort=name")
install-vpb-driver-source: TAR_OPTS += --mode='go=rX,u+rw,a-s' --mtime='@$(SOURCE_DATE_EPOCH)'
install-vpb-driver-source: TAR_OPTS += --owner=0 --group=0 --numeric-owner
install-vpb-driver-source: DH_OPTIONS=-p$(psource)
install-vpb-driver-source:
	dh_testdir
	dh_testroot
	dh_prep

	# Create the directories to install the source into
	dh_installdirs usr/src/modules/$(sname)/debian
	dh_installdirs usr/src/modules/$(sname)/src

	cp obj_shared/Version debian/$(psource)/usr/src/modules/$(sname)

	# Copy only the driver source to the proper location
	cp -a src/{vtcore,vpb} debian/$(psource)/usr/src/modules/$(sname)/src

	cp src/libvpb/vtcore_ioctl.h src/libvpb/messages_dsp_pc.h \
	   debian/$(psource)/usr/src/modules/$(sname)/src/vtcore
	cp src/libvpb/vpb_ioctl.h                                 \
	   debian/$(psource)/usr/src/modules/$(sname)/src/vpb

	# Copy the needed debian/ pieces to the proper location
	cp debian/*modules.in* \
	   debian/$(psource)/usr/src/modules/$(sname)/debian
#	cp debian/*_KVERS_* debian/rules debian/changelog debian/copyright
	cp debian/rules debian/changelog debian/copyright debian/compat \
	   debian/$(psource)/usr/src/modules/$(sname)/debian/
	cd debian/$(psource)/usr/src					\
	   && tar c $(TAR_OPTS) modules	| bzip2 -9 > $(sname).tar.bz2	\
	   && rm -rf modules

	dh_install debian/fetch-openline-firmware usr/share/vpb-driver
	chmod 755 $(CURDIR)/debian/vpb-driver-source/usr/share/vpb-driver/fetch-openline-firmware

install-libvpb: DH_OPTIONS=-plibvpb1
install-libvpb:
	dh_testdir
	dh_testroot
	dh_prep

	$(MAKE) -C obj_shared/src/libtoneg install-lib DESTDIR=$(CURDIR)/debian/libvpb1
	$(MAKE) -C obj_shared/src/libvpb   install-lib DESTDIR=$(CURDIR)/debian/libvpb1
	$(MAKE) -C obj_shared/vpb-detect   install     DESTDIR=$(CURDIR)/debian/libvpb1
	dh_install src/utils/vtdeviceinfo               usr/bin
	dh_install src/utils/vtdriverinfo               usr/bin
	dh_install obj_shared/src/utils/VpbConfigurator usr/sbin
	dh_installman doc/vtdeviceinfo.1
	dh_installman doc/vtdriverinfo.1
	dh_installman doc/VpbConfigurator.8
	dh_installman doc/vpbconf.1
	dh_installman doc/vpbscan.1
	dh_installudev --name=vtcore
	dh_installdebconf

install-libvpb-dev: DH_OPTIONS=-plibvpb-dev
install-libvpb-dev:
	dh_testdir
	dh_testroot
	dh_prep

	$(MAKE) -C obj_shared/src/libtoneg install-dev DESTDIR=$(CURDIR)/debian/libvpb-dev
	$(MAKE) -C obj_static/src/libtoneg install-dev DESTDIR=$(CURDIR)/debian/libvpb-dev
	$(MAKE) -C obj_shared/src/libvpb   install-dev DESTDIR=$(CURDIR)/debian/libvpb-dev
	$(MAKE) -C obj_static/src/libvpb   install-dev DESTDIR=$(CURDIR)/debian/libvpb-dev

install-vpb-utils: DH_OPTIONS=-pvpb-utils
install-vpb-utils:
	dh_testdir
	dh_testroot
	dh_prep

	dh_install obj_shared/src/utils/dtmfcheck   usr/bin
	dh_install obj_shared/src/utils/measerl     usr/bin
	dh_install obj_shared/src/utils/playwav     usr/bin
	dh_install obj_shared/src/utils/proslicerl  usr/bin
	dh_install obj_shared/src/utils/raw2wav     usr/bin
	dh_install obj_shared/src/utils/recwav      usr/bin
	dh_install obj_shared/src/utils/ringstat    usr/bin
	dh_install obj_shared/src/utils/tonedebug   usr/bin
	dh_install obj_shared/src/utils/tonegen     usr/bin
	dh_install obj_shared/src/utils/tonetrain   usr/bin
	dh_install obj_shared/src/utils/vdaaerl     usr/bin
	dh_install obj_shared/src/utils/vpbecho     usr/bin
	dh_installman doc/dtmfcheck.1
	dh_installman doc/measerl.1
	dh_installman doc/playwav.1
	dh_installman doc/proslicerl.1
	dh_installman doc/raw2wav.1
	dh_installman doc/recwav.1
	dh_installman doc/ringstat.1
	dh_installman doc/tonedebug.1
	dh_installman doc/tonegen.1
	dh_installman doc/tonetrain.1
	dh_installman doc/vdaaerl.1
	dh_installman doc/vpbecho.1

install-libvpb-doc: DH_OPTIONS=-plibvpb-doc
install-libvpb-doc:
	dh_testdir
	dh_testroot
	dh_prep

	dh_install obj_shared/src/libvpb/doc/doc_user/html usr/share/doc/libvpb-doc
	#dh_installman src/libvpb/doc/doc_user/man/man3/*


binary: binary-indep binary-arch

binary-indep: build-indep install-indep
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -i
	dh_installchangelogs -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: build-arch install-arch
	dh_testdir -a
	dh_testroot -a
	dh_installdocs -a
	dh_installchangelogs -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -plibvpb1 -V"libvpb1 (>=4.2.22)"
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

#k = $(shell echo $(KVERS) | grep -q ^2.6 && echo k)

# the binary-modules rule is invoked by module-assistant while processing the
# kdist* targets. It is called by module-assistant or make-kpkg and *not*
# during a normal build
binary-modules:
	dh_testroot
	dh_prep

	$(MAKE) $(NJOBS) -C src/vtcore						\
		KSRC=$(KSRC) KVERS=$(KVERS) DESTDIR=$(CURDIR)/debian/$(PKGNAME)
	$(MAKE) $(NJOBS) -C src/vpb						\
		KSRC=$(KSRC) KVERS=$(KVERS) DESTDIR=$(CURDIR)/debian/$(PKGNAME)

	$(MAKE) -C src/vtcore install						\
		KSRC=$(KSRC) KVERS=$(KVERS) DESTDIR=$(CURDIR)/debian/$(PKGNAME)
	$(MAKE) -C src/vpb install						\
		KSRC=$(KSRC) KVERS=$(KVERS) DESTDIR=$(CURDIR)/debian/$(PKGNAME)

	dh_installmodules
	dh_installchangelogs
	#dh_installdocs
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol -- -v$(VERSION)
	dh_md5sums
	dh_builddeb --destdir=$(DEB_DESTDIR)


.PHONY: clean build build-arch build-indep                                   \
	binary binary-indep binary-arch binary-modules                       \
	install install-arch install-indep install-libvpb install-libvpb-dev \
	install-vpb-utils install-vpb-driver-source install-libvpb-doc       \
	kdist kdist_configure kdist_image kdist_clean pre-clean
