#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
#
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.
#
# Modified to make a template file for a multi-binary package with separated
# build-arch and build-indep targets  by Bill Allombert 2001

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

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE    ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_HOST_ARCH_CPU    ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU)
DEB_BUILD_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_BUILD_GNU_SYSTEM ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_SYSTEM)
DEB_BUILD_ARCH	     ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)
ifeq ($(DEB_HOST_ARCH_CPU),powerpc)
PLATFORM	= linuxppcgcc
else
PLATFORM	=
endif
ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
BUILDOPT	= --build=debug
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
INSTALL_PROGRAM = -s
endif
SAPDB		= --disable-sapdb
ifneq (,$(findstring linux-gnu,$(DEB_BUILD_GNU_SYSTEM)))
ifneq (,$(findstring i386,$(DEB_BUILD_ARCH)))
SAPDB		= --enable-sapdb
endif
ifneq (,$(findstring ia64,$(DEB_BUILD_ARCH)))
SAPDB		= --enable-sapdb
endif
ifneq (,$(findstring amd64,$(DEB_BUILD_ARCH)))
SAPDB		= --enable-sapdb
endif
endif
HBOOK		= --enable-cern
ifneq (,$(findstring kfreebsd-i386,$(DEB_BUILD_GNU_SYSTEM)))
HBOOK		= --disable-cern
endif
ifneq (,$(findstring kfreebsd-amd64,$(DEB_BUILD_GNU_SYSTEM)))
HBOOK		= --disable-cern
endif
UNUR		= --enable-unuran
ifneq (,$(findstring alpha, $(DEB_BUILD_ARCH)))
UNUR		= --disable-unuran
endif
XRD		= --enable-xrootd
ifneq (,$(findstring sparc, $(DEB_BUILD_ARCH)))
XRD		= --disable-xrootd
endif

DTMP		= debian/tmp
PREFIX		= /usr
SYSCONFDIR	= /etc
PKGDOCDIR	= $(PREFIX)/share/doc/root
CINTDIR		= $(PREFIX)/lib/root/$(SOVERS)/cint
ICONTHEME	= $(PREFIX)/share/icons/hicolor/48x48
CONFOPTIONS	:=--enable-cintex				\
		  --enable-clarens				\
		  --enable-explicitlink				\
		  --enable-gdml					\
		  --enable-fftw3				\
		  --enable-ldap					\
		  --enable-qt					\
		  --enable-qtgsi				\
		  --enable-mathcore				\
		  --enable-mathmore				\
		  --enable-minuit2				\
		  --enable-mysql				\
		  --enable-peac					\
		  --enable-pgsql				\
		  --enable-odbc					\
		  --enable-reflex				\
		  --enable-roofit				\
		  --enable-ruby					\
		  --enable-shadowpw				\
		  --enable-shared				\
		  --enable-soversion				\
		  --enable-table				\
		  --disable-rpath				\
		  --disable-afs					\
		  --disable-srp					\
		  --disable-builtin-afterimage			\
		  --disable-builtin-ftgl			\
		  --disable-builtin-freetype			\
		  --disable-builtin-pcre			\
		  --disable-builtin-zlib			\
		  --disable-alien				\
		  --disable-chirp				\
		  --disable-dcache				\
		  --disable-g4root				\
		  --disable-gfal				\
		  --disable-globus				\
		  --disable-monalisa				\
		  --disable-oracle				\
		  --disable-pythia6				\
		  --disable-rfio				\
		  --fail-on-missing				\
		  $(UNUR)					\
		  $(XRD)					\
		  $(SAPDB)					\
		  $(HBOOK)					\
		  $(BUILDOPT)					
#
# Things for the future 
#
#		  --enable-mathcore	libroot
#		  --enable-cintex	libroot
#		  --enable-reflex	libroot
#		  --with-globus=/opt/globus
#		  --disable-builtin-gsl				
#		  --disable-builtin				\

