#!/usr/bin/make -f
# Made with the aid of debmake, by Christoph Lameter,
# based on the sample debian/rules file for GNU hello by Ian Jackson.

######################################################################
# Select EXT and FC variables before building the package 
#
#    for G95 Fortran Compiler
#
export EXT=g95

export FFLAGS=-O3 -mtune=i386
#export FFLAGS=-O

#    for Fujitsu Fortran Compiler
#
#export EXT=ffc
#export EXT=ffc3
#export EXT=ffc4
#export EXT=ffc5
#export FC=frt
#
#export FFLAGS=-O
#export FFLAGS=-Kfast
#export FFLAGS=-Kfast -v95d,95o,95s

#
#    for Intel Fortran Compiler
#
#export EXT=ifc
#export EXT=ifc6
#export EXT=ifc7
#export EXT=ifc7.1
#export EXT=ifc8
#export EXT=ifc8.1
#
#export FC=ifc
#export FC=ifort

#export FFLAGS=-fast

######################################################################
# Select MATRIXLIB for eigenvalue problem library subroutine. 
#
#   for SSL II 
#
#export MATRIX=ssl2-
#WITHMATRIX=--with-ssl2=/usr/FFC/lib/libssl2.a
#export LDFLAGS=  	# ssl2 correctly works alone (not needs lapack etc.)
#export LDLIBS=		# ssl2 correctly works alone (not needs lapack etc.)

#
#   for LAPACK
#
export MATRIX=lapack-
#LAPACKLIB=/usr/lib/liblapack-$(EXT).a
WITHMATRIX=--with-lapack=$(LAPACKLIB)
#WITHMATRIX=--with-lapack=/usr/lib/liblapack-$(EXT).a
#export LDFLAGS=-L/usr/lib/gcc-lib/i386-linux/2.95.4  	# for LAPACK
#export LDFLAGS=-L/usr/lib/gcc-lib/i486-linux/3.3.5  	# for LAPACK
#export LDLIBS=-lblas -lg2c -lgcc			# for LAPACK
#export LDLIBS=-lblas-2 -lg2c -lgcc			# for LAPACK

#
#   not using eigenvalue problem routines
#
#export MATRIX=
#WITHMATRIX=
#export LDFLAGS=
#export LDLIBS=


#
#   for other libraries
#
#ISPACKLIB=/usr/lib/libisp-$(FFTW)$(EXT).a
#NETCDFLIB=/usr/lib/libnetcdf-$(EXT).a
#GT4F90IOLIB=/usr/lib/gt4f90io-$(EXT)/lib/libgt4f90io.a
#
######################################################################
# Select FFTW library 
#
#FFTW=fftw2-
#WITHFFTW=--with-fftw=/usr/lib/libfftw-no2underscore.a --with-rfftw=/usr/lib/librfftw-no2underscore.a
FFTW=
WITHFFTW=

######################################################################
# Select PRIORITY for the update-altenatives command. 
# This parameter is used for automatic generation of *.postinst script.
#
export PRIORITY=10
#export PRIORITY=20
#export PRIORITY=30
#export PRIORITY=40
#export PRIORITY=50


export PACKAGEEXT=$(FFTW)$(MATRIX)$(EXT)
package=spml-$(PACKAGEEXT)
docpackage=spml-doc

build:
	$(checkdir)
