#!/usr/bin/make -f

#export DH_VERBOSE=1

pkgdir=$(shell pwd)/debian/tmp
libdir=$(pkgdir)/usr/lib

#CFLAGS = -Wall -g

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk

LDFLAGS_DEFAULT = `dpkg-buildflags --get LDFLAGS`
export LDFLAGS_DEFAULT
LDFLAGS_OPTIMIZE = `dpkg-buildflags --get LDFLAGS`
export LDFLAGS_OPTIMIZE

clean:
	dh_testdir
	dh_testroot
	rm -f configure-stamp build-stamp
	#
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -rf libtiff/tcl/port
	rm -rf libtiff/tcl/man
	rm -rf libtiff/tcl/libtiff
	rm -f libjpeg/jconfig.h
	rm -f libtiff/tcl/tools/Makefile
	find . -name "*.so" | xargs rm -f
	find . -name "*.a" | xargs rm -f
	find . -name config.status | xargs rm -f
	find . -name config.log | xargs rm -f
	dh_clean

configure: configure-stamp
configure-stamp:
	dh_testdir
	cp /usr/include/$(DEB_HOST_MULTIARCH)/jconfig.h libjpeg/
	#
	CFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)" ./configure \
		--host=$(DEB_HOST_GNU_TYPE) \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=/usr \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		--with-tcl=/usr/lib \
		--with-tk=/usr/lib \
		--with-tkinclude=/usr/include/tcl \
                --enable-threads
	#
	touch configure-stamp

build-arch: build-stamp
build-indep: build-stamp
build: build-stamp
build-stamp: configure
	dh_testdir
	#
	$(MAKE) TCL_INC_DIR=/usr/include/tcl/tcl-private/generic \
		TK_INC_DIR=/usr/include/tcl/tk-private/generic
	#
	touch build-stamp

install: build-stamp
	dh_testdir
	dh_testroot
	dh_prep
	#
	$(MAKE) install INSTALL_ROOT=$(pkgdir)
	#
	# Fix file permissions
	find $(libdir) -type f | xargs chmod a-x
	#
	# Fix pkgIndex.tcl to prevent loading package to Tk 8.3 or older
	sed -i -e'1i\
	# Tcl package index file\
	#\
	# Return if Tcl/Tk is old.\
	#\
	if {[package vcompare [info tclversion] 8.4] < 0} return\
	' $(libdir)/Img*/pkgIndex.tcl
	#
	# Move library into tcltk subdirectory
	install -m 755 -d $(libdir)/tcltk
	mv $(libdir)/Img* $(libdir)/tcltk
	#
	# Fix *Config.sh
	DIR=`ls -d $(libdir)/tcltk/Img*` ; \
	PKGDIR=`basename $$DIR` ; \
	for f in $(libdir)/*Config.sh ; do \
	  sed -i -e "s:/usr/lib/[0-9.a-z]*:/usr/lib/tcltk/$$PKGDIR:" $$f || exit 1; \
	done
	#
	# Cleanup manpages
	(cd $(pkgdir)/usr/share/man/mann && \
	  for f in *.n ; do \
	    sed -e'/^\.so man.macros/ d' \
		-e'/^\.BS/ d' \
		-e'/^\.BE/ d' \
		-e's/^\.TH \([^ ]\+\) n/.TH \1 3tk/' \
		-e's/\xc2\xad //' \
		$$f > `basename $$f .n`.3tk || exit 1; \
	  done)
	
	# Rename img to img-intro since it's refered as img-intro
	mv $(pkgdir)/usr/share/man/mann/img.3tk $(pkgdir)/usr/share/man/mann/img-intro.3tk

binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installdirs -i
	dh_install -i
	dh_installdocs -i
	dh_installexamples -i
	dh_installman -i
	dh_installchangelogs -i ChangeLog
	dh_lintian -i
	dh_link -i
	dh_strip -i
	dh_compress -i -X.tcl
	dh_fixperms -i
	chmod a-x debian/libtk-img-doc/usr/share/doc/libtk-img-doc/examples/msgs/*
	dh_makeshlibs -i
	dh_installdeb -i
	dh_shlibdeps -i
	tcltk-depends -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdirs -a
	dh_install -a
	dh_installdocs -a
	dh_installexamples -a
	dh_installman -a
	dh_installchangelogs -a ChangeLog
	dh_lintian -a
	dh_link -a
	dh_strip -a
	dh_compress -a -X.tcl
	dh_fixperms -a
	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a
	tcltk-depends -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch

get-orig-source:
	# Can't use version number from debian/changelog because
	# this target should work from any directory
	wget -O libtk-img_1.4.2.orig.tar.gz \
		http://prdownloads.sourceforge.net/tkimg/tkimg1.4.2.tar.gz

.PHONY: configure build build-arch build-indep clean binary-indep \
	binary-arch binary install get-orig-source
