#!/usr/bin/make -f
# Sample debian.rules file - for GNU Hello (1.3).
# Copyright 1994,1995 by Ian Jackson.
# I hereby give you perpetual unlimited permission to copy,
# modify and relicense this file, provided that you do not remove
# my name from the file itself.  (I assert my moral right of
# paternity under the Copyright, Designs and Patents Act 1988.)
# This file may have to be extensively modified

# There used to be `source' and `diff' targets in this file, and many
# packages also had `changes' and `dist' targets.  These functions
# have been taken over by dpkg-source, dpkg-genchanges and
# dpkg-buildpackage in a package-independent way, and so these targets
# are obsolete.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

SHELL=/bin/bash

include /usr/share/dpkg/buildflags.mk
CFLAGS := -Wall $(shell dpkg-buildflags --get CFLAGS)
CPPFLAGS := $(shell dpkg-buildflags --get CPPFLAGS)
LDFLAGS := $(shell dpkg-buildflags --get LDFLAGS)

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

groff_version=`groff -v| grep "groff version"| sed -e 's/GNU groff version //'`
tk_version = 8.6
tix_version = 8.4.3
soname=libTix$(tix_version).so.1

p_run	= tix
p_dev	= $(p_run)-dev

d_run	= debian/$(p_run)
d_dev	= debian/$(p_dev)

build-indep:
build-arch: build
build: build-static build-shared

build-static: build-static-stamp
build-static-stamp:
	dh_testdir
	rm -rf build-static
	mkdir build-static
	cd build-static && ../configure \
	    CPPFLAGS="$(CPPFLAGS)" CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" \
	    --prefix=/usr \
	    --mandir=\$${prefix}/share/man \
	    --disable-shared \
	    --with-tclconfig=/usr/lib/$(DEB_HOST_MULTIARCH)/tcl$(tk_version) \
	    --with-tclinclude=/usr/include/tcl$(tk_version)/tcl-private \
	    --with-tkconfig=/usr/lib/$(DEB_HOST_MULTIARCH)/tk$(tk_version) \
	    --with-tkinclude=/usr/include/tcl$(tk_version)/tk-private

	$(MAKE) -C build-static

	touch build-static-stamp

build-shared: build-shared-stamp
build-shared-stamp:
	dh_testdir
	rm -rf build-shared
	mkdir build-shared
	cd build-shared && ../configure \
	    CPPFLAGS="$(CPPFLAGS)" CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" \
	    --prefix=/usr \
	    --mandir=\$${prefix}/share/man \
	    --enable-shared \
	    --with-tclconfig=/usr/lib/$(DEB_HOST_MULTIARCH)/tcl$(tk_version) \
	    --with-tclinclude=/usr/include/tcl$(tk_version)/tcl-private \
	    --with-tkconfig=/usr/lib/$(DEB_HOST_MULTIARCH)/tk$(tk_version) \
	    --with-tkinclude=/usr/include/tcl$(tk_version)/tk-private

	$(MAKE) -C build-shared \
		PKG_LIB_FILE=libTix$(tix_version).so.1 \
		LDFLAGS_DEFAULT="-Wl,-soname,$(soname),--export-dynamic"

	touch build-shared-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f *-stamp
	rm -rf build-*
	dh_clean

install:
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	dh_installdirs -p$(p_run) \
		usr/lib \
		usr/share/man/man1 \
		usr/share/doc/$(p_run)
	dh_installdirs -p$(p_dev) \
		usr/lib \
		usr/include \
		usr/share/man/man3 \
		usr/share/doc/$(p_run) \
		usr/share/tix$(tix_version)

	: # Install the package
	$(MAKE) -C build-static install \
		DESTDIR=$(shell pwd)/$(d_run) \
		pkglibdir=/usr/lib/tix$(tix_version) \

	$(MAKE) -C build-shared install \
		DESTDIR=$(shell pwd)/$(d_run) \
		pkglibdir=/usr/lib/tix$(tix_version) \
		PKG_LIB_FILE=libTix$(tix_version).so.1 \

	: # install missing tix.h
	cp $(CURDIR)/generic/tix.h $(d_dev)/usr/include

	: # Move things around

	mv $(d_run)/usr/lib/tix$(tix_version)/*.so* $(d_run)/usr/lib/
	mv $(d_run)/usr/lib/tix$(tix_version)/*.a $(d_dev)/usr/lib/
	ln -sf libTix$(tix_version).so.1 \
		$(d_dev)/usr/lib/libTix$(tix_version).so

	mkdir -p $(d_run)/usr/share/tcltk/tcl$(tk_version)
	mv $(d_run)/usr/lib/tix$(tix_version) \
		$(d_run)/usr/share/tcltk/tcl$(tk_version)/Tix$(tix_version)
	ln -sf ../share/tcltk/tcl$(tk_version)/Tix$(tix_version) $(d_run)/usr/lib/Tix$(tix_version)

	sed -i -e '/^package/s/\$$dir/\$$dir ../' \
		$(d_run)/usr/share/tcltk/tcl$(tk_version)/Tix$(tix_version)/pkgIndex.tcl
	sed -i -e 's/\$$dir/\/usr\/lib/' \
		$(d_run)/usr/share/tcltk/tcl$(tk_version)/Tix$(tix_version)/pkgIndex.tcl

	rm  -f $(d_run)/usr/share/tcltk/tcl$(tk_version)/Tix$(tix_version)/license.terms

	install -m755 tools/tixindex $(d_run)/usr/bin/tixindex

	# remove tixwish man pages (close #407212)
	# cp -p man/tixwish.1 $(d_run)/usr/share/man/man1/
	cp -p debian/tixindex.1 $(d_run)/usr/share/man/man1/

	#  sed -i -e 's/man.macros/tixman.macros/g' $(d_run)/usr/share/man/man1/tixwish.1
	sed -i -e 's/.TH TIXINDEX/.TH TIX/g' $(d_run)/usr/share/man/man1/tixindex.1

#	for i in man/*.n; do \
#	  cp -p $$i $(d_dev)/usr/share/man/man3/`basename $$i .n`.3; \
#	  sed -i -e 's/.so man.macros/.mso tix.tmac/g' $(d_dev)/usr/share/man/man3/`basename $$i .n`.3; \
#	  perl -pi -e "s|\.TH \w+ n|\.TH TIX 3|g" $(d_dev)/usr/share/man/man3/`basename $$i .n`.3; \
#	done

	for i in man/*.n; do \
	  cp -p $$i $(d_dev)/usr/share/man/man3/`basename $$i .n`.3; \
	  perl -pi -e "s|\.TH \w+ n|\.TH TIX 3|g" $(d_dev)/usr/share/man/man3/`basename $$i .n`.3; \
	done

	rm -f $(d_run)/usr/lib/tix$(tix_version)/README.txt
	rm -f $(d_run)/usr/lib/tix$(tix_version)/license.terms
	rm -rf $(d_run)/usr/lib/tix$(tix_version)/html

	chmod ugo-x $(d_run)/usr/share/tcltk/tcl$(tk_version)/Tix$(tix_version)/pref/WmDefault.tcl

#	sed 's,-L[^ ]*,,' $(d_run)/usr/lib/tixConfig.sh \
#		> $(d_dev)/usr/share/tix$(tix_version)/tixConfig.sh
#	rm -f $(d_run)/usr/lib/tixConfig.sh

	: # Replace all '#!' calls to tclsh with /usr/bin/tclsh
	: # and make them executable
	for i in `find debian -mindepth 3 -type f`; do \
	  sed '1s,#!.*tclsh[^ ]*\(.*\),#! /usr/bin/tclsh\1,' \
		$$i > $$i.temp; \
	  if cmp --quiet $$i $$i.temp; then \
	    rm -f $$i.temp; \
	  else \
	    mv -f $$i.temp $$i; \
	    chmod 755 $$i; \
	    echo "fixed interpreter: $$i"; \
	  fi; \
	done

	: # remove empty directories, when all components are in place
	for d in `find debian -depth -type d -empty 2> /dev/null`; do \
	    while rmdir $$d 2> /dev/null; do d=`dirname $$d`; done; \
	done


binary-indep: build
# There are no architecture-independent files to be uploaded
# generated by this package.  If there were any they would be
# made here.

binary-arch: build install
	dh_testdir
	dh_testroot

	dh_installdocs -p$(p_run) \
		ABOUT.html README.txt \
		docs/pdf/TixUser.pdf \
		docs/FAQ.* docs/Files.txt \

	mkdir -p $(d_dev)/usr/share/doc/$(p_run)/
	cp -p docs/pdf/TixBook.pdf $(d_dev)/usr/share/doc/$(p_run)/
	ln -sf $(p_run) $(d_dev)/usr/share/doc/$(p_dev)
	mkdir -p $(d_dev)/usr/share/groff/$(groff_version)/tmac
	cp -p man/man.macros $(d_dev)/usr/share/groff/$(groff_version)/tmac/tix.tmac

	dh_installchangelogs -p$(p_run) ChangeLog

	dh_installexamples -p$(p_dev) demos/*

	dh_strip -a
	dh_link -a
	dh_compress -a -Xexamples -X.pdf -Xmacros
	dh_fixperms -a
	dh_makeshlibs -p$(p_run) -V 'tix (>= 8.4)'
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a


binary:	binary-indep binary-arch

.PHONY: binary binary-arch binary-indep clean

#Local variables:
#mode: makefile
#End:
