#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

#VER:=$(shell basename `pwd` | sed 's,.*-,,1')
PN:=axiom
VER:=$(shell awk '{if (i) next;i=1;a=$$2;gsub("[()]","",a);split(a,A,"-");print A[1];}' debian/changelog)
PD:=$(PN)-$(VER)

export DH_OPTIONS

NUMJOBS:=1
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  NUMJOBS:=$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  MAKEFLAGS+=-O
endif

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

configure: configure-stamp
configure-stamp:
	dh_testdir

	touch configure-stamp

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp: configure-stamp
	dh_testdir

	mkdir -p $(CURDIR)/mnt/linux/algebra
	cp -pr src/share/algebra/*.daase mnt/linux/algebra

#	echo '(setq si::*optimize-maximum-pages* nil)' \
#		'(load (compile-file "debian/gcl27p.l"))' \
#		'(setq si::*optimize-maximum-pages* t)' \
#		'(si::save-system "gcl27")' | GCL_ANSI=t gcl27

#	export PATH=$(CURDIR):$$PATH ; 
	export AXIOM=$(CURDIR)/mnt/linux ; \
	export PATH=$$AXIOM/bin:$$PATH ; \
	export GCL_ANSI=t ; \
	export GCL=`which gcl27` ; \
	eval `dpkg-buildflags --export=sh` && \
	HOME=$$(pwd) GCL_MULTIPROCESS_MEMORY_POOL=$$(pwd) $(MAKE) -j $(NUMJOBS) TESTSET=regresstests GCL=$$(which gcl27)

	find mnt/linux -name bessintr.eps -exec rm {} \;
	find mnt/linux -name htsearch -exec chmod 755 {} \;
	find mnt/linux -name command.list -exec chmod -x {} \;
	for i in $$(find mnt/linux -name presea); do cat $$i | sed 's,!/bin/awk,!/usr/bin/awk,1' >tmp && mv tmp $$i; done
	find mnt/linux -name presea -exec chmod 755 {} \;

	echo ")lisp (progn (setq compiler::*default-system-p* nil)(si::save-system \"foo\"))" | AXIOM=$(CURDIR)/mnt/linux $(CURDIR)/mnt/linux/bin/AXIOMsys && mv foo mnt/linux/bin/AXIOMsys

	for i in $$(find mnt/linux -wholename "*.daase/*.daase" -type d); do rm -rf $$i; done

	touch build-stamp

IFS:=$(addprefix debian/,axiom.install axiom.links axiom-doc.install axiom-doc.links axiom-source.install axiom-source.links axiom-test.install axiom-test.links axiom-databases.install axiom-databases.links axiom-tex.install axiom-graphics.install axiom-graphics-data.install axiom-hypertex.install axiom-hypertex-data.install )

fooclean:
	rm -f $(IFS)

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	set

	AXIOM=$(CURDIR)/mnt/linux  \
	C_INCLUDE_PATH=/usr/include:$C_INCLUDE_PATH \
	$(MAKE) clean
	rm -f lsp/Makefile.dvi src/algebra/Makefile.dvi src/input/Makefile.dvi obj/noweb/contrib/kostas/Makefile obj/noweb/src/c/notangle.o obj/noweb/src/c/getline.o obj/noweb/src/c/match.o obj/noweb/src/c/modules.o obj/noweb/src/c/modtrees.o obj/noweb/src/c/strsave.o obj/noweb/src/c/main.o obj/noweb/src/c/errors.o obj/noweb/src/c/columns.o obj/noweb/src/c/nt obj/noweb/src/c/markmain.o obj/noweb/src/c/markup.o obj/noweb/src/c/markup obj/noweb/src/c/mnt.o obj/noweb/src/c/mnt obj/noweb/src/c/finduses.o obj/noweb/src/c/recognize.o obj/noweb/src/c/finduses mnt/bin/lib/nt mnt/bin/lib/markup mnt/bin/lib/mnt mnt/bin/lib/finduses mnt/bin/Makefile.dvi  Makefile.dvi src/Makefile.dvi src/interp/Makefile.dvi src/doc/Makefile.dvi
	rm -f lsp/Makefile src/Makefile src/boot/Makefile src/clef/Makefile src/doc/Makefile src/etc/Makefile src/input/Makefile src/interp/Makefile src/lib/Makefile src/scripts/Makefile src/share/Makefile 
	rm -f src/algebra/Makefile lastBuildDate
	find -name Makefile.dvi -exec rm {} \;
	rm -f Makefile.linux
	rm -f $(IFS)
	rm -rf debian/axiom_tutorialu debian/bin debian/axiom.1
	find -mindepth 2 -name Makefile -exec rm {} \;
	rm -f books/tangle.o
	mv books/axiom.sty books/axiom.sty.ori
	mv src/axiom-website/CATS/axiom.sty src/axiom-website/CATS/axiom.sty.ori
	find -name axiom.sty -exec rm {} \;
	mv books/axiom.sty.ori books/axiom.sty
	mv src/axiom-website/CATS/axiom.sty.ori src/axiom-website/CATS/axiom.sty
	rm -rf kaf*.sdata
	rm -f src/interp/bookvol5.pamphlet src/interp/bookvol9.pamphlet src/algebra/bookvol10.2.spad.pamphlet
	rm -f src/algebra/bookvol10.4.spad.pamphlet src/algebra/bookvol10.3.spad.pamphlet 
	rm -f src/interp/Makefile.pamphlet.new debian/test_results
	rm -f libdb.text src/algebra/Makefile.help src/algebra/ELTAB.nrlib/ELTAB.data
	rm -f src/interp/bookvol10.5.pamphlet src/algebra/*.spad src/lib/Makefile.pdf books/tangle1.lisp
	rm -rf .texlive2024 gcl_pool src/etc/asq src/etc/asq.c src/algebra/layer*done
	rm -rf src/algebra/layer0bootstrap
	rm -rf src/algebra/layer0copy src/algebra/fasthelp src/algebra/fastinput src/algebra/fastxhtml
	rm -rf gcl27 .texlive2025 src/etc/rebuild #FIXME
	dh_clean 

debian/axiom.install: 
	find mnt/linux -name "*.o" | awk '{a=$$1;sub("/[^/]*$$","",a);sub("mnt/linux/","",a);printf("%s usr/lib/$(PD)/%s\n",$$1,a);}' >>$@
	find mnt/linux -name "*.lsp" | awk '{a=$$1;sub("/[^/]*$$","",a);sub("mnt/linux/","",a);printf("%s usr/lib/$(PD)/%s\n",$$1,a);}' >>$@
	find mnt/linux/bin/* -maxdepth 0 -type f | grep -v tanglec | grep -v /h[yt] | grep -v /view | grep -v /gcl | grep -v html | awk '{a=$$1;sub("/[^/]*$$","",a);sub("mnt/linux/","",a);printf("%s usr/lib/$(PD)/%s\n",$$1,a);}' >>$@
	find mnt/linux/lib/* | grep -v copyright | grep -v /h[yt] | grep -v /view | grep -v /graph | awk '{a=$$1;sub("/[^/]*$$","",a);sub("mnt/linux/","",a);printf("%s usr/lib/$(PD)/%s\n",$$1,a);}' >>$@
	echo debian/bin/axiom usr/bin >>$@
	echo debian/axiom.1 usr/share/man/man1 >>$@

debian/axiom-graphics.install: 
	find mnt/linux/bin/view* mnt/linux/lib/view* | awk '{a=$$1;sub("/[^/]*$$","",a);sub("mnt/linux/","",a);printf("%s usr/lib/$(PD)/%s\n",$$1,a);}' >>$@

debian/axiom-graphics-data.install: 
	echo mnt/linux/lib/graph | awk '{a=$$1;sub("/[^/]*$$","",a);sub("mnt/linux/","",a);printf("%s usr/lib/$(PD)/%s\n",$$1,a);}' >>$@
	echo mnt/linux/doc/viewports | awk '{printf("%s usr/share/doc/axiom-doc\n",$$1);}' >>$@

debian/axiom-hypertex.install: 
	find mnt/linux/bin/h[ty]* mnt/linux/lib/h[ty]* | awk '{a=$$1;sub("/[^/]*$$","",a);sub("mnt/linux/","",a);printf("%s usr/lib/$(PD)/%s\n",$$1,a);}' >>$@

debian/axiom-hypertex-data.install: 
	echo mnt/linux/doc/hypertex | awk '{printf("%s usr/share/doc/axiom-doc\n",$$1);}' >>$@
	echo mnt/linux/doc/ps | awk '{printf("%s usr/share/doc/axiom-doc\n",$$1);}' >>$@
	echo mnt/linux/doc/bitmaps | awk '{printf("%s usr/share/doc/axiom-doc\n",$$1);}' >>$@
	echo mnt/linux/doc/ht.db | awk '{printf("%s usr/share/doc/axiom-doc\n",$$1);}' >>$@
	find mnt/linux/doc/*.pamphlet | awk '{printf("%s usr/share/doc/axiom-doc\n",$$1);}' >>$@

debian/axiom.links:
	echo usr/share/doc/axiom/copyright usr/lib/$(PD)/lib/copyright >>$@
#	echo usr/bin/axiom usr/bin/AXIOMsys >>$@
	echo usr/share/man/man1/axiom.1 usr/share/man/man1/AXIOMsys.1 >>$@

debian/axiom-doc.install: debian/axiom-hypertex-data.install debian/axiom-graphics-data.install
	find mnt/linux/doc/* -maxdepth 0 ! -empty | \
		$(shell cat $^ | awk '{print "grep -v " $$1 " | "}') \
		awk '{printf("%s usr/share/doc/axiom-doc\n",$$1);}' >>$@
	echo debian/axiom_tutorialu usr/share/doc/axiom-doc >>$@

debian/axiom-doc.links:
	echo usr/share/doc/axiom-doc usr/lib/$(PD)/doc >>$@

debian/axiom-source.install:
	echo mnt/linux/src usr/share/$(PD) >>$@

debian/axiom-source.links:
	echo usr/share/$(PD)/src usr/lib/$(PD)/src >>$@

debian/axiom-test.install:
	echo mnt/linux/input usr/share/$(PD) >>$@
	echo debian/test_results usr/share/doc/axiom-test >>$@
	echo debian/bin/axiom-test usr/bin >>$@
	echo debian/axiom-test.1 usr/share/man/man1 >>$@

debian/axiom-test.links:
	echo usr/share/$(PD)/input usr/lib/$(PD)/input >>$@

debian/axiom-databases.install:
	find mnt/linux -name "*.daase" | awk '{a=$$1;sub("/[^/]*$$","",a);sub("mnt/linux/","",a);printf("%s usr/share/$(PD)/%s\n",$$1,a);}' >>$@
	find mnt/linux -name "*.text" | awk '{a=$$1;sub("/[^/]*$$","",a);sub("mnt/linux/","",a);printf("%s usr/share/$(PD)/%s\n",$$1,a);}' >>$@
	find mnt/linux -name "*.data" | awk '{a=$$1;sub("/[^/]*$$","",a);sub("mnt/linux/","",a);printf("%s usr/share/$(PD)/%s\n",$$1,a);}' >>$@

debian/axiom-databases.links:
	find mnt/linux -name "*.daase" | awk '{a=$$1;sub("mnt/linux/","",a);printf("usr/share/$(PD)/%s usr/lib/$(PD)/%s\n",a,a);}' >>$@
	find mnt/linux -name "*.text" | awk '{a=$$1;sub("mnt/linux/","",a);printf("usr/share/$(PD)/%s usr/lib/$(PD)/%s\n",a,a);}' >>$@
	find mnt/linux -name "*.data" | awk '{a=$$1;sub("mnt/linux/","",a);printf("usr/share/$(PD)/%s usr/lib/$(PD)/%s\n",a,a);}' >>$@

debian/axiom-tex.install:
	echo books/axiom.sty | awk '{a=$$1;sub("/[^/]*$$","",a);sub("mnt/linux/","",a);printf("%s usr/share/texmf/tex/plain/misc\n",$$1);}' >>$@


debian/axiom_tutorialu: debian/axiom_tutorial
	cp -a $< $@
	rm -f $@/images/*.uu

GIFS:=$(shell ls -1 debian/axiom_tutorial/images/*.gif.uu | sed -e "s,\.uu$$,,1" -e "s,axiom_tutorial,axiom_tutorialu,1")
$(GIFS): debian/axiom_tutorialu/images/%.gif: debian/axiom_tutorial/images/%.gif.uu debian/axiom_tutorialu
	cd $(@D) && uudecode ../../../$<

debian/bin/axiom: mnt/linux/bin/axiom debian/run-sman.sh
	mkdir -p $(@D)
	cat $< | sed -e "s,^SPADDEFAULT=\(.*\),SPADDEFAULT=\1;export AXIOM=/usr/lib/axiom-$(VER);export PATH=\$$AXIOM/bin:\$$PATH,1" \
		     -e 	"s,^otheropts=.*,otheropts=\"\$$([ -x /usr/lib/axiom-$(VER)/bin/hypertex ] || echo -noht) \$$([ -x /usr/lib/axiom-$(VER)/lib/viewman ] || echo -nogr)\",1" \
		     -e "s,^exec[ \t]\(.*\),#exec \1,1"	 >$@
	cat debian/run-sman.sh >>$@
	chmod +x $@

debian/axiom.1: debian/axiom.1.in
	mkdir -p $(@D)
	cat $< | sed "s,@VER@,$(VER),g"  >$@
	chmod +x $@

debian/bin/axiom-test: debian/axiom-test.sh
	mkdir -p $(@D)
	cat $< | sed "s,@PD@,$(PD),g"  >$@
	chmod +x $@

#mnt/linux/doc/book.pdf: mnt/linux/doc/book.dvi
#	cd $(@D) && dvipdfm $(subst .pdf,,$(@F))

debian/test_results: build
	rm -f $@
	find -name "*.output" -exec cat {} \; >>$@

install: DH_OPTIONS=
install: build $(GIFS)  debian/axiom.1 debian/bin/axiom debian/bin/axiom-test \
	 debian/test_results $(IFS) # mnt/linux/doc/book.pdf
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	for i in sty data text daase; do find mnt/linux -type f -name "*.$$i" -exec chmod -x {} \; ; done
	find mnt/linux/doc -type f -exec chmod -x {} \;
	find mnt/linux -name "summary" -exec chmod -x {} \;
	for i in $$(find mnt/linux -name CVS -type d); do rm -rf $$i ; done
	find mnt/linux/doc -empty -exec rm {} \;

# Build architecture-independent files here.
binary-indep: DH_OPTIONS:=-i
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs changelog
	dh_installdocs
	dh_installexamples
	dh_install
	dh_installmenu
	dh_installtex -p axiom-tex
	dh_installman
	dh_link
	dh_strip
	dh_compress -X.xhtml $(shell cat debian/axiom-hypertex-data.install debian/axiom-graphics-data.install | grep axiom-doc | sed 's,mnt/linux/doc/\([^ ]*\).*,-Xaxiom-doc/\1,g')
	dh_lintian
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture-dependent files here.
binary-arch: DH_OPTIONS:=-a
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs changelog
	dh_installdocs
	dh_installexamples
	dh_install
	dh_installmenu
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_lintian
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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