#!/usr/bin/make -f

versions=$(subst -dev,,\
           $(subst python,,\
             $(filter python%-dev,\
               $(shell sed -n '/^Build-Depends/s/,//gp' debian/control))))

DOC=usr/share/doc

DEFAULT=2.3
DEFAULTPACKAGE=python$(DEFAULT)-twisted

indeppackages=python-twisted python-twisted-conch twisted-doc \
         twisted-quotes twisted-doc-api\
         $(addsuffix -twisted,$(addprefix python,$(versions))) \
         $(addsuffix -twisted-conch,$(addprefix python,$(versions)))

archpackages=$(addsuffix -twisted-bin,$(addprefix python,$(versions)))
packages=$(indeppackages) $(archpackages)
docdir=debian/twisted-doc/$(DOC)/twisted-doc/
gamesdir=debian/twisted-quotes/usr/share/games/fortunes/

build: build-stamp
build-stamp:
	set -e;$(foreach version,$(versions),\
	         /usr/bin/python$(version) setup.py build;)
	touch build-stamp

clean:
	rm -rf *-stamp build
	rm -rf $(addprefix debian/,$(packages)) debian/files debian/substvars
	rm -rf _trial_temp test.log
	find . -name "*.pyc" |xargs -r rm

install-base: install-base-stamp
install-base-stamp: build-stamp

	set -e;for package in $(packages);\
		do rm -rf debian/$$package;\
		install -d debian/$$package/DEBIAN/;\
		install -d debian/$$package/${DOC}/$$package/;\
		install -m 644 \
		      debian/copyright debian/$$package/${DOC}/$$package/;\
		install -m 644 debian/changelog \
	               debian/$$package/${DOC}/$$package/changelog.Debian;\
		gzip -9 debian/$$package/${DOC}/$$package/changelog.Debian;\
	done

	set -e; for version in $(versions); \
		do package=python$$version-twisted;\
		/usr/bin/python$$version setup.py install \
                   --no-compile --root debian/$$package; \
	done

	set -e; for version in $(versions);\
		do package=python$$version-twisted;\
		binpackage=python$$version-twisted-bin;\
		for file in `find debian/python$$version-twisted -name "*.so"`;\
			do target=`echo $$file | sed 's/twisted/twisted-bin/'`;\
			if [ ! -d `dirname $$target` ]; then\
				install -d `dirname $$target`; \
			fi; \
			mv $$file $$target; \
		done; \
	done
	touch install-base-stamp

install-indep: install-indep-stamp

