#!/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/, interfaces/emacs/imaxima/stamp-vti\
	 interfaces/emacs/imaxima/imaxima.info interfaces/emacs/imaxima/version.texi\
	 src/lisp\
	 $(shell ls -1 doc/info/maxima*.html)\
	 src/share-subdirs_autogenerated.lisp \
	 interfaces/xmaxima/doc/xmaxima.info \
	 share/contrib/maxima-odesolve/maxima-odesolve-index.lisp \
	  \
	 src/autoconf-variables.lisp \
	 src/share-subdirs.lisp \
	 doc/info/category-macros.texi doc/info/texi2html.init\
	\
	\
	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/%: %
	touch interfaces/emacs/imaxima/imaxima.info
	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

	dh_autoreconf
#	aclocal
#	automake

	echo '(progn (setq si::*optimize-maximum-pages* nil)(when (fboundp (quote si::sgc-on)) (fmakunbound (quote si::sgc-on)))(load (compile-file "debian/gcl27p.l"))(si::save-system "gcl"))' | GCL_ANSI=t gcl27

	git_found=false PATH=$$(pwd):$$PATH GCL_ANSI=t ./configure --enable-gcl $$(gcl -batch -eval '(progn #-native-reloc(princ "--enable-gcl-alt-link"))') \
		--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)

	touch -r src/share-subdirs_autogenerated.lisp src/share-subdirs.lisp
	touch -r src/gcl-depends.mk src/binary-gcl/maxima

	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::gbc t)(si::save-system "foo")' | \
	GCL_MEM_MULTIPLE=1.0 GCL_GC_PAGE_MIN=0.5 GCL_GC_PAGE_MAX=0.75 GCL_GC_ALLOC_MIN=0.05 HOME=$$(pwd) \
	./maxima-local && mv foo src/binary-gcl/maxima


	chmod +x ./maxima-local
	touch tests/tests-gcl.log
	GCL_MEM_MULTIPLE=1.0 GCL_GC_PAGE_MIN=0.5 GCL_GC_PAGE_MAX=0.75 GCL_GC_ALLOC_MIN=0.05 HOME=$$(pwd) \
	./maxima-local -g --lisp=gcl --batch-string="run_testsuite(share_tests=true);"  >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")' | \
	GCL_MEM_MULTIPLE=1.0 GCL_GC_PAGE_MIN=0.5 GCL_GC_PAGE_MAX=0.75 GCL_GC_ALLOC_MIN=0.05 HOME=$$(pwd) \
	./maxima-local && mv foo src/binary-gcl/maxima

	touch build-stamp

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

