#!/usr/bin/make -f
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
# This has to be exported to make some magic below work.
export DH_OPTIONS

DEB_BUILD_MAINT_OPTIONS=hardening=+all,-pie
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

COMMA = ,
ifneq (,$(filter parallel=%,$(subst $(COMMA), ,$(DEB_BUILD_OPTIONS))))
  NJOBS := -j $(subst parallel=,,$(filter parallel=%,$(subst $(COMMA), ,$(DEB_BUILD_OPTIONS))))
endif

# useful variables
v = 2.4

dtmp = $(shell pwd)/debian/tmp

ifneq (,$(wildcard /usr/lib/$(DEB_HOST_MULTIARCH)/tcl8.6/tclConfig.sh))
  tcl_prefix = /usr/lib/$(DEB_HOST_MULTIARCH)/tcl8.6
else
  tcl_prefix = /usr/lib/tcl8.6
endif
ifneq (,$(wildcard /usr/lib/$(DEB_HOST_MULTIARCH)/tk8.6/tkConfig.sh))
  tk_prefix = /usr/lib/$(DEB_HOST_MULTIARCH)/tk8.6
else
  tk_prefix = /usr/lib/tk8.6
endif

# CONFIGURE = ./configure --prefix=/usr/ --mandir=/usr/share/man \
#	--with-cflags="-O2 -g -D_REENTRANT "
CFLAGS+=-D_REENTRANT
CONFIGURE = ./configure --prefix=/usr/ --mandir=/usr/share/man

# Now, the build targets...

build:  build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp

build-stamp: build-8.6-stamp
	touch build-stamp

#
# Now build for the best tcl/tk version (8.6)
#
build-8.6-stamp:
	dh_testdir
	dh_autotools-dev_updateconfig

	$(CONFIGURE) \
		--with-tk=$(tk_prefix) \
		--with-tcl=$(tcl_prefix)

	( cd src/shared ; \
	  sed -e 's:$$(version)$$(SHLIB_SUFFIX):.2.4.so.8.6:' Makefile > M2 ; \
	  mv M2 Makefile )

	$(MAKE) $(NJOBS) all

	touch build-8.6-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build*-stamp
	rm -f configure.old

	rm -f build config.cache
	[ ! -f Makefile ] || $(MAKE) clean
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f src/shared/*.o
	rm -f src/bltConfig.h src/bltHash.h

	rm -f `find . -name "Makefile"`

	rm -f debian/shlibs.local
	rm -rf tmplib
	rm -rf debian/tcl8.[0-6] debian/tk8.[0-6]

	dh_autotools-dev_restoreconfig
	dh_clean

install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	#dh_clean -k
	dh_prep

# Ok, do the basic install
	-mkdir -p $(dtmp)
	-mkdir -p $(dtmp)/usr/share/man/man3
	-mkdir -p $(dtmp)/usr/share/man/mann
	$(MAKE) INSTALL_ROOT=$(dtmp) install

ifeq (0,1)
# Add the extra libs we built, and rename the latest for consistency
	cp tmplib/* $(dtmp)/usr/lib
	(cd $(dtmp)/usr/lib/; mv libBLT24.a     libBLT.$(v).8.6.a)
	(cd $(dtmp)/usr/lib/; mv libBLTlite24.a libBLTlite.$(v).8.6.a)
endif

# Now fix up the man pages
	mv $(dtmp)/usr/share/man/mann/BLT.n \
	   $(dtmp)/usr/share/man/mann/intro.n
	(cd $(dtmp)/usr/share/man/mann ; for i in *.n ; do \
	   mv $$i ../man3/blt::`basename $$i .n`.3 ; \
	done)
	rmdir $(dtmp)/usr/share/man/mann

# remove some misplaced documentation
	(cd $(dtmp)/usr/lib/blt2.4; rm README PROBLEMS NEWS)

# Now we need to move the demos and examples over to /usr/share
	mkdir -p $(dtmp)/usr/share/blt2.4
	mv $(dtmp)/usr/lib/blt2.4/demos $(dtmp)/usr/share/blt2.4
	chmod a-x $(dtmp)/usr/share/blt2.4/demos/*.txt

	mkdir -p $(dtmp)/usr/share/blt2.4/examples
	cp examples/* $(dtmp)/usr/share/blt2.4/examples

# patch the demos and examples to use wish
	(cd $(dtmp)/usr/share/blt2.4; \
	   for i in demos/*.tcl examples/*.tcl ; do \
		sed -e '1c#!/usr/bin/wish' $$i >$$i.new ; \
		mv $$i.new $$i ; \
		chmod 755 $$i; \
	   done)

	chmod 644 $(dtmp)/usr/share/blt2.4/demos/images/*
	chmod 644 $(dtmp)/usr/share/blt2.4/demos/scripts/*.tcl
	chmod 755 $(dtmp)/usr/share/blt2.4/demos/scripts/page.tcl

# Build architecture-independent files here.
# Pass -i to all debhelper commands in this target to reduce clutter.
binary-indep: DH_OPTIONS=-i
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installdirs
	dh_installdocs
#	dh_installexamples
	dh_installchangelogs
#	dh_installmenu
	dh_movefiles
	dh_compress
	dh_fixperms
	dh_link
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture-dependent files here.
# Pass -a to all debhelper commands in this target to reduce clutter.
binary-arch: DH_OPTIONS=-a
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdirs
	dh_installdocs

# some minor cleanup needed for the html docs
	rm debian/blt/usr/share/doc/blt/html/Makefile.vc
	cp debian/blt-index.html debian/blt/usr/share/doc/blt/html

#	dh_installexamples
	dh_installchangelogs
#	dh_installmenu

	dh_movefiles

	dh_strip
	dh_link
	dh_compress
	dh_fixperms

	dh_makeshlibs -V 'blt (>= 2.4z-4.1)'
#	The excludes are probably wrong but necessary to make lintian happy
#	dh_makeshlibs -V 'blt (>= 2.4z)' \
#		--exclude="8.0" --exclude="8.3"

	dh_installdeb

# Now, it's time to  call upon my magic ugly little hack script, which
# makes a shlibs.local file with which to fool the shlibs system,
# and get the special dependencies we need.

	#chmod +x debian/hackshlibs # make sure we can run it
	#DEB_HOST_ARCH=$(DEB_HOST_ARCH) debian/hackshlibs
	dh_shlibdeps

	dh_gencontrol
	dh_md5sums
	dh_builddeb


binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install
