#!/usr/bin/make -f

version = 1.4.2
cvsdate = 20121213
reldate = 20080503
DIR = $(shell pwd)/debian/tcl-vfs

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/architecture.mk

ifeq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure -- \
			  --with-tcl=/usr/lib \
			  --enable-threads

override_dh_auto_install:
	# Install the package into debian/tcl-vfs.
	$(MAKE) install-binaries install-libraries DESTDIR=$(DIR)
	# Remove #! from non-executable files
	for f in fishvfs.tcl templatevfs.tcl ; do \
	    sed -i -e'1d' $(DIR)/usr/lib/$(DEB_HOST_MULTIARCH)/vfs*/template/$$f || exit 1 ; \
	done
	# Move the library into the multiarch dir
	install -m 755 -d $(DIR)/usr/lib/tcltk/$(DEB_HOST_MULTIARCH)
	mv $(DIR)/usr/lib/$(DEB_HOST_MULTIARCH)/vfs* $(DIR)/usr/lib/tcltk/$(DEB_HOST_MULTIARCH)
	# Remove empty directories
	find $(DIR)/ -empty -prune -exec rmdir \{\} \;
	# Cleanup manpages
	cd doc && \
	for f in *.n ; do \
	    sed -e'/^\.so man.macros/ d' \
	    	-e'/^\.B[SE]$$/ d' \
	    	-e's/^\.TH \([^ ]\+\) n/.TH \1 3tcl/' \
		$$f >$$(basename $$f .n).3tcl || exit 1; \
	done

override_dh_auto_test:
	# Disable tests

override_dh_shlibdeps:
	tcltk-depends
	dh_shlibdeps

get-orig-source: get-orig-source-cvs

get-orig-source-release:
	wget -O tclvfs_$(version)-$(reldate).orig.tar.gz \
		http://heanet.dl.sourceforge.net/sourceforge/tclvfs/tclvfs-$(reldate).tar.gz 

get-orig-source-cvs:
	CURDIR=`pwd` && \
	TMPDIR=`mktemp -d /tmp/tclvfs.XXXXXX` && \
	SCRDIR=`dirname $(MAKEFILE_LIST) | head -n1` && \
	expect $$SCRDIR/checkout $$TMPDIR "$(cvsdate)T23:59:59" && \
	find $$TMPDIR -name CVS -print | xargs rm -rf && \
	cd $$TMPDIR && \
	tar -zcf $$CURDIR/tclvfs_$(version)~$(cvsdate).orig.tar.gz . && \
	rm -rf $$TMPDIR

.PHONY: override_dh_auto_configure override_dh_auto_install override_dh_auto_test override_dh_shlibdeps
.PHONY: get-orig-source get-orig-source-release get-orig-source-cvs
