#!/usr/bin/make -f

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

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

# This has to be exported to make some magic below work.
#export DH_OPTIONS
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
arch = $(shell dpkg-architecture -qDEB_BUILD_ARCH)
MOD_VERSION = $(DEB_VERSION_UPSTREAM)

DAHDI_DIR = $(CURDIR)/debian/tmp/modules
OSMOCOM_DAHDI = osmocom-dahdi

%:
	dh $@ --with autoreconf

# Don't want to build. Just send source code to
# package to be built by DKMS
override_dh_auto_build:
	# Replace #MOD_VERSION# with actual version
	sed -e "s|#MOD_VERSION#|$(MOD_VERSION)|" \
	debian/osmocom-dahdi-dkms.install.in > debian/osmocom-dahdi-dkms.install
	sed -e "s|#MOD_VERSION#|$(MOD_VERSION)|" \
	debian/dkms.in > debian/dkms.conf
	sed -e "s|#MOD_VERSION#|$(MOD_VERSION)|" \
	debian/osmocom-dahdi-dkms.postinst.in > debian/osmocom-dahdi-dkms.postinst
	sed -e "s|#MOD_VERSION#|$(MOD_VERSION)|" \
	debian/osmocom-dahdi-dkms.prerm.in > debian/osmocom-dahdi-dkms.prerm

	mkdir -p $(DAHDI_DIR)/$(OSMOCOM_DAHDI)
	cp Makefile $(DAHDI_DIR)/$(OSMOCOM_DAHDI)/

	for dir in build_tools firmware include drivers; do \
	  if [ -d $$dir ]; then cp -r $$dir  $(DAHDI_DIR)/$(OSMOCOM_DAHDI); fi; \
	done

	# Change encoding for file for source oct6100_remote_debug.c
	./debian/national-encoding.sh
	# Tarball source code for osmocom-dahdi-source pkg
	tar -czf $(OSMOCOM_DAHDI).tar.gz  -C $(CURDIR)/debian/tmp modules/

# exclude LICENSE & README from being built into -dkms package
override_dh_install:
	dh_install -XLICENSE.firmware -XREADME

override_dh_clean:
	rm -rf $(DAHDI_DIR)/$(OSMOCOM_DAHDI)
	dh_clean
	$(RM) .tarball-version

override_dh_autoreconf:
	echo $(DEB_VERSION_UPSTREAM) > .tarball-version
	dh_autoreconf
override_dh_auto_test:
override_dh_auto_install:
override_dh_auto_clean:
