#!/usr/bin/make -f

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

# 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
# Package maintainers to append LDFLAGS.
#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildflags.mk
DIR = $(shell pwd)/debian/tcl-parse-args

TCLVERSIONS = 8.6 9.0

%:
	dh $@

override_dh_auto_configure:
	for v in $(TCLVERSIONS) ; do \
		rm -f $(CURDIR)/tcl.pc ; \
		ln -s /usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/tcl$$v.pc $(CURDIR)/tcl.pc ; \
		dh_auto_configure -B$(CURDIR)/$$v -- \
			--pkg-config-path=$(CURDIR) ; \
	done

override_dh_auto_install:
	for v in $(TCLVERSIONS) ; do \
	    dh_auto_install -B$(CURDIR)/$$v ; \
	done
	#
	# Remove unnecessary headers and the subs library
	rm $(DIR)/usr/lib/$(DEB_HOST_MULTIARCH)/parse_args-*/*.h
	rm $(DIR)/usr/lib/$(DEB_HOST_MULTIARCH)/parse_args-*/*.a
	rm $(DIR)/usr/lib/$(DEB_HOST_MULTIARCH)/parse_args-*/*.def
	rm -r $(DIR)/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/
	#
	# Fix the package directory
	mv $(DIR)/usr/lib/$(DEB_HOST_MULTIARCH) $(DIR)/usr/lib/tcltk/
	#
	# Move the doc
	mv $(DIR)/usr/share/doc/parse_args.html $(DIR)/usr/share/doc/tcl-parse-args/
	#
	# Fix the manpage
	cat $(DIR)/usr/share/man/parse_args.n | sed -e 's/^\.TH \("parse_args"\)/.TH \1 "3tcl"/g' \
						    -e 's/\([A-Z][0-9A-Za-z_]*\)(3)/\1(3tcl)/g' \
						    -e 's/(n)/(3tcl)/g' \
						    -e "s/\\N'244'/\\[^o]/g" > $(DIR)/usr/share/man/man3/parse_args.3tcl
	rm $(DIR)/usr/share/man/parse_args.n

