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

export DH_OPTIONS
export DEB_BUILD_MAINT_OPTIONS=hardening=+bindnow

OVERRIDES:= $(shell find debian/ -name "*.lintian-overrides.in" |sed 's,\.in,,g')

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

	[ ! -f Makefile ] || $(MAKE) clean

	rm -f   Makefile config.status config/fricas_c_macros.h config_cl.out contrib/emacs/Makefile pre-gen \
		src/Makefile src/aldor/Makefile src/aldor/Makefile2 src/aldor/Makefile3 src/algebra/Makefile \
		src/algebra/abbrev.list src/algebra/extract-spad.mk src/boot/Makefile src/clef/Makefile \
		src/doc/Makefile src/etc/Makefile src/etc/gen-cpl.fn src/graph/Gdraws/Makefile src/graph/Makefile \
		src/graph/view2D/Makefile src/graph/view3D/Makefile src/graph/viewAlone/Makefile \
		src/graph/viewman/Makefile src/hyper/Makefile src/input/Makefile src/interp/Makefile \
		src/lib/Makefile src/lisp/Makefile src/lisp/raw_prelisp_map src/sman/Makefile

	rm -f debian/test_results debian/*.install debian/*.out debian/*.links debian/install_list debian/installs
	rm -rf debian/fricas_tutorialu debian/efricas.1 config.log
	rm -rf debian/README.aldor debian/README.sbcl debian/README.macos debian/README.texmacs
	rm -rf $(OVERRIDES) src/doc/util-ht src/doc/util.ht gcl27

	dh_clean
	dh_autoreconf_clean

build: build-arch build-indep
build-arch: build-stamp
build-indep: #build-stamp
build-stamp:
	dh_testdir

	dh_autoreconf

#	echo '(load (compile-file "debian/gcl27p.l"))(si::save-system "gcl27")' | GCL_ANSI=t gcl27

	export GCL_ANSI=t ; \
	eval `dpkg-buildflags --export=sh` && ./configure --with-lisp=gcl27 --prefix=/usr && $(MAKE)

	(cd src/input && $(MAKE) check) | tee debian/test_results

	touch build-stamp

install: DH_OPTIONS=
install: install-arch install-indep
install-arch: install-stamp
install-indep: install-stamp
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	DESTDIR=$$(pwd)/debian/tmp $(MAKE) install

	find debian/tmp -name "*.fn" -exec rm {} \;

	find debian/tmp/usr/lib -name copyright -exec rm {} \;

	mkdir -p debian/tmp/usr/share/fricas/input && cp src/input/*.input debian/tmp/usr/share/fricas/input
	mkdir -p debian/tmp/usr/share/fricas/input && cp src/input/check_result debian/tmp/usr/share/fricas/input

	mkdir -p debian/tmp/usr/share/doc/fricas-test && cp debian/test_results debian/tmp/usr/share/doc/fricas-test
	mkdir -p debian/tmp/usr/bin && cp debian/fricas-test.sh debian/tmp/usr/bin/fricas-test
#	mkdir -p debian/tmp/usr/share/man/man1 && cp debian/fricas-test.1 debian/tmp/usr/share/man/man1

	for i in $$(find debian/tmp -name HTXFormatPage7.ht); do iconv -f ISO-8859-16 -t UTF-8 -o tmp $$i && mv tmp $$i && chmod 644 $$i; done

	touch install-stamp

debian/install_list: install
	find debian/tmp -type f |sort >$@

debian/%.links: debian/%.out
	cat $< | sort | uniq | awk '/usr\/lib/ {a=$$1;gsub("debian/tmp/","",a);b=a;gsub("usr/lib","usr/share",b);gsub("target/[^/]*/","",b);print b,a}' >$@
	for i in $$(cat $<); do grep $$i debian/install_list ; done | \
		sort | uniq | \
		awk '{a=$$1;gsub("debian/tmp/","",a);b=a;gsub("usr/lib","usr/share",b);gsub("target/[^/]*/","",b);gsub("[^/]*$$","",b);print $$1,b}' \
		>debian/$*.install
	join -v2 debian/$*.install debian/install_list >debian/tmp_install_list && \
		touch -r debian/install_list debian/tmp_install_list && \
		mv debian/tmp_install_list debian/install_list

debian/%.install: debian/%.out
	for i in $$(cat $<); do grep $$i debian/install_list ; done | \
		sort | uniq | awk '{a=$$1;gsub("debian/tmp/","",a);gsub("[^/]*$$","",a);print $$1,a}' >$@
	join -v2 $@ debian/install_list >debian/tmp_install_list && \
		touch -r debian/install_list debian/tmp_install_list && \
		mv debian/tmp_install_list debian/install_list

debian/fricas-graphics.out: debian/install_list
	find debian/tmp -path "*/bin/view*" >$@
	find debian/tmp -path "*/lib/view*" >>$@

debian/fricas-graphics-data.out: debian/fricas-graphics.install
	find debian/tmp -name graph -type d >$@
	find debian/tmp -name viewports -type d >>$@

debian/fricas-hypertex.out: debian/fricas-graphics-data.links
	find debian/tmp -path "*/bin/h[ty]*" >$@
	find debian/tmp -path "*/lib/h[ty]*" >>$@

debian/fricas-hypertex-data.out: debian/fricas-hypertex.install
	find debian/tmp -path "*share/hypertex" -type d >$@

debian/fricas-source.out: debian/fricas-hypertex-data.links
	find debian/tmp -name src -type d >$@

debian/fricas-doc.out: debian/fricas-source.links
	find debian/tmp -name spadhelp -type d >$@
	find debian/tmp -name msgs -type d >>$@

debian/efricas.1: doc/fricas.1
	cp $< $@

# debian/fricas-emacs.out: debian/fricas-doc.links debian/efricas.1
# 	find debian/tmp -name emacs -type d >$@
# 	find debian/tmp -name efricas >>$@

debian/elpa-fricas.out: debian/fricas-doc.links debian/efricas.1
	find debian/tmp -name emacs -type d >$@
	find debian/tmp -name efricas >>$@

debian/fricas-databases.out: debian/elpa-fricas.install # debian/fricas-emacs.links
	find debian/tmp -path "*.daase" >$@
	find debian/tmp -path "*.text" >>$@
	find debian/tmp -name command.list >>$@
	find debian/tmp -name summary >>$@
	find debian/tmp -name copyright >>$@

debian/fricas-test.out: debian/fricas-databases.links
	find debian/tmp -name input -type d >$@
	find debian/tmp -name test_results >>$@
	find debian/tmp -name "fricas-test*" >>$@

debian/fricas.out: debian/fricas-test.install
	find debian/tmp -type d -name bin >$@
	find debian/tmp -path "*/lib/*" -type f -perm 0755 >>$@
	find debian/tmp -type d -name "algebra" |grep -v src/algebra >>$@

debian/installs: debian/fricas.install
	! [ -s debian/install_list ] || false
	touch $@

debian/fricas_tutorialu: debian/fricas_tutorial
	cp -a $< $@
	cd $@/images && for i in *.uu ; do cat $$i | uudecode; done
	rm -f $@/images/*.uu

debian/README.aldor: src/aldor/README
	cp $< $@

debian/README.sbcl: contrib/sbcl/README
	cp $< $@

debian/README.macos: contrib/macos/README
	cp $< $@

debian/README.texmacs: contrib/texmacs/README
	cp $< $@

debian/%.lintian-overrides: debian/%.lintian-overrides.in
	cat $< | sed "s,@TARG@,$(shell ls target |head -n 1),g" >$@

# Build architecture-independent files here.
binary-indep: DH_OPTIONS:=-i
binary-indep: debian/installs debian/README.aldor debian/README.sbcl debian/README.macos debian/README.texmacs $(OVERRIDES) debian/fricas_tutorialu
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installchangelogs
	dh_installexamples
	dh_install
	dh_installmenu
	find debian/elpa-fricas -name "*.el" >debian/elpa-fricas.elpa
	echo debian/fricas-pkg.el >> debian/elpa-fricas.elpa
	echo debian/debian-autoloads.el >> debian/elpa-fricas.elpa
	dh_elpa -v
	cd debian/elpa-fricas/usr; \
		export j=$$(dirname $$(find -name fricas-pkg.el)) ; \
		for i in $$(find lib/fricas/emacs -name "*.el") ; do rm $$i && ln -s ../../../$$j/$$(basename $$i) $$i; done
	dh_installman
	dh_link
	dh_strip $(NO_STRIP)
	dh_lintian
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture-dependent files here.
binary-arch: DH_OPTIONS:=-a
binary-arch: debian/installs $(OVERRIDES)
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_installexamples
	dh_install
	dh_installmenu
	dh_installman
	dh_link
	dh_strip $(NO_STRIP)
	dh_lintian
	dh_compress
	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
