#!/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

# This is the debhelper compatability version to use.
#export DH_COMPAT=3

NO_STRIP:=
#ALT_LINK:=
#ifeq ($(DEB_BUILD_ARCH),mips)
#ALT_LINK:=--enable-gcl-alt-link
#endif
#ifeq ($(DEB_BUILD_ARCH),mipsel)
#ALT_LINK:=--enable-gcl-alt-link
#endif
#ifeq ($(DEB_BUILD_ARCH),ia64)
#ALT_LINK:=--enable-gcl-alt-link
#NO_STRIP:=-Xmaxima
#endif
#ifeq ($(DEB_BUILD_ARCH),hppa)
#ALT_LINK:=--enable-gcl-alt-link
#endif
#ifeq ($(DEB_BUILD_ARCH),alpha)
#ALT_LINK:=--enable-gcl-alt-link
#endif
#ifeq ($(DEB_BUILD_ARCH),powerpc)
#NO_STRIP:=-Xmaxima
#endif

SAVES:=$(addprefix debian/save/,aclocal.m4 configure interfaces/emacs/imaxima/stamp-vti\
	 interfaces/emacs/imaxima/imaxima.info interfaces/emacs/imaxima/version.texi\
	 src/sys-proclaim.lisp src/lisp)

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

	GCL_ANSI=t gcl -batch -eval '(bye #+ia64 1)' || \
	echo '(setq compiler::*opt-three* "-O0" compiler::*opt-two* "-O0")(si::save-system "gcl")' | GCL_ANSI=t gcl

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

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

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

# This sets up some reasonable readline command completion by default

# Reenable if/when gcl uses __mulsi et. al. on m68k, now configured for (020) unoptimally
#	[ "$$(dpkg --print-architecture)" != "m68k" ] || cp debian/rtest8.mac.m68k tests/rtest8.mac

	chmod +x ./maxima-local
#	echo '(bye #-(or sparc sparc64) 1)' | gcl || 
	./maxima-local --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

#	cat tmp tests/tests-gcl.log >debian/test_results.out

#	grep "Error(s) found in.*:" tests/tests-gcl.log | sed 's,Error(s) found in ,,1'	>tmp

#	! [ -e tests/known-bugs ] || diff -u tests/known-bugs tmp

#	[ -e tests/known-bugs ] || grep -q "No Errors Found" tests/tests-gcl.log

	touch tmp
	cat tmp >debian/test_results.out

#	echo '(bye #-(or sparc sparc64) 1)' | gcl || 
	grep -q "No unexpected errors found." debian/test_results.out

	rm -f tmp

#	cd doc/maximabook &&\
#		TEXINPUTS=../../interfaces/emacs/emaxima:.:$$TEXINPUTS latex maxima && \
#		TEXINPUTS=../../interfaces/emacs/emaxima:.:$$TEXINPUTS latex maxima && \
#		TEXINPUTS=../../interfaces/emacs/emaxima:.:$$TEXINPUTS latex maxima && \
#		dvips -o maxima.ps maxima

	echo ':lisp (setq si::*readline-prefix* "maxima::$$")(when (fboundp (quote si::sgc-on)) (si::sgc-on t))(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
	rm -f build-stamp install-stamp

	-cd doc && $(MAKE) clean
	-cd interfaces && $(MAKE) clean
	-GCL_ANSI=t $(MAKE) clean
	debian/rules restore
	-rm -f debian/test_results.out tests/tests-gcl.log
	-for i in $$(find -name Makefile) ; do\
		! [ -e $$i.am ] || rm -f $$i;\
		! [ -e $$i.am ] || rm -f $$i.in;\
	 done
	-rm -f src/maxima doc/man/maxima.1 interfaces/xmaxima/Tkmaxima/Header.tcl \
		interfaces/xmaxima/Tkmaxima/tclIndex interfaces/xmaxima/autoconf-variables.tcl \
		interfaces/xmaxima/xmaxima src/autoconf-variables.lisp
	rm -f src/factor.fn src/nparse.fn tests/gazonk0.fn
#	cd doc/maximabook && \
#		rm -f maxima.aux maxima.dvi maxima.idx maxima.lof \
#			maxima.log maxima.out maxima.toc maxima.ps
	rm -f debian/*.install
	rm -f $(BOOK) config.log config.status plotting/mgnuplot \
			src/maxima.bat src/rmaxima maxima-local xmaxima-local maxima.spec maxima.iss
	rm -rf src/numerical/slatec/fortran/CVS src/*gazonk* interfaces/xmaxima/win32/Makefile
	for i in aclocal.m4 src/sys-proclaim.lisp configure ; do ! [ -e $$i.ori ] || mv $$i.ori $$i; done
	rm -rf doc/info/maxima.html
	rm -rf src/binary-gcl
	rm -f doc/man/ru/maxima.1
	rm -f tests/rtest14.ERR
	rm -f doc/info/include-maxima.texi
	rm -f configure.lineno doc/info/extract_categories.sh.debdiff
	rm -f raw_ff_map src/share-subdirs.lisp doc/info/figures/favicon.ico
	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 >$@

install: install-stamp
install-stamp: build-stamp $(BOOK) $(INSTALLS)
	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

# GCL must be able to write to directory in which tests are run
#	cd debian/tmp/usr/share/maxima/$(MVERS)/tests && patch -p1 <../../../../../../tests.lisp.patch
#	cat debian/tmp/usr/share/maxima/$(MVERS)/tests/tests.lisp | sed "s,@MVERS@,$(MVERS),g" >tmp && \
#		mv tmp debian/tmp/usr/share/maxima/$(MVERS)/tests/tests.lisp

#	cd debian/tmp/usr/bin && patch -p1 <../../../xmaxima.patch
#	cat debian/tmp/usr/bin/xmaxima | sed "s,@MVERS@,$(MVERS),g" >tmp && \
#		chmod 755 tmp && mv tmp debian/tmp/usr/bin/xmaxima

	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

#	cp -a debian/plotting debian/tips debian/tmp/usr/share/doc/maxima-doc/
#	for i in $$(find debian/tmp/usr/share/doc/maxima-doc/plotting debian/tmp/usr/share/doc/maxima-doc/tips -name "*.uu") ; do \
#		cat $$i | uudecode > $${i%.uu} && rm $$i ; done
#
#	for i in $$(find debian/tmp/usr/share/doc/maxima-doc/plotting debian/tmp/usr/share/doc/maxima-doc/tips -name "*.shtml") ; do \
#		mv $$i $${i%.shtml}.html ; done

	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 &&\
		cat $$i | sed 's,#-gcl \*load-pathname\* #+gcl sys:\*load-pathname\*,"/usr/share/info/",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

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