clean: #restore
	dh_testdir
	dh_testroot

	! [ -e Makefile ] || $(MAKE) clean
	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 desktopintegration/Makefile
	rm -rf bin/interfaces bin/doc bin/src bin/xmaxima
	rm -f doc/info/figures/Makefile
	rm -f tmp
	rm -f src/gcl-depends.mk.tmp src/startmaxima_abcl.sh
	rm -f tests/test.sh
	rm -f doc/info/build_html.sh # doc/info/extract_categories.sh
	cd doc/info && rm -f  Affine.html  Bernstein_002dpkg.html  Bug-Detection-and-Reporting.html  Command-Line.html  Data-Types-and-Structures.html \
		Debugging.html  Differential-Equations.html  Differentiation.html  Documentation-Categories.html  Elliptic-Functions.html  Equations.html  \
		Error-and-warning-messages.html  Evaluation.html  Expressions.html  File-Input-and-Output.html  Function-Definition.html\
		Function-and-Variable-Index.html  Groups.html  Help.html  Integration.html  Introduction-to-Maxima.html  Limits.html  Mathematical-Functions.html\
		Matrices-and-Linear-Algebra.html  Maximas-Database.html  Miscellaneous-Options.html  Number-Theory.html  Numerical.html  Operators.html  Plotting.html\
		Polynomials.html  Program-Flow.html  Rules-and-Patterns.html  Runtime-Environment.html  Sets.html  Simplification.html  Special-Functions.html\
		Sums-Products-and-Series.html  Symmetries.html  alt_002ddisplay_002dpkg.html  asympa_002dpkg.html  atensor.html  augmented_005flagrangian_002dpkg.html\
		bitwise_002dpkg.html  bode_002dpkg.html  celine_002dpkg.html  clebsch_005fgordan_002dpkg.html  cobyla_002dpkg.html  combinatorics_002dpkg.html  \
		contrib_005fode_002dpkg.html  ctensor.html  descriptive_002dpkg.html  diag_002dpkg.html  distrib_002dpkg.html  draw_002dpkg.html  drawdf_002dpkg.html\
		dynamics_002dpkg.html  engineering_002dformat_002dpkg.html  ezunits_002dpkg.html  f90_002dpkg.html  finance_002dpkg.html  fractals_002dpkg.html  \
		ggf_002dpkg.html  graphs_002dpkg.html  grobner_002dpkg.html  impdiff_002dpkg.html  index.html  interpol_002dpkg.html  itensor.html  lapack_002dpkg.html  \
		lbfgs_002dpkg.html  lindstedt_002dpkg.html  linearalgebra_002dpkg.html  lsquares_002dpkg.html  makeOrders_002dpkg.html  minpack_002dpkg.html  \
		mnewton_002dpkg.html  numericalio_002dpkg.html  odepack_002dpkg.html  operatingsystem_002dpkg.html  opsubst_002dpkg.html  orthopoly_002dpkg.html\
		ratpow_002dpkg.html  romberg_002dpkg.html  simplex_002dpkg.html  simplification_002dpkg.html  solve_005frec_002dpkg.html  stats_002dpkg.html\
		stirling_002dpkg.html  stringproc_002dpkg.html  to_005fpoly_005fsolve_002dpkg.html  unit_002dpkg.html  wrstcse_002dpkg.html  zeilberger_002dpkg.html 

	rm -f 	doc/man/de/Makefile doc/man/de/maxima.1 doc/man/ru/Makefile \
		interfaces/xmaxima/doc/Command_002dline-options.html interfaces/xmaxima/doc/Concept-Index.html \
		interfaces/xmaxima/doc/Entering-commands.html interfaces/xmaxima/doc/Getting-Help.html \
		interfaces/xmaxima/doc/Main-Window.html interfaces/xmaxima/doc/Openmath-plots.html \
		interfaces/xmaxima/doc/Session-control.html interfaces/xmaxima/doc/The-browser.html \
		interfaces/xmaxima/doc/index.html doc/info/de/build_html.sh doc/info/ja/Makefile doc/info/ru/Makefile \
		interfaces/xmaxima/doc/Xmaxima-plots.html share/contrib/symplectic_ode/Makefile


	rm -f $(INSTALLS)
	rm -f debian/ChangeLog doc/info/de/include-maxima.texi doc/info/ja/include-maxima.texi doc/info/js_licenses.html doc/info/math.m4 doc/info/pt_BR/include-maxima.texi doc/info/ru/include-maxima.texi doc/info/texi2html-7.init share/nelder_mead/Makefile 

	dh_autoreconf_clean
	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

	mv debian/tmp/usr/share/emacs/site-lisp debian/tmp/usr/share/emacs/site-lisp1
	mkdir -p debian/tmp/usr/share/emacs/site-lisp
	mv debian/tmp/usr/share/emacs/site-lisp1 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
	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

	cd debian/tmp/usr/share/info && mkdir -p maxima && \
		for i in *.info; do if grep -q ": *(maxima/" $$i; then mv $$i maxima/; fi; done

	cd debian/tmp/usr/share/maxima/$(MVERS)/share && \
		for i in translators/m2mj/M2M.java translators/m2mj/Macsyma.stg translators/m2mj/Templates.java \
                         translators/m2mj/mapleLexer.g4 translators/m2mj/mapleParser.g4 \
                         translators/m2mj/mapleParserVisitorImpl.java translators/m2mj/readme \
			 diff_form/example.txt diff_form/surface_example.txt stringproc/rectangular.csv-utf-16be \
			 stringproc/rectangular.csv-utf-16le stringproc/rectangular.csv-utf-32be \
			 stringproc/rectangular.csv-utf-32le ; do \
			 iconv -c -t UTF-8 $$i -o tmp ; mv tmp $$i ; done  # iconv exits with code 1 now !

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

	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
	rm -f debian/tmp/usr/share/maxima/5.43.2/share/test_encodings/escape-double-quote

	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_lintian -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_lintian -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
