#!/usr/bin/make -f
# debian/rules file for the Debian GNU/Linux r-base package
# Copyright 1997-2001 by Douglas M. Bates <bates@stat.wisc.edu>

package		= r-base
pdfpackage	= r-doc-pdf
gnomepackage	= r-gnome

debbase		:= $(shell pwd)/debian
debtmp		:= $(debbase)/tmp
debdoc		:= $(debtmp)/usr/share/doc/$(package)

# set this to 0 if you don't want Gnome packages
gnome		= 1
ifeq ($(gnome),0)
gnomeconfig 	= --without-gnome
else
gnomeconfig	= --with-gnome
endif

# edd 10 Feb 98	See section 4.1 of Policy Manual
arch		:= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

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

# This is the debhelper compatability version to use.
export DH_COMPAT=1

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

# edd 15 Aug 2001: default to blas, atlas can override it 
atlas = --with-blas=blas

build: build-stamp configure-stamp make-stamp
build-stamp: configure make
	touch build-stamp

configure: configure-stamp
configure-stamp:	
	dh_testdir
	R_UNZIPCMD=/usr/bin/miniunzip R_ZIPCMD=/usr/bin/minizip \
	./configure --prefix=/usr		     \
		    --with-g77			     \
		    --with-tcltk		     \
		    --mandir=$(debtmp)/usr/share/man \
		    $(atlas)                         \
		    $(gnomeconfig)		     \
		    --enable-R-profiling             \
		    --enable-R-shlib                 \
		    --host $(arch)
	touch configure-stamp

make: make-stamp
make-stamp:
	make 
	make check
	make help
	make html
	make info 
	make pdf
	touch make-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp make-stamp
	-make distclean
	-rm -f `find . -name "*~"`
	-rm -rf debian/tmp debian/files* core debian/substvars
	-rm -f doc/html/function.html doc/html/packages.html
	-rm -f modules/*.so
	dh_clean

install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Add here commands to install the package into debian/tmp.
	#$(MAKE) prefix=`pwd`/debian/tmp/usr install
	make 
	make check 
	make install		prefix=`pwd`/debian/tmp/usr
	make install-pdf	prefix=`pwd`/debian/tmp/usr
	sed -e '/^R_HOME_DIR=.*/s::R_HOME_DIR=/usr/lib/R:' \
	  < bin/R > debian/tmp/usr/bin/R
	sed -e '/^R_HOME_DIR=.*/s::R_HOME_DIR=/usr/lib/R:' \
	  < bin/R > debian/tmp/usr/lib/R/bin/R
	# remove unneeded license file
	rm -v $(debtmp)/usr/lib/R/COPYING  

	# move the pdf files into the arch-indep. package
	dh_movefiles	-p$(pdfpackage)	usr/lib/R/doc/manual/*.pdf 

ifneq ($(gnome),0)
	# move the GNOME version to the r-gnome package
	dh_movefiles	-p$(gnomepackage) usr/lib/R/modules/R_gnome.so
endif

# Build architecture-independent files here.
# Pass -i to all debhelper commands in this target to reduce clutter.
binary-indep: DH_OPTIONS=-i
binary-indep: build install
	# Need this version of debhelper for DH_OPTIONS to work.
	dh_testversion 1.1.17
	dh_testdir
	dh_testroot
#	dh_installdebconf
	dh_installdocs
	dh_installexamples
	dh_installmenu
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
	dh_installcron
#	dh_installmanpages
	dh_installinfo
#	dh_undocumented
	dh_installchangelogs	NEWS 
	dh_link
	dh_compress
	dh_fixperms
	# You may want to make some executables suid here.
#	dh_suidregister
	dh_installdeb
#	dh_perl
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture-dependent files here.
# Pass -a to all debhelper commands in this target to reduce clutter.
binary-arch: DH_OPTIONS=-a
binary-arch: build install
	# Need this version of debhelper for DH_OPTIONS to work.
	dh_testversion 1.1.17
	dh_testdir
	dh_testroot
#	dh_installdebconf
	dh_installdocs		BUGS Y2K 
	dh_installdocs 		-p$(package) 	debian/README.Atlas
	dh_installexamples
	dh_installmenu
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
	dh_installcron
	dh_installmanpages	-N$(gnomepackage) -p$(package) getline.3
	dh_installinfo		-n doc/manual/R-*.info*
	(cd $(debdoc)/doc && ln -s ../../../../lib/R/doc/html .)
	(cd $(debdoc)/doc && ln -s ../../../../lib/R/doc/manual .)
	(cd $(debdoc) && ln -s ../../../lib/R/library .)
#	install lintian "silencer"
	install -p -m 0644  debian/$(package).lintian \
                        $(debtmp)/usr/share/lintian/overrides/$(package)
#	dh_undocumented
	dh_installchangelogs	NEWS 
	dh_strip
	dh_link
	dh_compress
	dh_fixperms
	# You may want to make some executables suid here.
#	dh_suidregister
	dh_installdeb
#	dh_makeshlibs
#	dh_perl
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums		-X'usr/bin/R'  -XLibIndex -Xindex.txt \
					-Xfunction.html -Xpackages.html
	dh_builddeb

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