#!/usr/bin/make -f


PKG  = $(word 2,$(shell dpkg-parsechangelog | grep Source))
VER ?= $(shell dpkg-parsechangelog | grep Version: | awk -F':' '{print $$NF}' | sed -e 's/-.*//')
ORIG_VER = 3.4.0
URL_DOWNLOAD = "https://github.com/asterisk/dahdi-tools/releases/download/v${ORIG_VER}/dahdi-tools-${ORIG_VER}.tar.gz"

include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/class/autotools.mk

# ./configure is not shipped in the archive, do same step as ./bootstrap.sh
DEB_AUTO_UPDATE_AUTOCONF = 2.69
DEB_AUTO_UPDATE_AUTOMAKE = 1.16
DEB_AUTO_UPDATE_AUTOHEADER = $(DEB_AUTO_UPDATE_AUTOCONF)
DEB_AUTO_UPDATE_ACLOCAL = $(DEB_AUTO_UPDATE_AUTOMAKE)
DEB_AUTO_UPDATE_LIBTOOL = pre
DEB_AUTOMAKE_ARGS = --add-missing --copy

DEB_DH_INSTALLINIT_ARGS = -- defaults 26 74
DEB_CONFIGURE_EXTRA_FLAGS = --with-perllib=/usr/share/perl5

pre-build::
	mkdir -p auxdir

binary-install/dahdi::
	cp $(DEB_SRCDIR)/init.conf.sample $(CURDIR)/debian/$(cdbs_curpkg)/etc/dahdi/init.conf
	cp $(DEB_SRCDIR)/blacklist.sample $(CURDIR)/debian/$(cdbs_curpkg)/etc/modprobe.d/dahdi.blacklist.conf
	rm $(CURDIR)/debian/$(cdbs_curpkg)/etc/dahdi/modules.sample
	rm $(CURDIR)/debian/$(cdbs_curpkg)/etc/dahdi/system.conf.sample
	find $(CURDIR)/debian/$(cdbs_curpkg)/etc -type f -exec chmod a-x {} \;
	#
	cp $(DEB_SRCDIR)/dahdi.init $(CURDIR)/debian/$(cdbs_curpkg).dahdi.init
	cp $(DEB_SRCDIR)/xpp/genconf_parameters $(CURDIR)/debian/$(cdbs_curpkg)/etc/dahdi/
	for app in patgen pattest patlooptest hdlcstress hdlctest hdlcgen hdlcverify timertest dahdi_maint; do \
		cp $(DEB_SRCDIR)/$$app $(CURDIR)/debian/$(cdbs_curpkg)/usr/sbin; \
	done
	cp $(DEB_SRCDIR)/modules.sample $(CURDIR)/debian/$(cdbs_curpkg)/usr/share/dahdi/
	dh_installinit --name=dahdi
	dh_installdocs $(DEB_SRCDIR)/UPGRADE.txt
	rm $(CURDIR)/debian/$(cdbs_curpkg).dahdi.init

clean::
	rm -rf *.cdbs-config_list aclocal.m4 autoconfig.h.in auxdir configure m4
	find -name Makefile.in -delete

.PHONY: get-orig-source

get-orig-source:
	@echo "# Downloading..."
	wget -nv -T10 -t3 $(URL_DOWNLOAD) -O ../$(PKG)_$(VER).orig.tar.gz
