#!/usr/bin/make -f
#export DH_VERBOSE = 1

# query for the architecture triplet we build for
export 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 -Wextra -pedantic -fPIC
# package maintainers to append LDFLAGS
export DEB_LDFLAGS_MAINT_APPEND = -shared -lsoldout 

CC ?= gcc
MAKE ?= make

SRC = tclsoldout.c
OBJ = ${SRC:.c=.o}

%:
	dh $@

override_dh_auto_clean:
	dh_auto_clean
	rm -f $(OBJ) libtclsoldout.so libtcl9tclsoldout.so
	rm -rf run-tests tclsoldout.3tcl

override_dh_gencontrol:
	tcltk-depends
	dh_gencontrol

override_dh_auto_build:
	echo "building for Tcl 8.6"; \
	$(CC) $(CFLAGS) -I/usr/include/tcl8.6 -c $(SRC) -o $(OBJ); \
	$(CC) -o libtclsoldout.so $(OBJ) -ltcl8.6 $(LDFLAGS) ; \
	rm -v tclsoldout.o; \
	echo "building for Tcl 9.0"; \
	$(CC) $(CFLAGS) -I/usr/include/tcl9.0 -c $(SRC) -o $(OBJ); \
	$(CC) -o libtcl9tclsoldout.so $(OBJ) -ltcl9.0 $(LDFLAGS) ; \
	echo "mangling names in tclsoldout.n"; \
	sed -e 's/TCLSOLDOUT n/tclsoldout 3tcl/g' tclsoldout.n > tclsoldout.3tcl

override_dh_auto_install:

override_dh_auto_test:
	@cp -a tests run-tests; \
	cd ./run-tests; \
	for f in *.test ; do \
		echo "mangling the shebang line in $$f" ; \
		sed -i -e 's|/usr/bin/env.*|/usr/bin/tclsh|' $$f; \
	done; \
	pwd; \
	sed -i -e 's|/usr/bin/env.*|/usr/bin/tclsh|' all.tcl ; \
	chmod 755 all.tcl ;\
	${MAKE}