VERSION		:= $(shell cat build/version_number | tr '/' '.')
MAJOR		:= $(shell echo $(VERSION) | tr '.' ' ' | cut -f1 -d' ')
MINOR		:= $(shell echo $(VERSION) | tr '.' ' ' | cut -f2 -d' ')
SOVERS		:= $(MAJOR).$(MINOR)
PKGLIST		:= $(shell ./configure $(PLATFORM) --pkglist $(CONFOPTIONS) | sed -n -e 's/packages: //p')
ESSENTIALS	:= libroot-dev			\
		   libroot			\
		   root-system-bin		\
		   root-system-common		\
		   ttf-root-installer		
PLUGINS		:= libroot-ldap			\
		   libroot-mathmore		\
		   libroot-minuit		\
		   libroot-mlp			\
		   libroot-quadp		\
		   libroot-tmva			\
		   libroot-unuran		\
		   root-system-doc		\
		   root-plugin-asimage		\
		   root-plugin-fftw3		\
		   root-plugin-fumili		\
		   root-plugin-gl		\
		   root-plugin-hbook		\
		   root-plugin-krb5		\
		   root-plugin-minuit2		\
		   root-plugin-netx		\
		   root-plugin-odbc		\
		   root-plugin-proof		\
		   root-plugin-qt		\
		   root-plugin-sql		\
		   root-plugin-xml		\
		   root-plugin-xproof		\
		   root-system-proofd		\
		   root-system-rootd		\
		   root-system-xrootd		
EXTRAS		:= libroot-clarens		\
		   libroot-python		\
		   libroot-roofit		\
		   libroot-ruby			\
		   root-plugin-alien		\
		   root-plugin-castor		\
		   root-plugin-chirp		\
		   root-plugin-dcache		\
		   root-plugin-globus		\
		   root-plugin-peac		\
		   root-plugin-maxdb		\
		   root-plugin-mysql		\
		   root-plugin-oracle		\
		   root-plugin-pgsql		\
		   root-plugin-pythia5		\
		   root-plugin-pythia6		\
		   root-plugin-srp		\
		   root-plugin-venus		
