#!/usr/bin/make -f
# debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

version = 2.7
cvsdate = 20130123

destdir = `pwd`/debian/tkcon

build-arch:
# Nothing here

build-indep: build-stamp
build-stamp:
	touch build-stamp

build: build-indep

clean:
	dh_testdir
	dh_testroot
	dh_clean build-stamp \
		 debian/*.1 \
		 debian/*.3tk \
		 debian/*.5

install: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Install the package into debian/tkcon
	install -m 755 -d $(destdir)/usr/share/tcltk/tkcon$(version)
	install -m 755 tkcon.tcl $(destdir)/usr/share/tcltk/tkcon$(version)/tkcon.tcl
	install -m 644 pkgIndex.tcl $(destdir)/usr/share/tcltk/tkcon$(version)/pkgIndex.tcl

	# Create symlinks (not using dh_link because of $(version))
	ln -s ../share/tcltk/tkcon$(version)/tkcon.tcl $(destdir)/usr/bin/tkcon

	# Install manpages
	for f in docs/*.man ; do \
	    mpexpand nroff $$f - | \
	    	sed -e '/^\.so man\.macros/ d' \
		    -e '/^\.B[SE]/ d' \
		    -e 's/^\.TH \(.\+\) n /.TH \1 3tk /' \
		    -e 's/(n)/(3tk)/g' >debian/`basename $$f .man` || exit 1 ; \
	done
	for f in debian/*.n ; do \
	    mv $$f debian/`basename $$f .n`.3tk ; \
	done

binary-arch:
# Nothing to do

# Build architecture-independent files here
binary-indep: install
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	# Moving old changelog from docs directory
	mv $(destdir)/usr/share/doc/tkcon/docs/changes.txt \
	   $(destdir)/usr/share/doc/tkcon/changelog.1999
	# Remove extra license file (it is referenced in HTML docs,
	# so, we'll link it to debian/copyright by dh_link)
	rm -f  $(destdir)/usr/share/doc/tkcon/docs/license.terms
	# Remove manual pages from docs directory
	rm -f  $(destdir)/usr/share/doc/tkcon/docs/*.man
	dh_installmenu
	dh_installman
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
	tcltk-depends
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep

get-orig-source: get-orig-source-cvs

get-orig-source-cvs:
	CURDIR=`pwd` && \
	TMPDIR=`mktemp -d /tmp/tkcon.XXXXXX` && \
	SCRDIR=`dirname $(MAKEFILE_LIST)` && \
	expect $$SCRDIR/checkout $$TMPDIR $(cvsdate) && \
	rm -rf $$TMPDIR/tkcon/extra/ && \
	find $$TMPDIR -name CVS -print | xargs rm -rf && \
	cd $$TMPDIR && \
	tar -zcf $$CURDIR/tkcon_$(version)~$(cvsdate).orig.tar.gz . && \
	rm -rf $$TMPDIR

get-orig-source-release:
	wget -O tkcon_$(version).orig.tar.gz \
		http://heanet.dl.sourceforge.net/sourceforge/tkcon/tkcon-$(version).tar.gz 

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

