#!/usr/bin/make -f
# rules
#
# Robert Jordens <rjo@gmx.de>
#
# This software may be used and distributed according to the terms
# of the GNU General Public License, incorporated herein by reference.
#
# $Id$
#

DEB_PYTHON_SYSTEM=pysupport

#"cdbs makefile.mk"
include /usr/share/cdbs/1/class/makefile.mk
DEB_UPSTREAM_VERSION := $(shell echo $(DEB_VERSION) | sed 's/-[^-]*$$//')
DEB_UPSTREAM_TARBALL_VERSION := $(shell echo $(DEB_UPSTREAM_VERSION) | sed 's/+[^+]*$$//')
DEB_TAR_SRCDIR := linux-$(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_TARBALL_VERSION)
# tarball has to be first because of #205396
#"cdbs tarball.mk"
include /usr/share/cdbs/1/rules/tarball.mk
#"cdbs patchsys-quilt.mk"
include /usr/share/cdbs/1/rules/patchsys-quilt.mk
#"cdbs autotools.mk"
include /usr/share/cdbs/1/class/autotools.mk
#"cdbs debhelper.mk"
include /usr/share/cdbs/1/rules/debhelper.mk

#"cdbs python-distutils.mk"
include /usr/share/cdbs/1/class/python-distutils.mk

DEB_PYTHON_MODULE_PACKAGE := python-gpib
DEB_PYTHON_SETUP_CMD := language/python/setup.py
DEB_PYTHON_INSTALL_ARGS_ALL = --root=$(CURDIR)/debian/python-gpib
phpapiver=$(shell php-config5 --phpapi)


#include /usr/share/cdbs/1/class/perlmodule.mk
#DEB_MAKEMAKER_INVOKE = /usr/bin/perl language/perl/Makefile.PL INSTALLDIRS=vendor

DEB_CONFIGURE_EXTRA_FLAGS := --enable-pcmcia --enable-static

DEB_DH_MAKESHLIBS_ARGS := -V
DEB_SHLIBDEPS_INCLUDE := lib/.libs

# Work around #590157
ORIG_DEB_DESTDIR := $(DEB_DESTDIR)

PACKAGE = gpib-modules
MA_DIR ?= /usr/share/modass
-include $(MA_DIR)/include/generic.make
-include $(MA_DIR)/include/common-rules.make
# save from environment
KPKG_DESTDIR := $(DEB_DESTDIR)
DEB_DESTDIR := $(ORIG_DEB_DESTDIR)

# after the directories are created but before dh_compress is run.
common-binary-post-install-arch::
	dh_buildinfo

# make include = gpib to build perl and python bindings
post-patches::
	cd build-tree/*-* && ./bootstrap
#	-rm -f gpib && ln -s include gpib

build/libgpib-perl::
	(cd $(DEB_BUILDDIR)/language/perl; \
	perl Makefile.PL INSTALLDIRS=vendor;\
	$(MAKE) OPTIMIZE="-O2 -g -Wall" LD_RUN_PATH=)

install/libgpib-perl::
	(cd $(DEB_BUILDDIR)/language/perl; \
	$(MAKE) install DESTDIR=$(CURDIR)/debian/libgpib-perl )

binary-predeb/php5-gpib::
	echo "php:Depends=phpapi-$(phpapiver)" >> debian/php5-gpib.substvars
# Don't use -f when deleting these files so that errors requiring a path change
# will be obvious (CUS)
	rm -f $(CURDIR)/debian/php5-gpib/usr/lib/php5/$(phpapiver)/gpib_php.a
	rm -f $(CURDIR)/debian/php5-gpib/usr/lib/php5/$(phpapiver)/gpib_php.la
	rm -f $(CURDIR)/debian/php5-gpib/usr/lib/php5/$(phpapiver)/gpib_php.so

.PHONY: clean/libgpib-perl
clean/libgpib-perl:
	-(cd $(DEB_BUILDDIR)/language/perl; \
	$(MAKE) distclean )

clean:: clean/libgpib-perl

build/gpib-modules-source::
	mkdir -p debian/gpib-modules-source/usr/src/modules
	cp -a build-tree/linux-gpib-*/drivers/gpib \
		debian/gpib-modules-source/usr/src/modules/
	mkdir -p debian/gpib-modules-source/usr/src/modules/gpib/debian
	cd debian ; cp changelog control common-description \
		compat *.modules.in rules copyright \
		gpib-modules-source/usr/src/modules/gpib/debian
	cp build-tree/linux-gpib-*/config.h \
		debian/gpib-modules-source/usr/src/modules/gpib/include
	ln -s . debian/gpib-modules-source/usr/src/modules/gpib/include/gpib
	cd debian/gpib-modules-source/usr/src/ ; \
		tar -c modules | bzip2 -9 > gpib.tar.bz2 && rm -rf modules
	mkdir -p debian/gpib-modules-source/usr/share/modass/overrides
	ln -s ../packages/default.sh \
		debian/gpib-modules-source/usr/share/modass/overrides/gpib-modules-source

kdist_clean:
	dh_clean
	find $(CURDIR) \( -name '*.[oas]' -o -name '*.ko' \
		-o -name core -o -name '.*.cmd' \) -type f -exec $(RM) {} \;

kdist_config: prep-deb-files
kdist_configure: kdist_config

KO := k

binary-modules: prep-deb-files
	dh_testdir
	dh_testroot
	dh_clean -k
	$(MAKE) -C $(KSRC) CC="$(CC) -I$(CURDIR) -I$(CURDIR)/include" \
		SUBDIRS="$(CURDIR)" \
                MODVERDIR="$(CURDIR)/.tmp-versions" modules
	install -d $(CURDIR)/debian/$(PKGNAME)/lib/modules/$(KVERS)/gpib
	install -m644 */*.$(KO)o \
		$(CURDIR)/debian/$(PKGNAME)/lib/modules/$(KVERS)/gpib/
	dh_installdocs
	dh_installchangelogs
	dh_compress
	dh_fixperms
	dh_installdeb
	cat debian/common-description >> debian/$(PKGNAME).substvars
	dh_gencontrol -- -v$(VERSION)
	dh_md5sums
	dh_builddeb --destdir=$(KPKG_DESTDIR)

$(patsubst %,binary-predeb/%,$(DEB_PACKAGES)) :: binary-predeb/%: binary-fixup/%
	cat debian/common-description >> debian/$(cdbs_curpkg).substvars

.PHONY: update-regen-autotools-patch
update-regen-autotools-patch:
	-fakeroot debian/rules clean apply-patches
	(cd build-tree/*-*; \
	quilt push -a -f; \
	aclocal --force; \
	autoconf --force; \
	automake -a -c -f; \
	libtoolize --force --copy; \
	quilt refresh; )
	

.PHONY: update-create-orig-tarball
update-create-orig-tarball:
	mkdir -p tmp-orig-tree/$(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_VERSION).orig
	cp *.tar.* tmp-orig-tree/$(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_VERSION).orig
	tar -C tmp-orig-tree -czvf \
		../$(DEB_SOURCE_PACKAGE)_$(DEB_UPSTREAM_VERSION).orig.tar.gz \
		$(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_VERSION).orig

.PHONY: update-clean
update-clean:
	rm -rf  tmp-cvs-tarball \
		tmp-orig-tree

clean:: update-clean

