#!/usr/bin/make -f
#
# debian/rules file to build upmethodology package.
#
# Created 23 Aug 2007, by Stephane Galland <galland@arakhne.org>

#export DH_VERBOSE=1

AUTOLATEX_TRANSLATORS = autolatex-astah autolatex-asymptote autolatex-dia autolatex-geogebra autolatex-gle autolatex-gnuplot autolatex-graphviz autolatex-gz autolatex-libreoffice autolatex-octave autolatex-inkscape autolatex-rsvg autolatex-texify autolatex-visio autolatex-xfig autolatex-umbrello autolatex-pgf

include /usr/share/cdbs/1/rules/buildvars.mk

top_dir      := $(shell pwd)
install_dir  := $(top_dir)/debian/tmp
upstream_dir := $(top_dir)/upstream
build_dir    := $(top_dir)/build_dir
patch_dir    := $(top_dir)/debian/patches

#build_src    := $(build_dir)/autolatex-${DEB_UPSTREAM_VERSION}
build_src    := $(build_dir)/autolatex

build: build-stamp

configure-stamp: unpack-stamp
	dh_testdir
	touch $@

build-stamp: configure-stamp
	dh_testdir

	@mkdir -p "${install_dir}/usr"

	# Force updating of DebConf internationalization
	@cd ${top_dir}/debian && debconf-updatepo

	@cd ${build_src} && perl ./Makefile.PL --prefix="${install_dir}/usr" --disable=gedit2 --disable=sublime2 --disable=gtk compile

	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postrm" -o "autolatex-astah.postrm" -d "astah2png;astah;png;" -d "astah2pdf;astah;pdf;"
	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postinst" -o "autolatex-astah.postinst" -d "astah2png;astah;png;" -d "astah2pdf;astah;pdf;"
	@cd ${top_dir}/debian && cp -f "_translator.templates" "autolatex-astah.templates"

	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postrm" -o "autolatex-asml.postrm" -d "asml2pdf;asml;pdf;"
	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postinst" -o "autolatex-asml.postinst" -d "asml2pdf;asml;pdf;"
	@cd ${top_dir}/debian && cp -f "_translator.templates" "autolatex-asml.templates"

	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postrm" -o "autolatex-asymptote.postrm" -d "asy2pdf;asy;pdf;"
	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postinst" -o "autolatex-asymptote.postinst" -d "asy2pdf;asy;pdf;"
	@cd ${top_dir}/debian && cp -f "_translator.templates" "autolatex-asymptote.templates"

	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postrm" -o "autolatex-pgf.postrm" -d "tikz2pdf;tikz;pdf;"
	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postinst" -o "autolatex-pgf.postinst" -d "tikz2pdf;tikz;pdf;"
	@cd ${top_dir}/debian && cp -f "_translator.templates" "autolatex-pgf.templates"

	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postrm" -o "autolatex-dia.postrm" -d "dia2pdf;dia;pdf;"
	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postinst" -o "autolatex-dia.postinst" -d "dia2pdf;dia;pdf;"
	@cd ${top_dir}/debian && cp -f "_translator.templates" "autolatex-dia.templates"

	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postrm" -o "autolatex-geogebra.postrm" -d "ggb2pdf;ggb;pdf;"
	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postinst" -o "autolatex-geogebra.postinst" -d "ggb2pdf;ggb;pdf;"
	@cd ${top_dir}/debian && cp -f "_translator.templates" "autolatex-geogebra.templates"

	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postrm" -o "autolatex-visio.postrm" -d "vsd2pdf;vsd;pdf;"
	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postinst" -o "autolatex-visio.postinst" -d "vsd2pdf;vsd;pdf;"
	@cd ${top_dir}/debian && cp -f "_translator.templates" "autolatex-visio.templates"

	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postrm" -o "autolatex-gle.postrm" -d "gle2pdf;gle;pdf;" -d "gle2png;gle;png;"
	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postinst" -o "autolatex-gle.postinst" -d "gle2pdf;gle;pdf;" -d "gle2png;gle;png;"  
	@cd ${top_dir}/debian && cp -f "_translator.templates" "autolatex-gle.templates"

	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postrm" -o "autolatex-gnuplot.postrm" -d "plot2pdf;plot;pdf;" -d "plot2pdf+tex;plot;pdf;"
	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postinst" -o "autolatex-gnuplot.postinst" -d "plot2pdf;plot;pdf;" -d "plot2pdf+tex;plot;pdf;"
	@cd ${top_dir}/debian && cp -f "_translator.templates" "autolatex-gnuplot.templates"

	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postrm" -o "autolatex-graphviz.postrm" -d "dot2pdf;dot;pdf;" -d "dot2png;dot;png;" -d "dot2tex;dot;tex;" -d "gxl2pdf;gxl;pdf;" -d "gxl2png;gxl;png;"
	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postinst" -o "autolatex-graphviz.postinst" -d "dot2pdf;dot;pdf;" -d "dot2png;dot;png;" -d "dot2tex;dot;tex;" -d "gxl2pdf;gxl;pdf;" -d "gxl2png;gxl;png;"
	@cd ${top_dir}/debian && cp -f "_translator.templates" "autolatex-graphviz.templates"

	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postrm" -o "autolatex-gz.postrm" -d "imggz2img;gz;img;"
	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postinst" -o "autolatex-gz.postinst" -d "imggz2img;gz;img;"
	@cd ${top_dir}/debian && cp -f "_translator.templates" "autolatex-gz.templates"

	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postrm" -o "autolatex-libreoffice.postrm" -d "odg2pdf;odg;pdf;"
	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postinst" -o "autolatex-libreoffice.postinst" -d "odg2pdf;odg;pdf;"
	@cd ${top_dir}/debian && cp -f "_translator.templates" "autolatex-libreoffice.templates"

	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postrm" -o "autolatex-octave.postrm" -d "oct2pdf;oct;pdf;" -d "oct2png;oct;png;"
	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postinst" -o "autolatex-octave.postinst" -d "oct2pdf;oct;pdf;" -d "oct2png;oct;png;"
	@cd ${top_dir}/debian && cp -f "_translator.templates" "autolatex-octave.templates"

	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postrm" -o "autolatex-inkscape.postrm" -d "svg2png_inkscape;svg;png;inkscape" -d "svg2pdf_inkscape;svg;pdf;inkscape"
	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postinst" -o "autolatex-inkscape.postinst" -d "svg2png_umbrello;svg;png;umbrello" -d "svg2pdf_umbrello;svg;pdf;umbrello"
	@cd ${top_dir}/debian && cp -f "_translator.templates" "autolatex-inkscape.templates"

	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postrm" -o "autolatex-rsvg.postrm" -d "svg2png_rsvg;svg;png;rsvg" -d "svg2pdf_rsvg;svg;pdf;rsvg"
	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postinst" -o "autolatex-rsvg.postinst" -d "svg2png_rsvg;svg;png;rsvg" -d "svg2pdf_rsvg;svg;pdf;rsvg"
	@cd ${top_dir}/debian && cp -f "_translator.templates" "autolatex-rsvg.templates"

	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postrm" -o "autolatex-gimp.postrm" -d "xcf2png;xcf;png;" -d "xcf2pdf;xcf;pdf;"
	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postinst" -o "autolatex-gimp.postinst" -d "xcf2png;xcf;png;" -d "xcf2pdf;xcf;pdf;"
	@cd ${top_dir}/debian && cp -f "_translator.templates" "autolatex-gimp.templates"

	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postrm" -o "autolatex-texify.postrm" -d "cpp2tex_texify;cpp;tex;texify" -d "java2tex_texify;java;tex;texify" -d "lisp2tex_texify;lisp;tex;texify" -d "matlab2tex_texify;m;tex;texify" -d "ml2tex_texify;ml;tex;texify" -d "perl2tex_texify;perl;tex;texify" -d "python2tex_texify;py;tex;texify" -d "ruby2tex_texify;rb;tex;texify" -d "sql2tex_texify;sql;tex;texify"
	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postinst" -o "autolatex-texify.postinst" -d "cpp2tex_texify;cpp;tex;texify" -d "java2tex_texify;java;tex;texify" -d "lisp2tex_texify;lisp;tex;texify" -d "matlab2tex_texify;m;tex;texify" -d "ml2tex_texify;ml;tex;texify" -d "perl2tex_texify;perl;tex;texify" -d "python2tex_texify;py;tex;texify" -d "ruby2tex_texify;rb;tex;texify" -d "sql2tex_texify;sql;tex;texify"
	@cd ${top_dir}/debian && cp -f "_translator.templates" "autolatex-texify.templates"

	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postrm" -o "autolatex-xfig.postrm" -d "fig2pdf;fig;pdf;" -d "fig2pdf+tex;fig;pdf;"
	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postinst" -o "autolatex-xfig.postinst" -d "fig2pdf;fig;pdf;" -d "fig2pdf+tex;fig;pdf;"
	@cd ${top_dir}/debian && cp -f "_translator.templates" "autolatex-xfig.templates"

	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postrm" -o "autolatex-umbrello.postrm" -d "xmi2pdf_umbrello;xmi;pdf;umbrello"
	@cd ${top_dir}/debian && ./make_translator_script -i "_translator.postinst" -o "autolatex-umbrello.postinst" -d "xmi2pdf_umbrello;xmi;pdf;umbrello"
	@cd ${top_dir}/debian && cp -f "_translator.templates" "autolatex-umbrello.templates"

	@touch $@