install-indep-stamp: install-base-stamp

	set -e; for version in $(versions); \
		do package=python$$version-twisted;\
		install -d debian/$$package/usr/share/man/man1;\
		install -d debian/$$package/usr/lib/menu/;\
		install -d debian/$$package/usr/bin/;\
		install -m 644 \
		       ChangeLog debian/$$package/${DOC}/$$package/changelog;\
		gzip -9 debian/$$package/${DOC}/$$package/changelog;\
		install -m 644 README CREDITS \
		           debian/$$package/${DOC}/$$package/;\
		for file in debian/$$package/usr/bin/*;\
			do sed "1s|.*|#!/usr/bin/python$$version|" $$file >\
			   $$file$$version;\
			rm $$file;\
			chmod 755 $$file$$version;\
		done;\
		for file in doc/man/*.1;\
    			do target=`basename $$file .1`$$version.1;\
			install -m 644 $$file \
			        debian/$$package/usr/share/man/man1/$$target;\
		done;\
		gzip -9 debian/$$package/usr/share/man/man1/*.1;\
		sed "s/@VERSION@/$$version/g" debian/python-twisted.menu.in >\
		    debian/$$package/usr/lib/menu/$$package;\
		for maint in prerm postrm postinst;\
			do sed "s/@VERSION@/$$version/g" \
			debian/python-twisted.$$maint.in > \
			debian/$$package/DEBIAN/$$maint;\
			chmod 755 debian/$$package/DEBIAN/$$maint;\
		done;\
	done

	if [ -d debian/$(DEFAULTPACKAGE)/usr/bin/ ]; then \
	  set -e;for file in debian/$(DEFAULTPACKAGE)/usr/bin/*;\
		  do target=`echo $$file | sed "s/$(DEFAULT)$$//"`;\
		  ln -s `basename $$file` $$target;\
		  manname=`basename $$target`.1.gz;\
		  ln -s `basename $$file`.1.gz \
		        debian/$(DEFAULTPACKAGE)/usr/share/man/man1/$$manname;\
	  done;\
	fi

	set -e; for version in $(versions); \
		do package=python$$version-twisted-conch;\
		tpackage=python$$version-twisted;\
		dir=usr/lib/python$$version/site-packages;\
		install -d debian/$$package/$$dir/twisted/scripts; \
		install -d debian/$$package/usr/bin; \
		install -d debian/$$package/usr/share/man/man1; \
		mv debian/$$tpackage/$$dir/twisted/conch \
		   debian/$$package/$$dir/twisted/;\
		for script in conch ckeygen tkconch; \
			do mv \
			   debian/$$tpackage/$$dir/twisted/scripts/$$script.py \
			   debian/$$package/$$dir/twisted/scripts;\
			mv debian/$$tpackage/usr/bin/$$script* \
			   debian/$$package/usr/bin/; \
			mv debian/$$tpackage/usr/share/man/man1/$$script* \
			   debian/$$package/usr/share/man/man1/; \
		done;\
		for maint in postinst prerm postrm; \
			do sed -e "s/@VERSION@/$$version/g" \
			       -e "s/twisted/twisted-conch/"\
			debian/python-twisted.$$maint.in > \
			debian/$$package/DEBIAN/$$maint;\
			chmod 755 debian/$$package/DEBIAN/$$maint;\
		done;\
		install -d debian/$$package/usr/lib/menu;\
		sed "s/@VERSION@/$$version/g" \
		debian/python-twisted-conch.menu.in > \
		debian/$$package/usr/lib/menu/$$package;\
	done
		
	set -e;for subdir in examples howto specifications img vision;\
		do install -d $(docdir)/$$subdir;\
	done
	install -d debian/twisted-doc/usr/share/doc-base/
	install -m 644 debian/twisted-book \
	               debian/twisted-doc/usr/share/doc-base/
	install -m 644 doc/howto/*.html doc/howto/book.* \
	               doc/howto/stylesheet.css $(docdir)/howto/
	gzip -9 $(docdir)/howto/book.ps
	install -m 644 doc/specifications/*.html $(docdir)/specifications/
	install -m 644 doc/vision/*.html $(docdir)/vision/
	install -m 644 doc/img/*.png doc/img/*.bmp $(docdir)/img/
	cp -ar doc/examples/* $(docdir)/examples/
	find $(docdir)/examples/ -name CVS | xargs -r rm -r
	find $(docdir)/examples/ -name .cvsignore | xargs -r rm
	find $(docdir)/examples/ -name .svn | xargs -r rm -r
	sed '/api/d' doc/index.xhtml > $(docdir)/index.html
	set -e;for maint in postinst prerm;\
		do sed 's/@CONTROL@/twisted-book/' debian/twisted-doc.$$maint > \
		           debian/twisted-doc/DEBIAN/$$maint;\
		chmod 755 debian/twisted-doc/DEBIAN/$$maint;\
	done

	install -d debian/twisted-doc-api/usr/share/doc-base/
	install -d debian/twisted-doc-api/${DOC}/twisted-doc/
	cp -ar doc/api/ debian/twisted-doc-api/${DOC}/twisted-doc/
	rm -rf `find debian/twisted-doc-api/${DOC}/twisted-doc -name .cvsignore`
	rm -rf `find debian/twisted-doc-api/${DOC}/twisted-doc -name CVS`
	rm -rf `find debian/twisted-doc-api/${DOC}/twisted-doc -name .svn`
	install -m 644 debian/twisted-api \
	               debian/twisted-doc-api/usr/share/doc-base/
	set -e;for maint in postinst prerm;\
		do sed 's/@CONTROL@/twisted-api/' debian/twisted-doc.$$maint > \
		           debian/twisted-doc-api/DEBIAN/$$maint;\
		chmod 755 debian/twisted-doc-api/DEBIAN/$$maint;\
	done

	install -m 644 debian/python-twisted.README.Debian \
	               debian/python-twisted/${DOC}/python-twisted/README.Debian

	install -d $(gamesdir)
	install -m 644 doc/fun/Twisted.Quotes $(gamesdir)/twisted-quotes
	strfile $(gamesdir)/twisted-quotes
	touch install-indep-stamp


binary-indep: build install-indep
	set -e;for package in $(indeppackages);\
		do dpkg-gencontrol -isp -p$$package -Pdebian/$$package;\
		chown -R root.root debian/$$package;\
		chmod -R go=rX debian/$$package;\
		dpkg --build debian/$$package ..;\
	done

binary-arch: build install-base
	find debian -name "*.so" | xargs -n 1 strip --remove-section=.comment \
                                                    --remove-section=.note \
	                                            --strip-unneeded
	find debian -name "*.so" | xargs -n 1 dpkg-shlibdeps
	set -e;for package in $(archpackages);\
		do dpkg-gencontrol -isp -p$$package -Pdebian/$$package;\
		chown -R root.root debian/$$package;\
		chmod -R go=rX debian/$$package;\
		dpkg --build debian/$$package ..;\
	done

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