#!/usr/bin/make -f
# Made with the aid of debmake, by Christoph Lameter,
# based on the sample debian/rules file for GNU hello by Ian Jackson.

DEB_HOST_GNU_TYPE := $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
CFLAGS = -Wall
INSTALLFLAGS = -p
INSTPGMFLAGS =
debug=
ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
CFLAGS += -g
debug=--enable-debug
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
INSTPGMFLAGS += -s
endif

FONTDIR=/usr/share/fonts/truetype

package=mgp

build-arch: build-stamp
build-stamp:
	dh_testdir

	ln -sf /usr/share/misc/config.sub .
	ln -sf /usr/share/misc/config.guess .
	./configure --prefix=/usr $(debug) --enable-locale --enable-freetype --enable-freetype-charset16 --enable-xft2 --disable-vflib --enable-gif $(DEB_HOST_GNU_TYPE)
	#./configure --prefix=/usr $(debug) --enable-locale --disable-freetype --enable-xft2 --disable-vflib --enable-gif --with-m17n-lib $(DEB_HOST_GNU_TYPE)
	xmkmf
	make Makefiles
	# XXX: it take too much time to comile unimap.c with -O
	make CFLAGS="$(CFLAGS) -DFREETYPEFONTDIR=\\\"$(FONTDIR)\\\"" \
		LIBDIR=/etc \
		ctlwords.h unimap.o
	make CFLAGS="-O2 $(CFLAGS) -DFREETYPEFONTDIR=\\\"$(FONTDIR)\\\"" \
		LIBDIR=/etc
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	-rm -f build-stamp install-stamp
	[ ! -f Makefile ] || make distclean
	-rm -f `find . -name "*~"`
	-rm -rf debian/mgp debian/files* core debian/substvars
	-rm -f Makefile Imakefile *._man
	rm -f config.guess config.sub

	dh_clean

install: install-stamp
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	make install DESTDIR=$(CURDIR)/debian/mgp prefix=$(CURDIR)/debian/mgp/usr \
		INSTPGMFLAGS="$(INSTPGMFLAGS)" INSTALLFLAGS="$(INSTALLFLAGS)" \
		LIBDIR=/etc
	make install.man DESTDIR=$(CURDIR)/debian/mgp prefix=$(CURDIR)/debian/mgp/usr \
		INSTALLFLAGS="$(INSTALLFLAGS)" \
		LIBDIR=/etc
	# contrib 
	install -d $(CURDIR)/debian/mgp/usr/share/doc/mgp/contrib/mgp-el
	install -m 755 contrib/eqn2eps.sh \
		contrib/mgp2html.pl contrib/mgp2latex.pl \
		contrib/tex2eps.sh \
		$(CURDIR)/debian/mgp/usr/bin/
	install -m 644 contrib/mgp-el/README \
		$(CURDIR)/debian/mgp/usr/share/doc/mgp/contrib/mgp-el
	install -m 644 contrib/mgp-el/mgp.sty \
		$(CURDIR)/debian/mgp/usr/share/doc/mgp/contrib/mgp-el
	install -m 644 contrib/mgp-el/sample.tex \
		$(CURDIR)/debian/mgp/usr/share/doc/mgp/contrib/mgp-el
	install -m 644 contrib/mgp-mode.el contrib/mgp-el/mgp.el \
		$(CURDIR)/debian/mgp/usr/share/emacs/site-lisp/mgp
	-rm $(CURDIR)/debian/mgp/etc/mgp/*
	cp debian/default.mgp $(CURDIR)/debian/mgp/etc/mgp/default.mgp

	#Rename shell scripts to remove .sh extension.
	mv $(CURDIR)/debian/mgp/usr/bin/eqn2eps.sh \
		$(CURDIR)/debian/mgp/usr/bin/eqn2eps
	mv $(CURDIR)/debian/mgp/usr/bin/tex2eps.sh \
		$(CURDIR)/debian/mgp/usr/bin/tex2eps
	mv $(CURDIR)/debian/mgp/usr/bin/mgp2html.pl \
		$(CURDIR)/debian/mgp/usr/bin/mgp2html
	mv $(CURDIR)/debian/mgp/usr/bin/mgp2latex.pl \
		$(CURDIR)/debian/mgp/usr/bin/mgp2latex

	touch install-stamp

# Build architecture-independent files here.
binary-indep: build-indep
# We have nothing to do by default.
build-indep:

# Build architecture-dependent files here.
binary-arch: build-arch install
#	dh_testversion
	dh_testdir
	dh_testroot
	dh_installdocs COPYRIGHT COPYRIGHT.jp FAQ \
		README README.fonts README.fonts.jp README.lang \
		SYNTAX TODO.jp USAGE USAGE.jp 
	dh_installexamples `find sample -type f ! -name '*akefile*' ! -name .cvsignore -print` debian/mgp-print6
	dh_installmenu
	dh_installmime
	dh_installcron
#	dh_installman 
	dh_installinit
	dh_installemacsen
	dh_installchangelogs RELNOTES
#	dh_movefiles
	dh_strip
	dh_compress --exclude=examples
	dh_fixperms
#	dh_suidregister
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

source diff:
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

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