#	cp debian/control.$(PACKAGEEXT) debian/control
	(cd debian ; sh ./spml-postinst-gen.sh ; sh ./spml-prerm-gen.sh)
	./configure --with-ispack=$(ISPACKLIB) --with-netcdf=$(NETCDFLIB) --with-gt4f90io=$(GT4F90IOLIB) $(WITHMATRIX) $(WITHFFTW) --prefix=/usr/lib/spml-$(PACKAGEEXT)
	cd src ; $(MAKE)
	cd spmfrt ; \
		mv spmfrtgen.sh spmfrtgen.sh_org ;\
		mv spmconfiggen.sh spmconfiggen.sh_org ;\
		sed -e "s|fc=\"\`which \\$$.FC.*$$|fc=/usr/bin/g95|" \
		    -e "s|fflags=.*$$|fflags=\"-I/usr/lib/spml-lapack-g95/include -I/usr/lib/gt4f90io-g95/include -O3 -mtune=i386\"|" \
		    -e "s|ldflags=.*$$|ldflags=\"-L/usr/lib/spml-lapack-g95/lib -L/usr/lib/gt4f90io-g95/lib -L/usr/lib\"|" \
			spmfrtgen.sh_org > spmfrtgen.sh ;\
		sed -e "s|fc=\"\`which \\$$.FC.*$$|fc=/usr/bin/g95|" \
		    -e "s|fflags=.*$$|fflags=\"-I/usr/lib/spml-lapack-g95/include -I/usr/lib/gt4f90io-g95/include -O3 -mtune=i386\"|" \
		    -e "s|ldflags=.*$$|ldflags=\"-L/usr/lib/spml-lapack-g95/lib -L/usr/lib/gt4f90io-g95/lib -L/usr/lib\"|" \
			\
			spmconfiggen.sh_org > spmconfiggen.sh ;\
		$(MAKE) ;\
		mv spmfrtgen.sh_org spmfrtgen.sh ;\
		mv spmconfiggen.sh_org spmconfiggen.sh ;\
	touch build

clean:
	$(checkdir)
	-rm -f build
	-$(MAKE) distclean
	-rm -f `find . -name "*~"`
	-rm -f `find . -name "*.bak"`
	-rm -rf debian/tmp debian/tmp-doc debian/files* core debian/substvars
	-rm -rf debian/*.debhelper
	-rm -rf debian/*.postinst
	-rm -rf debian/*.prerm

binary-indep: checkroot build
#	$(checkdir)
## There are no architecture-independent files to be uploaded
## generated by this package.  If there were any they would be
## made here.
##	install -d debian/tmp-doc/usr/share/doc/spml-doc/
#	dh_installdocs -p$(docpackage) -Pdebian/tmp-doc html test demo supplemental MEMO README
#	dh_installchangelogs -p$(docpackage) -Pdebian/tmp-doc CHANGELOG 
#	dh_installdeb -p$(docpackage) -Pdebian/tmp-doc
#	dpkg-gencontrol -isp -p$(docpackage) -Pdebian/tmp-doc
#	chown -R root.root debian/tmp-doc
#	chmod -R go=rX debian/tmp-doc
#	dpkg --build debian/tmp-doc ..

binary-arch: checkroot build
	$(checkdir)
	-rm -rf debian/tmp
	install -d debian/tmp
	cd debian/tmp && install -d `cat ../dirs`
	$(MAKE) install DESTDIR=`pwd`/debian/tmp
	dh_link usr/lib/$(package)/bin/spmfrt   usr/bin/spmfrt.$(PACKAGEEXT)
	dh_link usr/lib/$(package)/bin/spmconfig   usr/bin/spmconfig.$(PACKAGEEXT)
# Must have debmake installed for this to work. Otherwise please copy
# /usr/bin/debstd into the debian directory and change debstd to debian/debstd
	#debstd 
	dh_installdocs -p$(package) -Pdebian/tmp 
	dh_strip -p$(package) -Pdebian/tmp
	dh_compress -p$(package) -Pdebian/tmp
	dh_fixperms -p$(package) -Pdebian/tmp
	dh_installdeb -p$(package) -Pdebian/tmp
	dpkg-gencontrol -isp -p$(package) -Pdebian/tmp
	chown -R root.root debian/tmp
	chmod -R go=rX debian/tmp
	dpkg --build debian/tmp ..

define checkdir
	test -f debian/rules
endef

binary: binary-arch binary-indep 

checkroot:
	$(checkdir)
	test root = "`whoami`"

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