#!/usr/bin/make -f

# See debhelper(7) (uncomment to enable).
# Output every command that modifies files on the build system.
#export DH_VERBOSE = 1

# query for the architecture triplet we build for
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# See FEATURE AREAS in dpkg-buildflags(1).
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# See ENVIRONMENT in dpkg-buildflags(1).
# Package maintainers to append CFLAGS.
export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic -I/usr/include/tcl
#BUILD_DIR = $(shell pwd)/debian/tcl-unix-sockets
BUILDROOT = $(CURDIR)/debian/build
LOWDOWN = $(shell which lowdown)
TCLVERSIONS = 8.6 9.0
PKGDIR = $(CURDIR)/debian/tcl-unix-sockets
DOCVER = 8.6

%:
	dh $@

override_dh_auto_clean:
	rm -rf $(BUILDROOT)
	dh_auto_clean


override_dh_gencontrol:
	tcltk-depends
	dh_gencontrol

override_dh_auto_configure:
	for v in $(TCLVERSIONS) ; do \
		BUILD_DIR="$(BUILDROOT)/tcl$$v" ; \
		mkdir -p "$$BUILD_DIR/doc" ; \
		dh_auto_configure -B"$$BUILD_DIR" -- \
			--with-tcl=/usr/lib/tcl$$v \
			--prefix=/usr \
			--mandir=/usr/share/man \
			--libdir=/usr/lib/tcltk/$(DEB_HOST_MULTIARCH) \
			--libexecdir=/usr/lib/tcltk/$(DEB_HOST_MULTIARCH) ; \
	done
	( cd "$(BUILDROOT)/tcl$(DOCVER)" && \
	  ./config.status --file=doc/unix_sockets.md:$(CURDIR)/doc/unix_sockets.md.in )

override_dh_auto_build:
	for v in $(TCLVERSIONS) ; do \
		BUILD_DIR="$(BUILDROOT)/tcl$$v" ;   \
		echo "Building tclcurl-ng for Tcl$$v in $$BUILD_DIR" ; \
		dh_auto_build --builddir=$$BUILD_DIR --; \
	done
	echo "Generate man page $(CURDIR)/doc/unix_sockets.3tcl" ; \
	cat $(BUILDROOT)/tcl$(DOCVER)/doc/unix_sockets.md | \
		sed -e 's/(n)/(3tcl)/g' \
		    -e 's/unix_sockets n/unix_sockets 3tcl/g' | \
		lowdown -s -T man > $(BUILDROOT)/tcl$(DOCVER)/doc/unix_sockets.3tcl
	
override_dh_auto_test:

override_dh_auto_install:
override_dh_auto_install:
	for v in $(TCLVERSIONS) ; do \
		dh_auto_install -B$(BUILDROOT)/tcl$$v --destdir=$(PKGDIR) -- install-binaries install-libraries ; \
	done
	install -D -m 0644 \
		$(BUILDROOT)/tcl$(DOCVER)/doc/unix_sockets.3tcl \
		$(PKGDIR)/usr/share/man/man3/unix_sockets.3tcl
