#!/usr/bin/make -f

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

export pwd = $(shell pwd)
export DEB_CPPFLAGS_MAINT_APPEND = -I$(pwd)/vector -I$(pwd)/fitsy

# 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


%:
	dh $@


# dh_make generated override targets.
# This is an example for Cmake (see <https://bugs.debian.org/641051>).
#override_dh_auto_configure:
#	dh_auto_configure -- \
#	-DCMAKE_LIBRARY_PATH=$(DEB_HOST_MULTIARCH)

override_dh_auto_configure:
	dh_auto_configure -D vector
	dh_auto_configure -D fitsy
	dh_auto_configure -D tclfitsy
	dh_auto_configure -D tksao
	dh_auto_configure -D tclxmlrpc

override_dh_auto_build:
	dh_auto_build -D vector
	dh_auto_build -D fitsy
	dh_auto_build -D tclfitsy
	dh_auto_build -D tksao
	dh_auto_build -D tclxmlrpc -- SHLIB_LD="g++ $$LDFLAGS -shared"
	for i in $(shell find ds9 -name \*.tac) ; do \
	  f=$$(basename $$i | sed s/parser.tac//); \
          tclsh taccle/taccle.tcl -p $$f -d $$i ; \
	  sed '1 i\package provide DS9 1.0' -i ds9/parsers/$${f}parser.tcl ; \
        done
	for i in $(shell find ds9 -name \*.fcl) ; do \
	  f=$$(basename $$i | sed s/lex.fcl//); \
          tclsh fickle/fickle.tcl -P $$f $$i ; \
	  sed '1 i\package provide DS9 1.0' -i ds9/parsers/$${f}lex.tcl ; \
        done
	mv ds9/parsers/*.tcl ds9/library
	cd ds9/library; echo "pkg_mkIndex . *.tcl; exit" | tclsh
	chmod ugo+x ds9/ds9
	perl -w debian/generate-man > ds9/ds9.1

override_dh_auto_test:
ifeq ($(filter $(DEB_BUILD_OPTIONS),nocheck),)
	DS9_HOME=ds9 \
	TCLLIBPATH="tksao tclfitsy tclxmlrpc" \
	xvfb-run --server-args="-screen 0 1024x768x24" \
	./ds9/ds9 -quit
endif

override_dh_installchangelogs:
	dh_installchangelogs ds9/doc/release/r8.6.html