#!/usr/bin/make -f
# Sample debian/rules that uses debhelper. 
# GNU copyright 1997 by Joey Hess.
#
# This version is for a hypothetical package that builds an
# architecture-dependant package, as well as an architecture-independent
# package.

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

# This is the debhelper compatibility version to use.
export DH_COMPAT=3

VERSION     = $(shell head -1 debian/changelog | sed 's/ .*//;s/tclx//')
SRC_VERSION = $(shell head -1 debian/changelog | sed 's/.*(//;s/).*//;s/-.*//')
PKG_VERSION = $(shell head -1 debian/changelog | sed 's/.*(//;s/).*//')
TCL_VERSION = $(VERSION)
TCL_CONFIG  = /usr/lib/tcl$(TCL_VERSION)
TK_CONFIG   = /usr/lib/tk$(TCL_VERSION)
PRIORITY    = $(shell echo $(PKG_VERSION) | sed 's/-.*//g;s/\.//g')

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -g
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

configure: configure-stamp
configure-stamp:
	dh_testdir

	# Add here commands to configure the package.
	cd unix && ./configure --prefix=/usr --enable-shared \
	    --with-tclconfig=$(TCL_CONFIG) --with-tkconfig=$(TK_CONFIG)

	# Fix versions in installation scripts
	for f in debian/dirs debian/*.*{inst,rm,menu}; do \
	    perl -pi -e 's/8\.[0-9]/$(VERSION)/g' $$f; \
	    perl -pi -e 's/^PRIORITY=.*/PRIORITY=$(PRIORITY)/' $$f; \
	done

	touch configure-stamp

build-arch: configure-stamp  build-arch-stamp
build-arch-stamp:
	dh_testdir

	# Add here command to compile/build the package.
	cd unix && $(MAKE) CFLAGS="-g -O2 -D_REENTRANT"

	touch build-arch-stamp

build-indep:  configure-stamp build-indep-stamp
build-indep-stamp:
	dh_testdir

	# Add here command to compile/build the arch indep package.
	# It's ok not to do anything here, if you don't need to build
	#  anything for this package.

	touch build-indep-stamp

build: build-arch build-indep

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	# Add here commands to clean up after the build process.
	-cd unix && $(MAKE) clean
	-cd unix && $(MAKE) distclean
	-rm -rf tcl/help

	dh_clean
	-rm -f build-*-stamp

install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Build TclX help pages from manpages of the installed Tcl/Tk packages
	sh debian/copymanpages $(TCL_VERSION)

	# Add here commands to install the package into debian/tclx$(VERSION).
	#$(MAKE) install DESTDIR=$(CURDIR)/debian/tclx$(VERSION)
	cd unix \
	&& make INSTALL_ROOT=`pwd`/../debian/tclx$(VERSION) \
		TCLX_INST_MAN=/usr/share/man \
		TCL_MAN_DIR=`pwd`/../debian/tmp/tcl-man \
		TK_MAN_DIR=`pwd`/../debian/tmp/tk-man \
		install
	-rm -rf debian/tmp

	# Fix up the binaries.
	mv debian/tclx$(VERSION)/usr/bin/tcl \
	   debian/tclx$(VERSION)/usr/bin/tcl$(VERSION)
	mv debian/tclx$(VERSION)/usr/bin/wishx \
	   debian/tclx$(VERSION)/usr/bin/wishx$(VERSION)
	mv debian/tclx$(VERSION)/usr/bin/tclhelp \
	   debian/tclx$(VERSION)/usr/bin/tclhelp$(VERSION)

	# Fix up the libraries.
	cd debian/tclx$(VERSION)/usr/lib/ \
	   && ln -sf libtclx$(VERSION).so.1 libtclx$(VERSION).so \
	   && ln -sf libtkx$(VERSION).so.1 libtkx$(VERSION).so

	# Fix up the developement files.
	mv debian/tclx$(VERSION)/usr/lib/tkxConfig.sh \
	   debian/tclx$(VERSION)/usr/lib/tkX$(VERSION)/tkxConfig.sh
	mv debian/tclx$(VERSION)/usr/include/tclExtend.h \
	   debian/tclx$(VERSION)/usr/include/tclX$(VERSION)/tclExtend.h
	mv debian/tclx$(VERSION)/usr/lib/tclxConfig.sh \
	   debian/tclx$(VERSION)/usr/lib/tclX$(VERSION)/tclxConfig.sh

	# Fix up the manpages.
	mv -f debian/tclx$(VERSION)/usr/share/man/mann/* \
	   debian/tclx$(VERSION)/usr/share/man/man3/
	cd debian/tclx$(VERSION)/usr/share/man/man3; \
	for f in *.[3n]; do \
	    mv -f $$f `echo $$f | sed 's/\.[3n]/\.3tclx/'`; \
	done
	sh debian/fix-hardlinks -verbose
	ln -s TclX.3tclx debian/tclx$(VERSION)/usr/share/man/man3/tclx.3tclx

	# Move files to packages directories
	dh_movefiles --sourcedir=./debian/tclx$(VERSION)

	# Remove empty directories
	rmdir `find debian -type d | sort -r` 2>/dev/null || true

# Build architecture-independent files here.
# Pass -i to all debhelper commands in this target to reduce clutter.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	# dh_installdebconf -i
	dh_installdocs -i
	# dh_installexamples -i
	# dh_installmenu -i
	# dh_installlogrotate -i
	# dh_installemacsen -i
	# dh_installpam -i
	# dh_installmime -i
	# dh_installinit -i
	# dh_installcron -i
	# dh_installman -i
	# dh_installinfo -i
	# dh_undocumented -i
	dh_installchangelogs ChangeLog -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	# dh_perl -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	# dh_installdebconf -a
	dh_installdocs -a
	# dh_installexamples -a
	dh_installmenu -a
	# dh_installlogrotate -a
	# dh_installemacsen -a
	# dh_installpam -a
	# dh_installmime -a
	# dh_installinit -a
	# dh_installcron -a
	dh_installman -ptclx$(VERSION) debian/tcl8.3.1
	dh_installman -ptkx$(VERSION) debian/wishx8.3.1 debian/tclhelp8.3.1
	# dh_installinfo -a
	dh_installchangelogs ChangeLog -a
	dh_strip -a
	dh_link -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a -V
	dh_installdeb -a
	# dh_perl -a
	dh_shlibdeps -a \
	    -l debian/tclx$(VERSION)/usr/lib:debian/tkx$(VERSION)/usr/lib
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

lintian:
	dh_testdir
	cd .. && lintian -i tclx$(VERSION)_$(PKG_VERSION)_*.changes

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