#!/usr/bin/make -f
# Made with the aid of dh_make, by Craig Small
# Sample debian/rules that uses debhelper. GNU copyright 1997 by Joey Hess.
# Also some stuff taken from debmake scripts, by Cristopt Lameter.

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

#version=$(shell expr `pwd` : '.*-\([0-9.]*\)')
#version := $(shell grep ^VERSION Makefile | cut -d" " -f3)
version := $(shell perl -ne 'if (/AC_INIT\(gri, ([0-9.]+)\)/){print $$1}' configure.ac)
somefutureversion=2.14.0

CXXFLAGS = -O2 -Wall
ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
CXXFLAGS += -g
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
STRIPIT=1
endif

ARCH := $(shell dpkg --print-architecture | perl -pe chop)

build: build-indep build-arch

# Build architecture-dependent files here (no need to be root).
build-arch: debian/copyright gri
	cd doc; make refcard.ps cmdrefcard.ps info

gri:    Makefile
	dh_testdir
ifeq ($(ARCH),m68k)
	cd src; make CXXFLAGS="-O0 -Wall" rpncalc.o
endif
	cd src; make CXXFLAGS=${CXXFLAGS}

clean:
	dh_testdir
	dh_testroot
	[ ! -f Makefile ] || $(MAKE) distclean
	mv doc/FAQ.html doc/FAQ.html.KEEP
	-rm -f config.log config.status debian/Makefile doc/*.html
	-rm -f doc/gri.info* doc/*.dvi doc/*.ps doc/*.pdf doc/*.log doc/html
	-rm src/gri.cmd src/startup.msg-tmp
	mv doc/FAQ.html.KEEP doc/FAQ.html

	dh_clean
	rm -fR debian/gri-$(version)static
	rm -f  debian/README.Debian debian/gri.1 debian/copyright
	rm -f doc/gri_unpage.1 doc/gri_merge.1
	chmod u+w debian/changelog

install: gri
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	cd src; make install_linux_debian debian=`pwd`/../debian/gri

debian/copyright:
	sed -e 's/VSN/$(version)/g' \
		debian/copyright.template > debian/copyright
	sed -e 's/LATERVSN/$(somefutureversion)/g;s/VSN/$(version)/g' \
		debian/gri.man > debian/gri.1
	sed -e 's/LATERVSN/$(somefutureversion)/g;s/VSN/$(version)/g' \
		debian/README.Debian.template > debian/README.Debian
	cp doc/gri_unpage.1-skel doc/gri_unpage.1
	cp doc/gri_merge.1-skel doc/gri_merge.1

Makefile:
# To test on g++-3.0 :
#	CC=gcc-3.0 CXX=g++-3.0 ./configure --prefix=/usr
	./configure --prefix=/usr --enable-linux_debian

# Build architecture-independent files here (no need to be root).
build-indep: debian/copyright Makefile
	cd doc; make gri.pdf html

# Install (as root) architecture-independent files here.
binary-indep: build-indep
	dh_testdir -i
	dh_testroot -i
	install -d debian/gri-el/usr/share/emacs/site-lisp/gri-el
	install -m 644 src/gri-mode.el debian/gri-el/usr/share/emacs/site-lisp/gri-el
	install -d debian/gri-pdf-doc/usr/share/doc/gri
	cd doc; make gri.pdf
	install -m 644 doc/gri.pdf debian/gri-pdf-doc/usr/share/doc/gri
	cd doc; make html-install DESTDIR=`pwd`/../debian/gri-html-doc/
	rm -f debian/*debhelper
	dh_installdocs -i -A README debian/README.Debian
	dh_installexamples -i
	dh_installemacsen -i
	dh_installchangelogs -i 
	dh_compress -i -X.jpg -Xlogo.gri
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Install (as root) architecture-dependent files here.
binary-arch: build-arch install
	dh_testdir -a
	dh_testroot -a
	(cd debian/gri/usr/share/gri/$(version) ; ln -fs ../../common-licenses/GPL license.txt)
	rm -f debian/*debhelper
	dh_installdocs -a README doc/refcard.ps doc/cmdrefcard.ps
	dh_installexamples -a
	dh_installemacsen -a
	dh_installinfo doc/gri.info*
	dh_installman debian/gri.1 doc/gri_unpage.1 doc/gri_merge.1
	(cd debian/gri/usr/share/man/man1 ; ln -fs gri.1.gz gri-$(version).1.gz)
	dh_installchangelogs -a 
        ifdef STRIPIT
		dh_strip -a
        endif
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

# This is a target that developers won't use.  I use it to create
# non-Debian Gri packages that contain a specific version of Gri for use
# alongside other (newer) versions of Gri.  
# See README.Debian.unofficial
binary-arch-unofficial:    debian/copyright build-arch
	rm -f debian/*debhelper
	rm -rf debian/gri-$(version) debian/gri-$(version)static
	-cp debian/control debian/control.real 
	sed -e 's/VSN/$(version)/g' debian/control.unofficial > debian/control 
	-cp debian/README.Debian debian/README.Debian.real 
	-cp debian/README.Debian.unofficial debian/README.Debian
	cd src; make install_linux_debian_grionly debian=../debian/gri-$(version)
	(cd debian/gri-$(version)/usr/share/gri/$(version) ; ln -s ../../common-licenses/GPL license.txt)
	cd src;  mv gri gri.real
	-rm debian/substvars
	make LIBS="-lm  -L/usr/lib/netcdf -lnetcdf -static"
	cd src; make install_linux_debian_grionly debian=../debian/gri-$(version)static
	(cd debian/gri-$(version)static/usr/share/gri/$(version) ; ln -s ../../common-licenses/GPL license.txt)
	cd src; mv gri.real gri
	dh_testdir -a
	dh_testroot -a
	dh_installdocs -a -A README debian/README.Debian
	install -d debian/gri-$(version)/usr/share/man/man1
	install debian/gri.1 debian/gri-$(version)/usr/share/man/man1/gri-$(version).1
	install -d debian/gri-$(version)static/usr/share/man/man1
	install debian/gri.1 debian/gri-$(version)static/usr/share/man/man1/gri-$(version).1
#	dh_installmanpages -a doc/gri*.1 debian/gri.1
#	-rm debian/gri-$(version).1
	dh_installchangelogs -a 
        ifdef STRIPIT
		dh_strip -a
        endif
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a
	-mv debian/control.real debian/control          
	-mv debian/README.Debian.real debian/README.Debian

source diff:                                                                  
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

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