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

PYTHON_VERSION=$(shell /usr/bin/python -V 2>&1 | /usr/bin/perl -le 'print "$$1.$$2" if <> =~m/^Python\s+(\d+)\.(\d+)(\.\d+)*/')

build-stamp: bittorrent-downloader.bittornado.1 bittorrent-multi-downloader.bittornado.1 btcompletedir.bittornado.1 btcompletedirgui.bittornado.1 btcopyannounce.1 btdownloadcurses.bittornado.1 btdownloadgui.bittornado.1 btdownloadheadless.bittornado.1 btlaunchmany.bittornado.1 btlaunchmanycurses.bittornado.1 btmakemetafile.bittornado.1 btmaketorrentgui.1 btreannounce.bittornado.1 btrename.bittornado.1 btsethttpseeds.1 btshowmetainfo.bittornado.1 bttrack.bittornado.1
	dh_testdir
	touch build-stamp

%.1:	debian/%.sgml
	/usr/bin/docbook-to-man $< > debian/$@

clean:
	dh_testdir
	dh_testroot
	rm -rf build/ BitTornado/BT1/*.pyc BitTornado/*.pyc *.pyc
	rm -f debian/*.1
	rm -f build-stamp
	dh_clean

install: build-stamp


	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Add here commands to install the package into debian/bittornado.
	chmod +x ./setup.py
	./setup.py install --prefix=$(CURDIR)/debian/tmp/usr --install-lib=$(CURDIR)/debian/tmp/usr/share/python-support/bittornado

	install -d debian/tmp/usr/share/mimelnk/application

	install -D -m644 debian/bittornado.desktop debian/bittornado-gui/usr/share/applications/bittornado.desktop
	install -D -m644 debian/bittornado.xpm debian/tmp/usr/share/pixmaps/bittornado.xpm
	install -d debian/tmp/usr/share/bittorrent
	chmod 755 debian/tmp/usr/share/python-support/bittornado/BitTornado/launchmanycore.py
	cp debian/tmp/usr/bin/btmakemetafile.py debian/tmp/usr/share/python-support/bittornado/BitTornado
	cp debian/tmp/usr/bin/btcompletedir.py debian/tmp/usr/share/python-support/bittornado/BitTornado

	# Remove the generated .pyc files
	( cd debian/tmp/usr/share/python-support/bittornado/BitTornado && \
	  rm -f *.pyc  && \
	  cd BT1 && \
	  rm -f *.pyc )

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installexamples
	dh_installmenu
	dh_installmime
	dh_installman
	
	# Remove the .py from the end of each of these and add .bittornado
	# so that the alternatives system can work
	( cd debian/tmp/usr/bin && \
	  for i in btdownloadgui btdownloadheadless bttrack btmakemetafile \
	  	   btlaunchmany btcompletedir btcompletedirgui \
		   btdownloadcurses btlaunchmanycurses btreannounce btrename \
		   btshowmetainfo; \
	  do mv $$i.py $$i.bittornado || exit 1 ; done )
	
	# Remove the .py from the end of each of these, the following are not
	# included in bittorrent, so they are not renamed to having the
	# suffix of .bittornado
	( cd debian/tmp/usr/bin && \
	  for i in btcopyannounce btsethttpseeds btmaketorrentgui; \
	  do mv $$i.py $$i || exit 1; done )

	dh_installchangelogs 
	dh_install --sourcedir=debian/tmp
	dh_link
	dh_compress
	dh_fixperms
	dh_pysupport
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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