#!/usr/bin/make -f
# -*- makefile -*-

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

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

CONFIGOPTS=

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CONFIGOPTS="--disable-optimization"
endif

# option 2, assuming the library is created as src/.libs/libfoo.so.2.0.5 or so
version=`ls .libs/libcwd.so.* | \
    awk '{if (match($$0,/[0-9]+\.[0-9]+\.[0-9]+$$/)) print substr($$0,RSTART)}'`
major=`ls .libs/libcwd.so.* | \
    awk '{if (match($$0,/\.so\.[0-9]+$$/)) print substr($$0,RSTART+4)}'`

config.status: configure
	dh_testdir
	dh_autotools-dev_updateconfig
	# Add here commands to configure the package.
	./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr $(CONFIGOPTS)


build: build-stamp
build-stamp:  config.status
	dh_testdir

	# Add here commands to compile the package.
	$(MAKE)

	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp 

	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) distclean

	dh_autotools-dev_restoreconfig
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs

	# Add here commands to install the libcwd package into debian/libcwd
	$(MAKE) DESTDIR="$(CURDIR)/debian/libcwd" install
	mkdir -p $(CURDIR)/debian/libcwd/usr/share/lintian/overrides
	echo "libcwd: package-name-doesnt-match-sonames" > $(CURDIR)/debian/libcwd/usr/share/lintian/overrides/libcwd
	echo "libcwd: non-dev-pkg-with-shlib-symlink" >> $(CURDIR)/debian/libcwd/usr/share/lintian/overrides/libcwd
	echo "libcwd: no-symbols-control-file" >> $(CURDIR)/debian/libcwd/usr/share/lintian/overrides/libcwd
	# Add here commands to install the libcwd-doc package into debian/libcwd-doc
	$(MAKE) DESTDIR="$(CURDIR)/debian/libcwd-doc" DOCDESTDIR="$(CURDIR)/debian/libcwd-doc/usr/share/doc/libcwd-doc" install-doc
	rm -f $(CURDIR)/debian/libcwd-doc/usr/share/doc/libcwd-doc/example-project/.files
	find $(CURDIR)/debian/libcwd-doc/usr/share/doc/libcwd-doc/documentation -maxdepth 1 -type f ! -name index.html | xargs rm -f
	find $(CURDIR)/debian/libcwd-doc/usr/share/doc/libcwd-doc/documentation/styles -type f ! -name '*.css' | xargs rm -f
	rm -rf $(CURDIR)/debian/libcwd-doc/usr/share/doc/libcwd-doc/documentation/doxygen-examples
	rm -rf $(CURDIR)/debian/libcwd-doc/usr/share/doc/libcwd-doc/documentation/www
	find $(CURDIR)/debian/libcwd-doc/usr/share/doc/libcwd-doc/documentation/tutorial -maxdepth 1 \
	    -type f -name '*.m4' -o -name '*.in' -o -name '*.awk' | xargs rm
	find $(CURDIR)/debian/libcwd-doc/usr/share/doc/libcwd-doc/documentation/reference-manual -name '*.map' -o -name '*.md5' | xargs rm -f

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	dh_link
	dh_strip
	dh_compress -X.js -X.map -X.m4 -X.in -X.config -X.dox -XINSTALL -XREADME
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install 
