#!/usr/bin/make -f
#-*- makefile -*-
# Made with the aid of dh_make, by Craig Small
# Sample debian/rules that uses debhelper. GNU copyright 1997 by Joey Hess.
# Some lines taken from debmake, by Christoph Lameter.

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

#FIXME remove when converting to dh
export SOURCE_DATE_EPOCH = $(shell date -d "$$(dpkg-parsechangelog -SDate)" +%s)

NO_STRIP:=

SAVES:=$(addprefix debian/save/,aclocal.m4 configure interfaces/emacs/imaxima/stamp-vti\
	 interfaces/emacs/imaxima/imaxima.info interfaces/emacs/imaxima/version.texi\
	 src/lisp\
	 \
	 admin/Makefile.in crosscompile-windows/Makefile.in crosscompile-windows/wxwidgets/Makefile.in\
	 demo/Makefile.in doc/emaxima/Makefile.in doc/info/de/Makefile.in doc/info/de.utf8/Makefile.in\
	 doc/info/es/Makefile.in doc/info/es.utf8/Makefile.in doc/info/Makefile.in doc/info/pt_BR/Makefile.in\
	 doc/info/pt_BR.utf8/Makefile.in doc/info/pt/Makefile.in doc/info/pt.utf8/Makefile.in doc/intromax/Makefile.in\
	 doc/Makefile.in doc/man/Makefile.in doc/share/Makefile.in interfaces/emacs/emaxima/Makefile.in\
	 interfaces/emacs/imaxima/Makefile.in interfaces/emacs/Makefile.in interfaces/emacs/misc/Makefile.in\
	 interfaces/Makefile.in interfaces/xmaxima/doc/figures/Makefile.in interfaces/xmaxima/doc/Makefile.in\
	 interfaces/xmaxima/Makefile.in interfaces/xmaxima/msgs/Makefile.in interfaces/xmaxima/win32/Makefile.in\
	 lisp-utils/Makefile.in locale/Makefile.in Makefile.in plotting/Makefile.in share/contrib/integration/Makefile.in\
	 share/contrib/Makefile.in share/contrib/maxima-odesolve/Makefile.in share/draw/Makefile.in\
	 share/logic/Makefile.in share/Makefile.in src/autoconf-variables.lisp src/Makefile.in\
	 src/share-subdirs.lisp tests/Makefile.in\
	 \
	doc/info/figures/Makefile.in doc/info/maxima-index.lisp doc/info/maxima.info doc/info/maxima.info-1 \
	doc/info/maxima.info-2 doc/info/maxima.info-3 \
	)

debian/save/%: %
	mkdir -p $(@D)
	[ -e $@ ] || cp $< $@

debian/save: $(SAVES)

restore: 
	! [ -d debian/save ] || ( cd debian/save && for i in $$(find -type f); do cp $$i ../../$$i; done )
	rm -rf debian/save

MVERS:=$(shell head -n 1 debian/changelog | cut -f2 -d\  | tr -d '()' | cut -f1 -d-)
build: build-arch build-indep
build-arch: debian/save build-stamp
build-indep: debian/save build-stamp
build-stamp:
	dh_testdir

	aclocal
	automake

	echo '(progn (setq si::*optimize-maximum-pages* nil)(si::save-system "gcl"))' | GCL_ANSI=t gcl

	git_found=false PATH=$$(pwd):$$PATH GCL_ANSI=t ./configure --enable-gcl $$(gcl -batch -eval '(progn #-native-reloc(princ "--enable-gcl-alt-link"))') \
		--enable-sys-proclaim \
		--prefix=/usr \
		--libexec=/usr/lib \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info 

	cp debian/favicon.ico doc/info/figures/

	rm -f src/sys-proclaim.lisp

	PATH=$$(pwd):$$PATH GCL_ANSI=t $(MAKE)

	echo ':lisp (setq maxima::*maxima-started* nil si::*optimize-maximum-pages* t si::*code-block-reserve* (make-array 10000000 :element-type (quote character) :static t))(si::sgc-on nil)(si::gbc t)(si::save-system "foo")' | ./maxima-local && mv foo src/binary-gcl/maxima


	chmod +x ./maxima-local
	touch tests/tests-gcl.log
	./maxima-local -g --lisp=gcl --batch-string="run_testsuite();"  >tmp 2>&1 & \
		j=$$! ; echo Waiting on pid $$j ; \
		tail -f --pid=$$j --retry tests/tests-gcl.log tmp & wait $$j

	touch tmp
	cat tmp >debian/test_results.out

	grep -q "No unexpected errors found." debian/test_results.out

	rm -f tmp

	echo ':lisp (setq si::*readline-prefix* "maxima::$$")(setq maxima::*maxima-started* nil maxima::*maxima-index-dir* "/usr/share/doc/maxima/info")(si::save-system "foo")' | ./maxima-local && mv foo src/binary-gcl/maxima

	touch build-stamp

