#!/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.
# modified by Gopal Narayanan <gopal@debian.org> to use
# debhelper commands

package=pgplot
npackage=pgplot5

version=$(shell expr `pwd` : '.*-\([0-9.]*\)')
version_major=$(shell expr `pwd` : '.*-\([0-9]*\).[0-9.]*')

vm=$(version_major)

curdir=$(shell pwd)
bdir=debian/builddir
packagedir=debian/$(npackage)

INSTALL =/usr/bin/install
GZIP    =/bin/gzip
INSTALL_DATA =$(INSTALL) -m644 -o root -g root
INSTALL_STRIP =$(INSTALL) -m755 -o root -g root -s
GZIP_DATA =$(GZIP) -9n
INSTALL_DIR = $(INSTALL) -d -m755 -o root -g root

# Decide if changes for 64 bit systems need to be made
#
64-BIT_BUILD_STAMP =
64-BIT_CLEAN_STAMP =
ifeq (64,$(shell dpkg-architecture -qDEB_BUILD_ARCH_BITS))
        64-BIT_BUILD_STAMP = 64-bit-patch-stamp
        64-BIT_CLEAN_STAMP = 64-bit-clean-stamp
endif

# Construct the library dependency entry for shlibs
#
shl1=lib$(package)  $(vm) pgplot5 (>= $(version))

# Restore 32-bit files
#
64-bit-clean-stamp:
	mv debian/gidriv.f drivers/gidriv.f
	mv debian/ppdriv.f drivers/ppdriv.f
	mv debian/wddriv.f drivers/wddriv.f
	rm 64-bit-patch-stamp

# Move 64-bit-safe files into position
#
64-bit-patch-stamp:
	mv drivers/gidriv.f debian/gidriv.f
	cp debian/gidriv_64.f drivers/gidriv.f
	mv drivers/ppdriv.f debian/ppdriv.f
	cp debian/ppdriv_64.f drivers/ppdriv.f
	mv drivers/wddriv.f debian/wddriv.f
	cp debian/wddriv_64.f drivers/wddriv.f
	touch 64-bit-patch-stamp

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp: $(64-BIT_BUILD_STAMP)
	dh_testdir
	# create the build directory and copy configuration files to it.
	install -d  $(bdir)
	cp debian/drivers.list.debian $(bdir)/drivers.list
	cp sys_linux/g77_gcc.conf sys_linux/g77_gcc.conf.orig
	cp debian/g77_gcc.conf.debian sys_linux/g77_gcc.conf
	cd $(bdir); $(curdir)/makemake $(curdir)  linux g77_gcc
#       changeperl no longer necessary - upstream uses /usr/bin/perl now
#	perl debian/changeperl
	cd $(bdir);make FFLAGC="-u -Wall -O2 -fallow-argument-mismatch" CFLAGC="-Wall -DPG_PPU -O3" all;make clean;make FFLAGC="-u -Wall -O2" CFLAGC="-Wall -DPG_PPU -O3" cpg;make pgplot.html;make pgplot-routines.tex
#	-cd $(bdir); rm *.o
	cd $(bdir) ; make clean; make  FFLAGC="-u -Wall  -O2 -fPIC -fallow-argument-mismatch" CFLAGC="-Wall -fPIC -D_REENTRANT -DPG_PPU -O3" VM="$(version_major)"  shared cpg-shared
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	-rm -rf static shared
	-rm -f build-stamp install-stamp
	-rm -rf $(bdir)
