#!/usr/bin/make -f

package=lplayer
binaryname=lplayer
picturessourcedir=src/images
shellscriptsourcedir=src
lplayerdir=/usr/share/$(package)
srcdir=src
debbuilddir=$(shell pwd)/debian/tmp
patches=$(shell cd debian/patches && ls *.patch) #ugly
makefile=Makefile

-include debian/debiandirs

debian/debiandirs: admin/debianrules
	perl -w admin/debianrules echodirs > debian/debiandirs
	
patch: 
	if [ ! -e "patch-stamp" ] ; then \
	for patch in $(patches); do \
	        printf "Applying patch $$patch: "; \
   	     if patch --dry-run -p1 < debian/patches/$$patch; then \
   	             patch -p1 < debian/patches/$$patch; \
                printf "successfull.\n"; \
   	     else \
   	             printf "failed.\nPatch output follows:\n"; \
	                patch --dry-run -p1 < debian/patches/$$patch; \
   	     fi \
	done\
	fi
	touch patch-stamp

unpatch:
	if [ -e "patch-stamp" ] ; then \
	for PATCH in $(patches); do \
	printf \# Reversing patch $$PATCH; \
	if patch --dry-run -R -p1 < debian/patches/$$PATCH; then\
	 patch -R -p1 < debian/patches/$$PATCH; \
	 printf "successfull.\n" ;\
	else \
	 printf "failed.\nPatch output follows:\n" ;\
	 patch --dry-run -R -p1 < debian/patches/$$PATCH ;\
	fi \
	done \
	fi
	rm -f patch-stamp
																		  

build: 
	$(checkdir)
	#cd $(srcdir) && tmake lp.pro -o $(makefile)
	#cd $(srcdir) && $(MAKE) -f $(makefile)
	$(MAKE) -f Makefile.cvs
	#QTDIR=/usr/share/qt3 ./configure --prefix=/usr --disable-rpath --datadir=/usr/share --with-qt-includes=/usr/include/qt
	./configure $(configkde)
	$(MAKE)
	touch build

clean: cleanfiles 

cleanfiles:
	$(checkdir)
	rm -f build
	(if  [ -f $(makefile) ] ; then $(MAKE) -f $(makefile) -i clean ; fi )
	dh_clean
	cd $(srcdir) && rm -f $(makefile)
	rm -f $(makefile)

binary-indep:	checkroot 
	$(checkdir)
# There are no architecture-independent files to be uploaded
# generated by this package.  If there were any they would be
# made here.

binary-arch:	checkroot build
	$(checkdir)
	rm -rf $(debbuilddir)
	
	# make the directories
	install -d $(debbuilddir)/DEBIAN\
		-d $(debbuilddir)/usr/share/doc/$(package)
	#install -d $(debbuilddir)/$(lplayerdir)
	#install -d $(debbuilddir)/usr/lib/lplayer
	#install -d $(debbuilddir)/usr/bin
	install -d $(debbuilddir)/$(lplayerdir)/images
	cd $(srcdir) && $(MAKE) install DESTDIR=$(debbuilddir)
	#$(MAKE) install DESTDIR=$(debbuilddir)
	dh_installdocs
	#ln -s copying $(debbuilddir)/usr/share/doc/$(package)/copyright
	
	# copy the pictures used in the binary
	cp -a $(picturessourcedir)/*.png $(debbuilddir)/$(lplayerdir)/images
	#rm -rf $(debbuilddir)/$(lplayerdir)/CVS
	
	# copy the binary
	#install -o root -g root -m 0755 -s $(srcdir)/$(binaryname) $(debbuilddir)/usr/lib/lplayer/$(binaryname)
	
	# copy the shell script that launches lplayer
	#install -o root -g root -m 0755  $(shellscriptsourcedir)/$(shellscriptname) $(debbuilddir)/usr/bin/$(shellscriptname)
	
	dh_installchangelogs
	# menu item
	dh_installmenu -A  
	# the splendid manpage ;)
	dh_installman debian/$(binaryname).1
	
	# pictures and text files should not be executable 
	#chmod a-xw $(debbuilddir)/$(lplayerdir)/*.png
	
	# compress docs
	dh_compress
	
	# create library dependecies
	dh_shlibdeps
	dh_fixperms
	#dh_strip
	dh_installdeb
	dpkg-gencontrol -isp
	dh_md5sums
	dpkg --build $(debbuilddir) ..

define checkdir
	test -f $(srcdir)/$(package).cpp -a -f debian/rules
endef

# Below here is fairly generic really

binary:	binary-indep binary-arch

checkroot:
	$(checkdir)
	test $$(id -u) = 0

.PHONY: binary binary-arch binary-indep clean checkroot