BOOK:=debian/maximabook-19-Sept-2004.pdf

clean: #restore
	dh_testdir
	dh_testroot

	debian/rules restore
	rm -rf $(BOOK) src/binary-gcl binary gcl src/sys-proclaim.lisp tests/tests-gcl.log
	rm -rf admin/Makefile config.log config.status crosscompile-windows/Makefile\
		crosscompile-windows/wxwidgets/Makefile demo/Makefile doc/emaxima/Makefile\
		doc/info/de/Makefile doc/info/de.utf8/Makefile doc/info/es/include-maxima.texi\
		doc/info/es/Makefile doc/info/es.utf8/Makefile\
		doc/info/Makefile doc/info/pt_BR/Makefile doc/info/pt_BR.utf8/Makefile\
		doc/info/pt/include-maxima.texi doc/info/pt/Makefile doc/info/pt.utf8/Makefile\
		doc/intromax/Makefile doc/Makefile doc/man/Makefile doc/man/maxima.1 doc/man/ru/maxima.1\
		doc/share/Makefile interfaces/emacs/emaxima/Makefile interfaces/emacs/imaxima/Makefile\
		interfaces/emacs/Makefile interfaces/emacs/misc/Makefile interfaces/Makefile\
		interfaces/xmaxima/autoconf-variables.tcl interfaces/xmaxima/doc/figures/Makefile\
		interfaces/xmaxima/doc/Makefile interfaces/xmaxima/Makefile interfaces/xmaxima/msgs/Makefile\
		interfaces/xmaxima/Tkmaxima/Header.tcl interfaces/xmaxima/win32/Makefile interfaces/xmaxima/xmaxima\
		lisp-utils/Makefile locale/Makefile Makefile maxima.iss maxima-local maxima.spec plotting/Makefile\
		plotting/mgnuplot share/contrib/integration/Makefile share/contrib/Makefile\
		share/contrib/maxima-odesolve/Makefile share/draw/Makefile share/logic/Makefile share/Makefile\
		src/gcl-depends.mk src/Makefile src/maxima src/maxima.bat src/rmaxima tests/Makefile xmaxima-local\
		interfaces/xmaxima/Tkmaxima/tclIndex
	rm -rf bin/interfaces bin/doc bin/src bin/xmaxima
	rm -f doc/info/figures/Makefile
	rm -f tmp
	rm -f share/contrib/maxima-odesolve/maxima-odesolve-index.lisp

	rm -f $(INSTALLS)
	rm -f debian/ChangeLog

	dh_clean


LOC_DOC=<a href=file://usr/share/doc/maxima/html/maxima_toc.html> (local copy) </a>

debian/%.install: debian/%.install.in
	cat $< | sed "s,@MVERS@,$(MVERS),g" >$@