#	-make clean
#	-rm -f `find . -name "*~"`
#	-rm -rf debian/tmp `find debian/* -type d ! -name CVS` debian/files* core
	-rm -f debian/*substvars
	dh_clean

install: install-stamp
install-stamp: build-stamp $(64-BIT_CLEAN_STAMP)
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	$(INSTALL_DATA) $(bdir)/libpgplot.a  $(packagedir)/usr/lib/
	$(INSTALL_DATA) $(bdir)/libcpgplot.a  $(packagedir)/usr/lib/
	$(INSTALL_DATA) $(bdir)/libpgplot.so  $(packagedir)/usr/lib/libpgplot.so.$(version)
	$(INSTALL_DATA) $(bdir)/libcpgplot.so  $(packagedir)/usr/lib/libcpgplot.so.$(version)
	strip --strip-unneeded $(packagedir)/usr/lib/libpgplot.so.$(version)
	strip --strip-unneeded $(packagedir)/usr/lib/libcpgplot.so.$(version)
	ln -s lib$(package).so.$(version) $(packagedir)/usr/lib/lib$(package).so.$(version_major)
	ln -s libcpgplot.so.$(version) $(packagedir)/usr/lib/libcpgplot.so.$(version_major)
	ln -s lib$(package).so.$(version_major) $(packagedir)/usr/lib/lib$(package).so
	ln -s libcpgplot.so.$(version_major) $(packagedir)/usr/lib/libcpgplot.so
#	chmod 644 $(packagedir)/usr/lib/$(npackage)/*
	$(INSTALL_DATA) $(bdir)/cpgplot.h $(packagedir)/usr/include/
#	$(INSTALL_STRIP)  $(bdir)/pgdisp $(packagedir)/usr/bin/
#	install  $(bdir)/pgbind $(packagedir)/usr/bin/
	$(INSTALL_STRIP)  $(bdir)/pgxwin_server  $(packagedir)/usr/bin/
#	cd $(packagedir)/usr/bin ; strip --strip-unneeded pgdisp  pgxwin_server
	$(INSTALL_DATA) $(bdir)/grexec.f $(packagedir)/usr/lib/$(npackage)
	$(INSTALL_DATA) $(bdir)/grfont.dat $(packagedir)/usr/lib/$(npackage)
	$(INSTALL_DATA) $(bdir)/grexec.f $(packagedir)/usr/lib/$(npackage)
	$(INSTALL_DATA) $(bdir)/rgb.txt $(packagedir)/usr/lib/$(npackage)
	$(INSTALL_DATA) $(bdir)/grpckg1.inc $(packagedir)/usr/lib/$(npackage)

	#dh_movefiles
	touch install-stamp

binary-indep: build install
#	$(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: build install
#     dh_testversion
	dh_testdir -a
	dh_testroot -a
#	-rm -rf $(packagedir)
#	install -d  $(packagedir)/DEBIAN

# Insert appropriate lines into shlibs file
#
#	echo -e '$(shl1)\n' >$(packagedir)/DEBIAN/shlibs

#	install -d $(packagedir)/usr/doc/$(npackage)/examples
#	install -d $(packagedir)/usr/lib/$(npackage)
#	install -d $(packagedir)/usr/bin
#	cd $(packagedir) && install -d `cat ../dirs`

	$(INSTALL_DATA) $(bdir)/pgplot.doc $(packagedir)/usr/share/doc/$(npackage)
	$(GZIP_DATA) $(packagedir)/usr/share/doc/$(npackage)/pgplot.doc
#	cp  pgdispd/aaaread.me  $(packagedir)/usr/share/doc/$(npackage)/pgdisp.txt
#	$(GZIP_DATA) $(packagedir)/usr/share/doc/$(npackage)/pgdisp.txt
	$(INSTALL_DATA) $(bdir)/drivers.list $(packagedir)/usr/share/doc/$(npackage)
	$(GZIP_DATA) $(packagedir)/usr/share/doc/$(npackage)/drivers.list
	$(INSTALL_DATA) $(bdir)/pgplot-routines.tex $(packagedir)/usr/share/doc/$(npackage)
	$(GZIP_DATA) $(packagedir)/usr/share/doc/$(npackage)/pgplot-routines.tex
	$(INSTALL_DATA) $(bdir)/pgplot.html $(packagedir)/usr/share/doc/$(npackage)
	$(INSTALL_DATA) cpg/cpgplot.doc $(packagedir)/usr/share/doc/$(npackage)
	$(GZIP_DATA) $(packagedir)/usr/share/doc/$(npackage)/cpgplot.doc
#	$(INSTALL_DATA) cpg/cpgdemo.c $(packagedir)/usr/share/doc/$(npackage)/examples
#	$(INSTALL_DATA) debian/pgplot.3x $(packagedir)/usr/man/man3
#	gzip -9v $(packagedir)/usr/man/man3/*
	dh_installman
	dh_installdocs
	$(INSTALL_DATA) aaaread.me $(packagedir)/usr/share/doc/$(npackage)
#	$(INSTALL_DATA) install-unix.txt $(packagedir)/usr/share/doc/$(npackage)
	$(INSTALL_DATA) ver5*.txt $(packagedir)/usr/share/doc/$(npackage)
	dh_installexamples examples/*.f
	dh_installexamples cpg/cpgdemo.c
#	cp examples/* $(packagedir)/usr/share/doc/$(npackage)/examples


# Install installation scripts
#
#	cp debian/postinst $(packagedir)/DEBIAN/.
#	chmod +x $(packagedir)/DEBIAN/postinst

	dh_installchangelogs
	$(INSTALL_DATA) debian/copyright $(packagedir)/usr/share/doc/$(npackage)
	$(INSTALL_DATA) debian/changelog $(packagedir)/usr/share/doc/$(npackage)/changelog.Debian
	$(GZIP_DATA) $(packagedir)/usr/share/doc/$(npackage)/changelog.Debian
	dh_strip -a $(packagedir)/usr/bin/*
	dh_makeshlibs -a
	dh_link -a
#	dh_dhelp
	dh_compress -a
	dh_fixperms -a
#	dh_suidregister -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

#	cp debian/README.debian $(packagedir)/usr/doc/$(npackage)
#	cp debian/changelog $(packagedir)/usr/doc/$(npackage)/changelog.Debian
#	gzip -9v $(packagedir)/usr/doc/$(npackage)/changelog.Debian
#	cp debian/copyright $(packagedir)/usr/doc/$(npackage)
#	dpkg-shlibdeps $(packagedir)/usr/lib/libpgplot.so.$(version)
#	dpkg-gencontrol
#	chown -R root.root $(packagedir)
#	chmod -R go=rX $(packagedir)
#	dpkg --build $(packagedir) ..

#define checkdir
#	test -f debian/rules
#endef

binary: binary-indep binary-arch

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

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