#!/usr/bin/make -f
#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 -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_gencontrol:
	tcltk-depends
	dh_gencontrol

override_dh_auto_clean:
	$(MAKE) clean
	rm -rf run-tests tclsoldout.3tcl

.c.o:
	. /usr/lib/tclConfig.sh; \
	${CC} ${CFLAGS} -I/usr/include/tcl$${TCL_VERSION} -c ${SRC} -o ${OBJ}

libtclsoldout.so: ${OBJ}
	. /usr/lib/tclConfig.sh; \
	${CC} ${LDFLAGS} -ltcl$${TCL_VERSION} ${OBJ} -o libtclsoldout.so -lsoldout

override_dh_auto_build: libtclsoldout.so
	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}