unpack-stamp:
	dh_testdir
	mkdir -p $(build_src)

	# Force updating of DebConf internationalization
	debconf-updatepo

	# unpack upstream tarballs
	@cd $(build_dir)\
		&& for f in $(upstream_dir)/*.tar.gz ; do\
			echo "unpacking $$f"; \
			tar zxf "$$f"; \
		   done

	# apply any patches
	$(foreach PATCH,$(wildcard $(patch_dir)/*.patch), @cd "${top_dir}"; patch -p0 < "${PATCH}")

	touch $@

binary-indep:
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdirs
	dh_install --sourcedir=debian/tmp --autodest --fail-missing
	dh_installdocs
	dh_installchangelogs
	dh_installman
	dh_link
	dh_installdebconf
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

install: install-stamp

install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k

	@cd ${build_src} && perl ./Makefile.PL --nobinlink "--prefix=${install_dir}/usr" --disable=gedit2 --disable=sublime2 --disable=gtk  install

	@mkdir -p "$(install_dir)/usr/share/python/runtime.d"
	@cp -f "$(top_dir)/debian"/*.rtupdate "$(install_dir)/usr/share/python/runtime.d/"

	touch $@
clean::
	dh_testdir
	dh_testroot
	${RM} *-stamp
	for PACK in ${AUTOLATEX_TRANSLATORS}; do \
		${RM} debian/$$PACK.postrm debian/$$PACK.postinst debian/$$PACK.templates; \
	done
	${RM} -rf ${build_dir}
	dh_clean


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