PLUGINLIST	:= $(filter-out $(EXTRAS) $(ESSENTIALS), $(PKGLIST))
EXTRALIST	:= $(filter-out $(PLUGINS) $(ESSENTIALS), $(PKGLIST))
PLUGINLIST	:= $(PLUGINLIST:libroot%=libroot%-dev)
EXTRALIST	:= $(EXTRALIST:libroot%=libroot%-dev)
PLUGINHEAD	:= $(filter-out $(lastword $(PLUGINLIST)), $(PLUGINLIST))
PLUGINLIST	:= $(PLUGINHEAD:%=%,) $(lastword $(PLUGINLIST))
EXTRAHEAD	:= $(filter-out $(lastword $(EXTRALIST)), $(EXTRALIST))
EXTRALIST	:= $(EXTRAHEAD:%=%,) $(lastword $(EXTRALIST))
TEMPLATES	:= $(wildcard debian/*.in)
SPKGLIST	:= $(filter-out root-system-doc root-system-common ttf-root-installer, $(PKGLIST:libroot%=libroot%$(SOVERS))) $(filter libroot%, $(PKGLIST:libroot%=libroot%-dev))
IPKGLIST	:= root-system-doc ttf-root-installer root-system-common root-system
DPKGLIST	:= $(SPKGLIST:%=%,) root-system-doc, root-ttf
#root-plugins root-extra-plugins
SPKGOPTS	:= $(SPKGLIST:%=-p%) 
IPKGOPTS	:= $(IPKGLIST:%=-p%) 
PKGOPTS		:= $(IPKGOPTS) $(SPKGOPTS)
HAVERUBY	:= $(filter libroot-ruby, $(PKGLIST))
ifeq ($(HAVERUBY), libroot-ruby)
RUBYARCHDIR	:= $(shell ruby -rrbconfig -e 'puts Config::CONFIG["archdir"]' | sed 's,/,,')
else
RUBYARCHDIR	:= 
endif
HAVEPYTHON	:= $(filter libroot-python, $(PKGLIST))
ifeq ($(HAVEPYTHON), libroot-python)
PYVERS		:= $(shell pyversions -vs)
PYVER		:= $(shell python -c 'import sys; print "%d.%d" % (sys.version_info[0], sys.version_info[1])')
PYTHONDIR	:= /usr/lib/python-support/root/python$(PYVER)
else
PYTHONDIR	:= 
endif

# This has to be exported to make some magic below work.
export DH_OPTIONS

show_pkgs:
	@echo -e "SPKGLIST:\n $(foreach i, $(SPKGLIST), \t$(i)\n)"
	@echo -e "IPKGLIST:\n $(foreach i, $(IPKGLIST), \t$(i)\n)"
	@echo -e "PLUGINLIST:\n $(foreach i, $(PLUGINLIST), \t$(i)\n)"
	@echo -e "EXTRALIST:\n $(foreach i, $(EXTRALIST), \t$(i)\n)"
	@echo -e "HAVERUBY: $(HAVERUBY)\nRUBYARCHDIR: $(RUBYARCHDIR)"
	@echo -e "HAVEPYTHON: $(HAVEPYTHON)\nPYTHONDIR: $(PYTHONDIR)"

show_libpkgs:
	@echo "$(SPKGLIST)"
	@echo "$(patsubst %, -p%, $(filter libroot%$(SOVERS),$(SPKGLIST)))"

show_conf:
	./configure $(PLATFORM) --pkglist $(CONFOPTIONS)

debian/%:debian/%.in  
	@echo "Making $@ from $< "
	@sed -e 's,@prefix@,$(PREFIX),g' \
		-e 's,@sysconfdir@,$(SYSCONFDIR),g' \
		-e 's,@pkgdocdir@,$(PKGDOCDIR),g' \
		-e 's,@rubyarchdir@,$(RUBYARCHDIR),g' \
		-e 's,@pythondir@,$(PYTHONDIR),g' \
		-e 's,@libvers@,$(SOVERS),g' < $< > $@ 

debian/control:debian/control.in 
	@echo "Making $@ (special)"
	@cp $< $@.tmp
	@for p in $(PKGLIST) ; do \
	  if test -f build/package/common/$$p.control ; then \
	    echo "Adding package $$p to control file" ; \
	    cat build/package/common/$$p.control >> $@.tmp ; fi ; done 
	@builddep="$(shell ./build/package/lib/makebuilddepend.sh $(PKGLIST))";\
	    sed -e 's,@prefix@,$(PREFIX),g' \
		-e 's,@libvers@,$(SOVERS),g' \
		-e 's,@sysconfdir@,$(SYSCONFDIR),g' \
		-e 's,@pkgdocdir@,$(PKGDOCDIR),g' \
		-e "s/@builddepends@/$${builddep}/" \
		-e 's/@pyvers@/$(PYVER)/g' \
		-e 's/@pkglist@/$(DPKGLIST)/'	\
		-e 's/@plugins@/$(PLUGINLIST)/'	\
		-e 's/@extras@/$(EXTRALIST)/' < $@.tmp > $@
	rm -rf $@.tmp 

configure-stamp:$(TEMPLATES:%.in=%) config.status
config.status: configure 
	dh_testdir
	# Add here commands to configure the package.
	./configure $(PLATFORM)						\
		    --prefix=$(PREFIX)					\
		    --mandir=$(PREFIX)/share/man/man1			\
		    --docdir=$(PKGDOCDIR)				\
		    --cintincdir=$(CINTDIR)				\
		    --etcdir=$(SYSCONFDIR)/root				\
		    --with-sys-iconpath=$(PREFIX)/share/pixmaps		\
		    --libdir=$(PREFIX)/lib/root/$(SOVERS)		\
		    $(CONFOPTIONS)
	touch xrootd/src/headers.d 
	touch asimage/src/headers.d 
	touch unuran/src/headers.d 
	touch configure-stamp

#	  --fontdir=%_datadir/root/fonts/truetype
#Architecture 
build: build-arch build-indep

build-arch: build-arch-cint-stamp
build-arch-stamp:  configure-stamp

	# Add here commands to compile the arch part of the package.
	$(MAKE) ASTEPETAG= XROOTDETAG= UNURANETAG=
	touch build-arch-stamp

build-arch-cint-stamp:build-arch-stamp
	$(MAKE) cintdlls
	touch build-arch-cint-stamp

build-indep: build-indep-stamp
build-indep-stamp:  configure-stamp

	# Add here commands to compile the indep part of the package.
	#$(MAKE) doc
	touch build-indep-stamp

clean:	$(TEMPLATES:%.in=%)
	dh_testdir
	dh_testroot
	debconf-updatepo --podir=build/package/debian/po
	-rm -f debian/pyversions debian/pycombat
	-rm -f build-arch-stamp build-indep-stamp build-arch-cint-stamp \
		config.status configure-stamp config.log
	-rm -rf minuit2/src/Minuit-1_7_6
	-rm -rf freetype/src/freetype-2.1.9
	#-rm -rf asimage/src/libAfterImage 
	-rm -rf debian/*.files	debian/*.install	
	-rm -f	macros/html.C 
	-rm -f	cint/MAKEINFO
	-rm -f	etc/daemons/proofd.rc.d
	-rm -f	etc/daemons/proofd.xinetd
	-rm -f	etc/daemons/rootd.rc.d
	-rm -f	etc/daemons/rootd.xinetd
	-rm -f	etc/daemons/olbd.rc.d
	-rm -f	etc/daemons/xrootd.rc.d
	-rm -f	qtgsi/src/G__QtGSI.cxx qtgsi/src/G__QtGSI.h
	-rm -f	qt/src/moc_*.cxx
	-rm -f	G__genreflex_rootcint.h
	-rm -f	asimage/src/libAfterImage/Makefile
	-rm -f	asimage/src/libAfterImage/afterbase.h
	-rm -f	asimage/src/libAfterImage/afterimage-config
	-rm -f	asimage/src/libAfterImage/afterimage-libs
	-rm -f	asimage/src/libAfterImage/config.h
	-rm -f	asimage/src/libAfterImage/config.log
	-rm -f	asimage/src/libAfterImage/config.status
	-rm -f	asimage/src/libAfterImage/libAfterImage.a
	-rm -f	xrootd/src/xrootd/GNUmakefile
	-rm -f	xrootd/src/xrootd/src/GNUmake.env
	-rm -f	xrootd/src/xrootd/src/GNUmake.options
	-find . -name ".#*" | xargs rm -rf
	-find . \( -name "G__*.cxx" 			\
		   -or -name "G__*.h" 			\
		   -or -name "G__*.c" \) \
		-and -not -name "G__ci.h" 		\
		-and -not -name G__postprocess.h 	\
		-and -not -name "G__main.c" 		\
		-and -not -name "G__setup.c" 		\
	        -and -not -name "G__ci_fproto.h" 	\
		-and -not -name "G__security.h"  	\
		| xargs rm -rf
	# Add here commands to clean up after the build process.
	touch unuran/src/.bogus.tar.gz
	-$(MAKE) distclean ASTEPVERS=.bogus  ASTEPETAG= \
			   XROOTDDIRD=	     XROOTDETAG= \
			   UNRVERS=.bogus    UNURANETAG=
	rm -f unuran/src/.bogus.tar.gz
	build/unix/makecintdll.sh clean
	rm -rf bin lib include				\
		system.rootrc				\
		config/Makefile.config			\
		README/Changelog			\
		etc/system.rootrc			\
		etc/system.rootauthrc			\
		etc/system.rootdaemonrc			\
		etc/root.mimes				\
		build/misc/root-help.el			\
		debian/*.files				\
		debian/*.install			\
		debian/shlibs.local			\
		freetype/src/freetype-2.1.3		\
		cint/lib/posix/a.out			\
		cint/include/*.so*			\
		cint/include/*.dll			\
		cint/include/sys/*.so*			\
		cint/include/sys/*.dll			\
		cint/stl/*.so*				\
		cint/stl/*.dll				\
		pcre/src/pcre-6.4			\
		unuran/src/unuran-*-root/config.status	\
		unuran/src/unuran-*-root/config.log	\
		xrootd/src/xrootd/lib			\
		xrootd/src/xrootd/obj			\
		xrootd/src/xrootd/bin
	# touch the headers for xrootd and asimage 
	touch xrootd/src/headers.d 
	touch asimage/src/headers.d 
	touch unuran/src/headers.d 
	-rm -f  $(filter-out debian/control, $(TEMPLATES:%.in=%))
	dh_clean 

pkglists:
	-rm -rf debian/*.files	debian/*.install	
	build/package/lib/makelists.sh debian debian/tmp \
		$(VERSION) $(PREFIX) $(SYSCONFDIR) $(PKGDOCDIR)
#	@for i in debian/libroot* ; do \
#	  if test -d $$i ; then continue ; fi ; \
#	  case $$i in \
#	    *-dev.*|*.in|libroot*$(SOVERS).*) continue ;; \
#	  *) echo "Renaming $$i to version specific" ; \
#	     cp $$i `echo $$i | sed 's/libroot\(.*\)\.\(.*\)/libroot\1$(SOVERS).\2/'` ;; esac ; \
#	done 

install: install-indep install-arch
install-indep: 
	dh_testdir
	dh_testroot
	dh_clean -k -i -X$(DTMP)
	dh_installdirs -i

	# Add here commands to install the indep part of the package into
	# debian/<package>-doc.
	#INSTALLDOC#

	# dh_install $(IPKGOPTS) --sourcedir=$(DTMP)
	dh_install -i --sourcedir=$(DTMP)

	# Fix some permissions 
	-find debian/root-system-common/$(PREFIX)/share/root/icons \
		-perm 0755 -and -not -type d | xargs chmod a-x 
	mkdir -p debian/root-system-doc/usr/share/lintian/overrides/
	cp debian/root-system-doc.overrides \
	  debian/root-system-doc/usr/share/lintian/overrides/root-system-doc

	# New Ld.so.conf file 
	cp debian/root-system-common.conf \
	  debian/root-system-common/$(SYSCONFDIR)/ld.so.conf.d


install-arch: 
	dh_testroot
	dh_clean -k -s -X$(DTMP)
	# Need to clean this, because we don't want to use the 
	# symblic links made later on. 
	rm -rf $(DTMP)$(PREFIX)/lib/root
	dh_installdirs -s

	# Add here commands to install the arch part of the package into 
	# $(DTMP).
	$(MAKE) install DESTDIR=$(CURDIR)/$(DTMP) \
		ASTEPETAG= XROOTDETAG= UNURANETAG=

	# Install desktop entry and icon 
	mkdir -p $(DTMP)/$(PREFIX)/share/applications
	mkdir -p $(DTMP)/$(PREFIX)/share/pixmaps
	mkdir -p $(DTMP)/$(ICONTHEME)/apps
	mkdir -p $(DTMP)/$(ICONTHEME)/mimetypes
	-cp debian/root-system-bin.desktop $(DTMP)/$(PREFIX)/share/applications
	-cp build/package/debian/root-system-bin.xpm \
		$(DTMP)/$(PREFIX)/share/pixmaps/root-system-bin.xpm
	-cp build/package/debian/root-system-bin.png \
		$(DTMP)/$(ICONTHEME)/apps/root-system-bin.png
	-cp build/package/debian/application-x-root.png \
		$(DTMP)/$(ICONTHEME)/mimetypes/application-x-root.png

	# Make root-config output '/usr/lib/root' instead of 
	# '/usr/lib/root/<version>' 
	sed 's,$(PREFIX)/lib/root/$(SOVERS),$(PREFIX)/lib/root,' \
		< $(DTMP)/$(PREFIX)/bin/root-config > root-config.tmp 
	mv root-config.tmp $(DTMP)/$(PREFIX)/bin/root-config

	# Make default ZIP mode be new library (system library)
	sed -e 's,Root\.ZipMode:.*,Root.ZipMode: 1,' \
	    -e 's,\(.*\.DynamicPath:.*\):$(PREFIX)/lib/root[^:]*\(.*\),\1\2,'\
		< $(DTMP)$(SYSCONFDIR)/root/system.rootrc  > rootrc.tmp 
	mv rootrc.tmp $(DTMP)$(SYSCONFDIR)/root/system.rootrc

	# Rename proofserv, xrdgsiproxy, xrdpwdadmin, xrdcp, and xrd in 
	# /usr/bin to end in `.bin' and make wrapper script in /usr/bin 
	for i in proofserv xrdgsiproxy xrdpwdadmin xrdcp xrd ; do	\
	  if test -f $(DTMP)$(PREFIX)/bin/$$i &&			\
	     test -f build/package/common/$$i.in ; then			\
	    mv $(DTMP)$(PREFIX)/bin/$$i					\
	      $(DTMP)$(PREFIX)/bin/$$i.bin ;				\
	    sed -e 's,@prefix@,$(PREFIX),g'				\
		-e 's,@libvers@,$(SOVERS),g'				\
		< build/package/common/$$i.in				\
		> $(DTMP)$(PREFIX)/bin/$$i	;			\
	    chmod 755 $(DTMP)$(PREFIX)/bin/$$i ; fi ; done 

	# Make the empty proof log file 
	mkdir -p $(DTMP)$(PREFIX)/share/root/proof/log
	touch $(DTMP)$(PREFIX)/share/root/proof/log/proof.log 

	# Let usr/include/root/cfortran.h point to usr/include/cfortran.h
	rm -rf $(DTMP)$(PREFIX)/include/root/cfortran.h 
	(cd $(DTMP)$(PREFIX)/include/root && ln -s ../cfortran.h cfortran.h)

	# Remove some unwanted files 
	rm -rf $(DTMP)$(PREFIX)/share/doc/root/INSTALL 
	rm -rf $(DTMP)$(PREFIX)/share/doc/root/LICENSE
	rm -rf $(DTMP)$(PREFIX)/share/doc/root/COPYING.CINT
	rm -rf $(DTMP)$(PREFIX)/share/doc/root/cfortran.doc
	rm -rf $(DTMP)$(PREFIX)/share/root/fonts/LICENSE
	rm -rf $(DTMP)$(CINTDIR)/lib/posix/a.out
	rm -rf $(DTMP)$(CINTDIR)/lib/posix/mktypes
	rm -rf $(DTMP)$(CINTDIR)/lib/dll_stl/setup
	rm -rf $(DTMP)$(CINTDIR)/lib/G__*
	rm -rf $(DTMP)$(CINTDIR)/lib/dll_stl/G__*
	rm -rf $(DTMP)$(CINTDIR)/lib/dll_stl/rootcint_*
	rm -rf $(DTMP)$(CINTDIR)/lib/posix/exten.o
	rm -rf $(DTMP)$(CINTDIR)/include/makehpib

	# Remove MacOSX service descriptions 
	rm -rf $(DTMP)$(SYSCONFDIR)/root/*.plist

	# Remove Xinetd and Red Hat Init scripts from /etc 
	rm -rf $(DTMP)$(SYSCONFDIR)/root/*.xinetd
	rm -rf $(DTMP)$(SYSCONFDIR)/root/proofd
	rm -rf $(DTMP)$(SYSCONFDIR)/root/rootd
	rm -rf $(DTMP)$(SYSCONFDIR)/root/xrootd
	rm -rf $(DTMP)$(SYSCONFDIR)/root/olbd 

	# Remove sample configurations for PROOF 
	rm -rf $(DTMP)/$(PREFIX)/share/root/proof/etc/*.sample

	# Remove the CVS directories if present
	find $(DTMP) -name "CVS" | xargs rm -fr
	find $(DTMP) -name ".cvsignore" | xargs rm -fr

	# Fix some permissions 
	-find $(DTMP)$(CINTDIR) -perm 755 -and -not -type d | xargs chmod a-x 
	-chmod a+x $(DTMP)$(CINTDIR)/lib/pthread/setup
	-chmod a-x $(DTMP)$(PREFIX)/lib/root/$(SOVERS)/*.py
	-chmod a-x $(DTMP)$(SYSCONFDIR)/root/html/*
	# -chmod a+x $(DTMP)$(PREFIX)/lib/root/$(SOVERS)/GDMLROOT.py

	# Remove she-bang line from a python scripts
	-for i in $(DTMP)/$(PREFIX)/lib/root/$(SOVERS)/*.py ; do \
	  sed -e 's,#!/usr/bin/env python[0-9.]*,#,' \
	      -e 's,#!/usr/bin/python[0-9.]*,#,' \
		< $$i > $$i.tmp ; \
	  mv $$i.tmp $$i ; \
	done 

	# Make file lists
	mkdir -p $(DTMP)/$(PREFIX)/lib/root
	./debian/rules pkglists

	#	--sourcedir=$(DTMP) $(SPKGOPTS)			
	dh_install -s							\
		--list-missing						\
		--sourcedir=$(DTMP)					\
		-X$(PREFIX)/include/root/config.h			\
		-X$(PREFIX)/include/root/rootcint.pri			\
		-X$(PREFIX)/include/root/rootcintrule.pri		\
		-X$(PREFIX)/include/root/rootlibs.pri			\
		-X$(PREFIX)/include/root/precompile.h			\
		-X$(PREFIX)/include/root/precompile.h.gch		\
		-X$(PREFIX)/include/root/TQtWidget.cw			\
		-X$(PREFIX)/share/root/fonts				\
		-X$(PREFIX)/share/doc/root				\
		-X$(PREFIX)/share/man/man1/system.rootdaemonrc.1	\
		-X$(PREFIX)/share/man/man1/xproofd.1			\
		-X$(PREFIX)/bin/thisroot.sh				\
		-X$(PREFIX)/bin/thisroot.csh				\
		-X$(PREFIX)/share/root/icons/				\
		-X$(SYSCONFDIR)/root/rootd				\
		-X$(SYSCONFDIR)/root/daemons/				\
		-X$(SYSCONFDIR)/root/proof/				\
		-X$(SYSCONFDIR)/root/html				\
		-X$(SYSCONFDIR)/root/RadioNuclides.txt			\
		-X$(SYSCONFDIR)/root/gdb-backtrace-script		\
		-X$(SYSCONFDIR)/root/gdb-backtrace.sh			\
		-X$(SYSCONFDIR)/root/hostcert.conf			\
		-X$(SYSCONFDIR)/root/pdg_table.txt			\
		-X$(SYSCONFDIR)/root/root.mimes				\
		-X$(SYSCONFDIR)/root/system.rootrc			\
		-X$(SYSCONFDIR)/root/system.rootauthrc			\
		-X$(SYSCONFDIR)/root/system.rootdaemonrc		\
		-X$(PREFIX)/lib/root/$(SOVERS)/libAfterImage.a		\
		-X$(PREFIX)/lib/root/$(SOVERS)/PyCintex.pyc		\
		-X$(PREFIX)/lib/root/$(SOVERS)/PyCintex.pyo		\
		-X$(PREFIX)/lib/root/$(SOVERS)/ROOT.pyc			\
		-X$(PREFIX)/lib/root/$(SOVERS)/ROOT.pyo			
#		$(foreach i, \
#		   $(wildcard $(DTMP)/$(PREFIX)/lib/root/lib*.so.$(SOVERS)), \
#		    -X$(i:$(DTMP)/%=%) )

	# Install Lintian overrides files 
	for i in debian/*.overrides ; do	\
	  if test ! -f $$i ; then continue; fi;	\
	  b=`basename $$i .overrides` ; \
	  echo "Installing Lintian override $$i in debian/$$b/usr/share/lintian/overrides/$$b" ; \
	  mkdir -p debian/$$b/usr/share/lintian/overrides/ ; \
	  cp  $$i debian/$$b/usr/share/lintian/overrides/$$b ; \
	done 

	# This is for alternatives registration, if one want's to install
	# Richard Kreckels cint package next to ROOT
	mv debian/root-system-bin/$(PREFIX)/bin/cint \
		debian/root-system-bin/$(PREFIX)/bin/cint.root
	mv debian/root-system-bin/$(PREFIX)/bin/makecint \
		debian/root-system-bin/$(PREFIX)/bin/makecint.root
	mv debian/root-system-bin/$(PREFIX)/share/man/man1/cint.1 \
		debian/root-system-bin/$(PREFIX)/share/man/man1/cint.root.1
	mv debian/root-system-bin/$(PREFIX)/share/man/man1/makecint.1 \
		debian/root-system-bin/$(PREFIX)/share/man/man1/makecint.root.1

# Must not depend on anything. This is to be called by
# binary-arch/binary-indep
# in another 'make' thread.
binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	dh_installexamples
	dh_installmenu
	dh_installdebconf	
	dh_desktop
#	dh_installlogrotate	
#	dh_installemacsen
#	dh_installpam
	dh_installmime
	dh_installinit
#	dh_installcron
#	dh_installinfo
	dh_installman
	dh_link
	dh_strip
	dh_compress -Xusr/share/doc/root/test -Xusr/share/doc/root/tutorials
	dh_fixperms
#	dh_perl

# Build architecture independent packages using the common target.
binary-indep: build-indep install-indep
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture dependent packages using the common target.
binary-arch: build-arch install-arch
	$(MAKE) -f debian/rules DH_OPTIONS=-s binary-common
ifeq ($(HAVEPYTHON), libroot-python)
	# We make this here, so that we get the right python version on the 
	# system compiling. 
	echo "$(PYVER)-" > debian/pyversions
endif
	dh_pysupport -plibroot-python-dev 
	#	$(PREFIX)/lib/root/$(SOVERS) $(PREFIX)/lib/root
	dh_pysupport -plibroot-dev \
		$(PREFIX)/lib/root/$(SOVERS)/python/genreflex
ifeq ($(HAVEPYTHON), libroot-python)
	# dh_pysupport will only write this field if the package is 
	# named something like 'python-*', so we make it ourselves. 
	echo "python:Provides=python$(PYVER)-pyroot" >> debian/libroot-python-dev.substvars
endif
	dh_makeshlibs -V $(patsubst %, -p%, $(filter libroot%$(SOVERS),$(SPKGLIST))) -proot-plugin-proof
	dh_installdeb -s
	dh_shlibdeps -s $(SPKGLIST:%=-L%) \
	  -l$(shell echo $(SPKGLIST:%=debian/%/$(PREFIX)/lib/root/$(SOVERS)) | tr ' ' ':')
	dh_gencontrol -s
	dh_md5sums -s 
	dh_builddeb -s 

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

#
# EOF
#
