#!/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

v = 2.7.1

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_prep
	dh_installdirs

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

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

	# Install manpages
	for f in docs/*.man ; do \
	    mpexpand nroff $$f - | \
	    	sed -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
	# Remove links to an external sourceforge logo
	for f in $(destdir)/usr/share/doc/tkcon/index.html \
		 $(destdir)/usr/share/doc/tkcon/docs/*.html ; do \
	    sed -i -e '/<IMG src="http:/{N;s/<[^>]*>/<B>TkCon Homepage<\/B>/}' $$f || exit 1 ; \
	done
	# Comment out the external Tcl plugin invocation (it doesn't work anyway)
	sed -i -e 's/<OBJECT/<!-- <OBJECT/' -e 's/<\/OBJECT>/<\/OBJECT> -->/' \
		$(destdir)/usr/share/doc/tkcon/docs/plugin.html
	dh_installmenu
	dh_installman
	dh_install
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
	tcltk-depends
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep

get-orig-source:
	wget -O tkcon_$(v).orig.tar.gz https://github.com/wjoye/tkcon/archive/v$(v).tar.gz

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