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

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

PYDEF=$(shell pyversions -d)
PYVERS=$(shell pyversions -r)

include /usr/share/python/python.mk

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.

	touch configure-stamp


build: build-stamp fix-doc

build-stamp: configure-stamp 
	dh_testdir
	set -e

	for python in $(PYVERS); do \
		python=$$(echo $$python | sed "s/$(PYDEF)/python/"); \
		$$python setup.py build; \
	done
	touch build-stamp

clean:
	dh_testroot
	dh_testdir
	for python in $(PYVERS); do \
		python=$$(echo $$python | sed "s/$(PYDEF)/python/"); \
		$$python setup.py clean; \
	done
	rm -f build-stamp configure-stamp
	dh_clean

	rm -rf build/ BitTorrent/*.pyc *.pyc
	rm -rf www-doc/bittorrent_logo.png

	dh_clean

update-doc:
	if [ ! -d www-doc ] ; then mkdir www-doc ; fi
	cd www-doc && /usr/bin/wget --proxy=off -L -nd -A html,png,gif -k --no-parent -q -m -r -l 1 http://bitconjurer.org/BitTorrent/documentation.html 
	find www-doc -type f -a \( -name "*.gif" -o -name "*.png" \) | \
	while read file ; do cat www-doc/`basename $$file` | uuencode `basename $$file` > www-doc/`basename $$file`.uu ; done
	-rm -f www-doc/robots.txt www-doc/*.gif www-doc/*.png

clean-doc:
	-rm -f www-doc/*

fix-doc:
	# We do this in order to prevent dpkg-source from breaking
	cd www-doc/ && for i in *.uu; do uudecode $$i; done

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	set -e; \
	for python in $(PYVERS); do \
		pydir=$$python \
		python=$$(echo $$python | sed "s/$(PYDEF)/python/"); \
		$$python setup.py install --root=debian/tmp $(py_setup_install_args); \
		cp debian/tmp/usr/bin/btmakemetafile.py \
		   debian/tmp/usr/bin/btcompletedir.py \
		   debian/tmp/usr/lib/$$pydir/$(call py_sitename_sh, $$python)/BitTorrent; \
	done

	: # Replace all '#!' calls to python with /usr/bin/python
	: # and make them executable
	for i in `find debian -mindepth 3 -type f`; do \
	  sed '1s,#!.*python[^ ]*\(.*\),#! /usr/bin/python\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

	# Install the icon
	cp bittorrent.ico debian/tmp

# Build architecture-independent files here.
binary-arch: build install
# We have nothing to do by default.


# Build architecture-dependent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installexamples
	dh_installmenu
	dh_installmime
	dh_installman
	# .bittorrent for alternatives
	( cd debian/tmp/usr/bin && \
	  for i in btdownloadgui btdownloadheadless bttrack btmakemetafile btlaunchmany btdownloadcurses btlaunchmanycurses btreannounce btrename btshowmetainfo btcompletedir btcompletedirgui; \
	  do mv $$i.py $$i.bittorrent; done )
	dh_installchangelogs
	dh_installinit
	dh_install --sourcedir=debian/tmp
	dh_link
	dh_compress
	dh_fixperms
	dh_pysupport
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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