INSTALLS:=$(shell ls -1 debian/*.install.in | sed 's,\.in$$,,1')

debian/%.pdf: debian/%.pdf.uu
	cat $< | uudecode >$@

CHANGELOGS:=$(shell ls -1 ChangeLog*)
debian/ChangeLog: $(CHANGELOGS)
	for i in $^; do echo "==========">>$@;echo "File: $$i" >>$@;echo "==========">>$@;cat $$i >>$@; done

install: install-stamp
install-stamp: build-stamp $(BOOK) $(INSTALLS) debian/ChangeLog
	dh_testdir
	dh_testroot
	dh_prep

	$(MAKE) install DESTDIR=$$(pwd)/debian/tmp INSTALL="/usr/bin/install -D"

	mkdir -p $$(pwd)/debian/tmp/usr/share/doc/maxima-doc
	cp -a debian/tmp/usr/share/maxima/$(MVERS)/doc/* \
		$$(pwd)/debian/tmp/usr/share/doc/maxima-doc 
	rm -rf debian/tmp/usr/share/maxima/$(MVERS)/doc
	ln -snf ../../doc/maxima-doc debian/tmp/usr/share/maxima/$(MVERS)/doc

	mkdir -p $$(pwd)/debian/tmp/usr/share/doc/xmaxima
	cp -a debian/tmp/usr/share/maxima/$(MVERS)/xmaxima/* \
		$$(pwd)/debian/tmp/usr/share/doc/xmaxima 
	rm -rf debian/tmp/usr/share/maxima/$(MVERS)/xmaxima
	ln -snf ../../doc/xmaxima debian/tmp/usr/share/maxima/$(MVERS)/xmaxima

	mkdir -p debian/tmp/usr/share/emacs/site-lisp/maxima
	cp -a debian/tmp/usr/share/maxima/$(MVERS)/emacs/* \
		debian/tmp/usr/share/emacs/site-lisp/maxima
	rm debian/tmp/usr/share/emacs/site-lisp/maxima/dbl.el
	mkdir -p debian/tmp/usr/share/texmf/tex/latex
	mv debian/tmp/usr/share/emacs/site-lisp/maxima/*sty \
		debian/tmp/usr/share/texmf/tex/latex
	rm -rf debian/tmp/usr/share/maxima/$(MVERS)/emacs
	ln -snf ../../emacs/site-lisp/maxima debian/tmp/usr/share/maxima/$(MVERS)/emacs

	for i in debian/tmp/usr/bin/xmaxima \
		debian/tmp/usr/lib/maxima/$(MVERS)/mgnuplot ; do \
		cat $$i | sed  -e 's,^#!/bin/sh,#!/usr/bin/wish,1' \
			-e 's,^exec wish,#exec wish,1' \
			-e 's,\$${prefix}/share,/usr/share,1' \
                        -e 's,[Nn]etscape,sensible-browser,g' >tmp && chmod 755 tmp && \
			mv tmp $$i ; done

	mkdir -p debian/tmp/usr/share/applications
	cp debian/xmaxima.desktop debian/tmp/usr/share/applications
	mkdir -p debian/tmp/usr/share/pixmaps
	ln -s /usr/share/doc/xmaxima/maxima-icon.png debian/tmp/usr/share/pixmaps/maxima-icon.png

	cp $(BOOK) debian/tmp/usr/share/doc/maxima-doc

	cd debian/tmp/usr/share/info && \
		for i in $$(find -name "*.info"); do if ! grep -q START-INFO-DIR-ENTRY $$i ; then k=$$(basename $$i); k=$${k%.*}; awk '{if (!i) {i=1;printf("INFO-DIR-SECTION Maxima\nSTART-INFO-DIR-ENTRY\n* Maxima-%s: (%s).  A computer algebra system -- contributions.\nEND-INFO-DIR-ENTRY\n",k,k);}} {print}' k=$$k $$i >$$i.tmp; diff -u $$i $$i.tmp ; mv $$i.tmp $$i ; fi ; done

	cat debian/tmp/usr/share/info/maxima.info | \
		awk '/START-INFO-DIR-ENTRY/ {print "INFO-DIR-SECTION Maxima"}{print}' >debian/foo && \
		mv debian/foo debian/tmp/usr/share/info/maxima.info

	cat debian/tmp/usr/share/man/man1/maxima.1 | \
		sed 's,^.TH MAXIMA 1L,.TH MAXIMA 1,1' >debian/foo && \
		mv debian/foo debian/tmp/usr/share/man/man1/maxima.1

	for i in $$(find debian/tmp/usr/share/info -name "maxima-index.lisp") ; do \
		mkdir -p debian/tmp/usr/share/doc/maxima/$$(basename $$(dirname $$i)) && \
		cat $$i | sed 's,^(load-info-hashtables,(clrhash cl-info::*info-tables*)\n(load-info-hashtables,1' >tmp &&\
	        ! cmp $$i tmp && mv tmp $$i &&\
                mv $$i debian/tmp/usr/share/doc/maxima/$$(basename $$(dirname $$i)) ; done

	chmod 755 ./debian/tmp/usr/share/maxima/$(MVERS)/share/contrib/lurkmathml/mathmltest

	rm -f debian/tmp/usr/share/info/dir debian/tmp/usr/share/maxima/$(MVERS)/share/logic/COPYING

	! [ -e debian/tmp/usr/share/maxima/$(MVERS)/share/contrib/gentran/man/MANUAL.ps.gz ] ||\
		gunzip debian/tmp/usr/share/maxima/$(MVERS)/share/contrib/gentran/man/MANUAL.ps.gz

	rm -f debian/tmp/usr/share/maxima/$(MVERS)/share/.gitattributes

	dh_install

	touch install-stamp

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -i
	dh_installexamples -i
	dh_installmenu -i
	dh_installemacsen -i
	dh_installtex -i -p maxima-emacs
	dh_installcron -i
	dh_installman -i
#	dh_desktop -i
	dh_installinfo -p maxima-doc debian/tmp/usr/share/info/*info*
	dh_installchangelogs  -i debian/ChangeLog
	dh_link -i
	dh_strip -i
	dh_compress -i -X.shtml -X.hh -Xmaxima-index.lisp
	dh_fixperms -i
	dh_installdeb -i
	dh_shlibdeps -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installdocs -a
	dh_installexamples -a
	dh_installmenu -a
	dh_installemacsen -a
	dh_installcron -a
	dh_installman -a
#	dh_desktop -a
	dh_installinfo -a
	dh_installchangelogs  -a debian/ChangeLog
	dh_link -a
	dh_strip -a $(NO_STRIP)
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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

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