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

DTMP		= debian/tmp
PREFIX		= /usr
SYSCONFDIR	= /etc
PKGDOCDIR	= $(PREFIX)/share/doc/root
CINTDIR		= $(PREFIX)/lib/root/$(SOVERS)/cint
CONFOPTIONS	:=--enable-explicitlink				\
		  --enable-fftw3				\
		  --enable-qt					\
		  --enable-qtgsi				\
		  --enable-mathcore				\
		  --enable-mathmore				\
		  --enable-minuit2				\
		  --enable-odbc					\
		  --enable-roofit				\
		  --enable-ruby					\
		  --enable-shared				\
		  --enable-soversion				\
		  --enable-table				\
		  --enable-xrootd				\
		  --disable-rpath				\
		  --disable-afs					\
		  --disable-srp					\
		  --disable-builtin-afterimage			\
		  --disable-builtin-freetype			\
		  --disable-builtin-gsl				\
		  --disable-builtin-pcre			\
		  --disable-builtin-zlib			\
		  --disable-alien				\
		  --disable-chirp				\
		  --disable-dcache				\
		  --disable-gfal				\
		  --disable-globus				\
		  --disable-monalisa				\
		  --disable-oracle				\
		  --disable-pythia				\
		  --disable-pythia6				\
		  --disable-rfio				\
		  --disable-venus				\
		  $(SAPDB)					\
		  $(HBOOK)					\
		  $(BUILDOPT)					
#
# Things for the future 
#
#		  --enable-mathcore	libroot
#		  --enable-cintex	libroot
#		  --enable-reflex	libroot
#		  --with-globus=/opt/globus

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 --pkglist $(CONFOPTIONS) | sed -n -e 's/packages: //p')
ESSENTIALS	:= libroot-dev 			\
		   libroot 			\
		   root-bin 			\
		   root-common			\
		   ttf-root-installer		
PLUGINS		:= libroot-ldap 		\
		   libroot-mathmore 		\
		   libroot-minuit 		\
		   libroot-mlp	 		\
		   libroot-quadp 		\
		   root-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-proofd 			\
		   root-rootd 			\
		   root-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)
PLUGINLIST	:= $(PLUGINLIST:%=%,)
EXTRALIST	:= $(EXTRALIST:%=%,)
TEMPLATES	:= $(wildcard debian/*.in)
SPKGLIST	:= $(filter-out root-doc root-common ttf-root-installer, $(PKGLIST:libroot%=libroot%$(SOVERS))) $(filter libroot%, $(PKGLIST:libroot%=libroot%-dev))
IPKGLIST	:= root-doc ttf-root-installer root-common root
DPKGLIST	:= $(SPKGLIST:%=%,) root-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)
PYTHONDIR	:= usr/lib/python$(shell python -c 'import sys; print "%d.%d" % (sys.version_info[0], sys.version_info[1])')/site-packages
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)))"

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/@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 --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 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) 
	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
	-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  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/moc_*.cxx
	-find . -name ".#*" | xargs rm -rf

	# Add here commands to clean up after the build process.
	-$(MAKE) distclean 
	build/unix/makecintdlls.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		
	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-common/$(PREFIX)/share/root/icons \
		-perm 0755 -and -not -type d | xargs chmod a-x 
	mkdir -p debian/root-doc/usr/share/lintian/overrides/
	cp debian/root-doc.overrides \
	  debian/root-doc/usr/share/lintian/overrides/root-doc

install-arch: 
	dh_testroot
	dh_clean -k -s -X$(DTMP)
	dh_installdirs -s

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

	# 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 's,Root\.ZipMode:.*,Root.ZipMode: 1,' \
		< $(DTMP)/etc/root/system.rootrc  > rootrc.tmp 
	mv rootrc.tmp $(DTMP)/etc/root/system.rootrc

	# Rename proofserv, xrdgsiproxy, and xrdpwdadmin in /usr/bin 
	# to end in `.bin' and make wrapper script in /usr/bin 
	for i in proofserv xrdgsiproxy xrdpwdadmin ; 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 


	# 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/root/fonts/LICENSE
	rm -rf $(DTMP)/$(PREFIX)/lib/root/$(SOVERS)/cint/lib/posix/a.out

	# 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

	mkdir -p $(DTMP)/$(PREFIX)/lib/root
	./debian/rules pkglists

	#	--sourcedir=$(DTMP) $(SPKGOPTS) 		
	dh_install -s						\
		--list-missing 					\
		--sourcedir=$(DTMP)	 			\
		-Xetc/root/rootd 				\
		-Xusr/include/root/config.h 			\
		-Xusr/share/man/man1/rlibmap.1 			\
		-Xusr/include/root/rootcint.pri 		\
		-Xusr/include/root/rootcintrule.pri 		\
		-Xusr/include/root/rootlibs.pri 		\
		-Xusr/include/root/TQtWidget.cw 		\
		-Xusr/share/root/fonts 				\
		-Xusr/share/doc/root				\
		-Xusr/lib/root/lib\*.so.$(SOVERS)		\
		-Xusr/share/root/icons/  			\
		-Xetc/root/daemon/				\
		-Xetc/root/proof/				\
		-Xusr/lib/root/$(SOVERS)/libAfterImage.a	

	# 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 

	# Fix some permissions 
	-find debian/libroot$(SOVERS)/$(CINTDIR) \
		-perm 755 -and -not -type d | xargs chmod a-x 
	-chmod a+x debian/libroot$(SOVERS)/$(CINTDIR)/lib/pthread/setup
	-chmod a+x debian/libroot$(SOVERS)/$(CINTDIR)/include/makehpib
	-chmod a-x \
	  debian/libroot-python-dev/$(PREFIX)/lib/root/$(SOVERS)/ROOT.py
	# Install desktop entry and icon 
	mkdir -p debian/root-bin/usr/share/applications
	cp debian/root-bin.desktop debian/root-bin/usr/share/applications
	mkdir -p debian/root-bin/usr/share/icons
	-cp build/package/debian/root-bin.png debian/root-bin/usr/share/icons

	# This is for alternatives registration, if one want's to install
        # Richard Kreckels cint package next to ROOT
	mv debian/root-bin/$(PREFIX)/bin/cint \
		debian/root-bin/$(PREFIX)/bin/cint.root
	mv debian/root-bin/$(PREFIX)/bin/makecint \
		debian/root-bin/$(PREFIX)/bin/makecint.root
	mv debian/root-bin/$(PREFIX)/share/man/man1/cint.1 \
		debian/root-bin/$(PREFIX)/share/man/man1/cint.root.1
	mv debian/root-bin/$(PREFIX)/share/man/man1/makecint.1 \
		debian/root-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
	dh_python -plibroot-python-dev \
		$(PREFIX)/lib/root/$(SOVERS) $(PREFIX)/lib/root
	dh_makeshlibs -V $(patsubst %, -p%, $(filter libroot%$(SOVERS),$(SPKGLIST)))
	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
#
