#!/usr/bin/make -f

DEB_PYTHON_SYSTEM=pysupport
DEB_DH_INSTALL_SOURCEDIR=$(CURDIR)/debian/tmp

DEBIAN_VERSION:=$(shell head -1 debian/changelog | cut -d\( -f2 | cut -d\) -f1)
UPSTREAM_VERSION:=1.9.0
#$(shell echo "$(DEBIAN_VERSION)"| cut -d- -f1 | sed 's/~//')

#configure/freevo::
#	#this is needed in SVN versions to build documentations
#	#cd Docs/installation && docbook2html -o html howto.sgml
#	#but also locales
#	sh autogen.sh
#	echo '__revision__="11545"' > src/revision.py

include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/class/python-distutils.mk

# this is already copied in debian/copyright
DEB_DH_INSTALL_ARGS= -XAquaFusion_Icons_LICENSE -XCOPYING

install/freevo::

	: # Replace all '#!' calls to python with /usr/bin/python
	: # and make them executable
	for i in debian/tmp/usr/bin/freevo `find debian/tmp -type f -name "*py" `; 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

	: #fill __init__.py so that they are not butchered by pysupport
	for i in `find debian/tmp -type f -name "__init__.py" -size 0 `; do  echo '#placeholder' >> "$$i" ; echo "sos $$i" ; done

	mv $(CURDIR)/debian/tmp/usr/share/doc/freevo-$(UPSTREAM_VERSION) \
		$(CURDIR)/debian/tmp/usr/share/doc/freevo

	mv $(CURDIR)/README \
		$(CURDIR)/debian/freevo-doc/usr/share/doc/freevo
	mv $(CURDIR)/TODO \
		$(CURDIR)/debian/freevo-doc/usr/share/doc/freevo

	mv $(CURDIR)/debian/tmp/usr/bin/freevo  $(CURDIR)/debian/tmp/usr/bin/freevo.real
	install  $(CURDIR)/debian/scripts/freevo $(CURDIR)/debian/tmp/usr/bin/freevo
	install  $(CURDIR)/debian/scripts/freevo+log $(CURDIR)/debian/tmp/usr/share/freevo/contrib/freevo+log

	dh_installinit -pfreevo --no-start --name=freevo_xserver
	dh_installinit -pfreevo --no-start --name=freevo_recordserver
	dh_installinit -pfreevo --no-start --name=freevo_encodingserver
	dh_installinit -pfreevo --no-start --name=freevo_webserver
	dh_installinit -pfreevo --no-start --name=freevo_rssserver

$(patsubst %,install/%,$(DEB_PACKAGES)) ::
	if [ -e debian/$(cdbs_curpkg).linda-overrides ] ; then \
	  mkdir -p debian/$(cdbs_curpkg)/usr/share/linda/overrides/ ; \
	  cp debian/$(cdbs_curpkg).linda-overrides debian/$(cdbs_curpkg)/usr/share/linda/overrides/$(cdbs_curpkg) ; \
	fi

binary-install/freevo-data::
	for i in `cd  debian/freevo-data/usr/share/freevo/fonts ; echo Vera*`; do \
		rm -v debian/freevo-data/usr/share/freevo/fonts/$$i  ;\
		dh_link -pfreevo-data /usr/share/fonts/truetype/ttf-bitstream-vera/$$i \
			/usr/share/freevo/fonts/$$i ;\
	done
	for i in `cd debian/freevo-data/usr/share/freevo/fonts ; echo Deja*`; do \
		rm -v debian/freevo-data/usr/share/freevo/fonts/$$i ;\
		dh_link -pfreevo-data /usr/share/fonts/truetype/ttf-dejavu/$$i \
			/usr/share/freevo/fonts/$$i ;\
	done

	# Temporaly hacks to fix file permissions
	chmod a-x debian/freevo-data/usr/share/freevo/skins/main/Tux* \
		debian/freevo-data/usr/share/freevo/fonts/Sansumi-DemiBold.ttf \
		debian/freevo-data/usr/share/freevo/images/atlantice/back_black.jpg \
		debian/freevo-data/usr/share/freevo/skins/osd/base.fxd
	find  debian/freevo-data/usr/share/freevo -type f -name '*.png' -print0 | xargs -0 chmod a-x

clean::
	rm -f version.pyc
