#!/usr/bin/make -f
# -*- makefile -*-

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

# This has to be exported to make some magic below work.
export DH_OPTIONS

export CONFIG_SHELL=/bin/bash

LIBTOOLVERSION = $(shell dpkg-query -W -f='$${Version}\n' "libtool")

DIST=$(shell lsb_release -i -s)

CONFIG_OPTS = --prefix=/usr \
              --libdir=\$${prefix}/lib \
              --mandir=\$${prefix}/share/man \
              --with-regular-link \
              --enable-devicetables \
              --enable-pyextension \
              --enable-type3 \
              --with-freetype-src

ifeq "$(DIST)" "Ubuntu"
	CONFIG_OPTS += --with-freetype-bytecode
else
	CONFIG_OPTS += --with-freetype-bytecode=no
endif


DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
	CROSS = --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
else
	CROSS = --build $(DEB_BUILD_GNU_TYPE)
endif


configure: configure-stamp
configure-stamp:
	dh_testdir

#ifneq "$(wildcard /usr/share/misc/config.sub)" ""
	cp -f /usr/share/misc/config.sub config.sub
#endif
#ifneq "$(wildcard /usr/share/misc/config.guess)" ""
	cp -f /usr/share/misc/config.guess config.guess
#endif

	cp configure configure.old
	-cp install-sh install-sh.old
	-cp configure.in configure.in.old
	autoreconf -i
	# Now configure fontforge
	#if dpkg --compare-versions $(LIBTOOLVERSION) gt 1.9b; then \
	#	set -e; cp configure.dynamic.in configure.in; libtoolize --force --copy --install; aclocal; autoconf; \
	#else \
	#	set -e; cp configure.dynamic.in configure.in; libtoolize --force --copy; aclocal; autoconf; \
	#fi

	./configure $(CROSS) ${CONFIG_OPTS} CFLAGS="$(CFLAGS)"

	touch $@

configure-nox: configure-stamp-nox
configure-stamp-nox:
	dh_testdir

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

	./configure $(CROSS) ${CONFIG_OPTS} --without-x CFLAGS="$(CFLAGS)"

	touch $@

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

	$(MAKE)
	#$(MAKE) -C fontforge FontForge.pot

	touch $@

build-nox: build-stamp-nox
build-stamp-nox: configure-stamp-nox
	dh_testdir

	$(MAKE)

	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp build-stamp-nox configure-stamp configure-stamp-nox

	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f config.sub config.guess

	[ ! -f configure.old ] || mv -f configure.old configure
	[ ! -f install-sh.old ] || mv -f install-sh.old install-sh
	[ ! -f configure.in.old ] || mv -f configure.in.old configure.in

	rm -f debian/fontforge.sharedmimeinfo debian/fontforge.desktop
	rm -rf Unicode/.libs Unicode/Makefile.in
	rm -rf gdraw/.libs gdraw/Makefile.in
	rm -rf fontforge/Makefile.in gutils/Makefile.in fontforge/FontForge.pot
	rm -f Makefile.in aclocal.m4 ltmain.sh config.log

	dh_clean  --exclude ./fontforge/FontForge-MenuShortCuts.pot.orig --exclude ./fontforge/nowakowskittfinstr.c.orig --exclude ./fontforge/scstyles.c.orig

install: build
	dh_testdir
	dh_testroot
	dh_prep -s --exclude ./fontforge/FontForge-MenuShortCuts.pot.orig --exclude ./fontforge/nowakowskittfinstr.c.orig --exclude ./fontforge/scstyles.c.orig
	dh_installdirs -s

	#$(MAKE) install prefix=$(CURDIR)/debian/fontforge/usr libdir=$(CURDIR)/debian/libfontforge1/usr/lib
	$(MAKE) install prefix=$(CURDIR)/debian/fontforge/usr 
	chrpath -d $(CURDIR)/debian/fontforge/usr/bin/fontforge

	cp -p $(CURDIR)/debian/fontforge/usr/include/fontforge/* $(CURDIR)/debian/libfontforge-dev/usr/include/fontforge
	-rm -rf $(CURDIR)/debian/fontforge/usr/include \
                $(CURDIR)/debian/fontforge/usr/lib/*.la
	#-rm -rf $(CURDIR)/debian/fontforge/usr/include \
    #            $(CURDIR)/debian/libfontforge1/usr/lib/*.la
	mv $(CURDIR)/debian/fontforge/usr/lib/lib*.so $(CURDIR)/debian/libfontforge-dev/usr/lib
	#mv $(CURDIR)/debian/libfontforge1/usr/lib/lib*.so $(CURDIR)/debian/libfontforge-dev/usr/lib
	mv -t $(CURDIR)/debian/libfontforge-dev/usr/lib $(CURDIR)/debian/fontforge/usr/lib/pkgconfig
	#mv -t $(CURDIR)/debian/libfontforge-dev/usr/lib $(CURDIR)/debian/libfontforge1/usr/lib/pkgconfig

	mkdir -p $(CURDIR)/debian/libgdraw4/usr/lib
	mv $(CURDIR)/debian/fontforge/usr/lib/libgdraw.* $(CURDIR)/debian/libgdraw4/usr/lib
	#mv $(CURDIR)/debian/libfontforge1/usr/lib/libgdraw.* $(CURDIR)/debian/libgdraw4/usr/lib

	mv $(CURDIR)/debian/fontforge/usr/share/fontforge/python $(CURDIR)/debian/python-fontforge/usr/share/fontforge

	cp -p desktop/fontforge.desktop debian/fontforge/usr/share/applications/
	mkdir -p debian/fontforge/usr/share/icons/hicolor
	cp -a desktop/icons debian/fontforge/usr/share/icons/hicolor
	-cp -p debian/fontforge.xpm debian/fontforge/usr/share/pixmaps/
	cp desktop/fontforge.xml debian/fontforge.sharedmimeinfo

	dh_install -s
	#set -e; cd pyhook; python setup.py install --prefix=$(CURDIR)/debian/python-fontforge/usr

install-nox: build-nox
	dh_testdir
	dh_testroot

	dh_installdirs -s

	$(MAKE) install prefix=$(CURDIR)/debian/fontforge-nox/usr exec_prefix=$(CURDIR)/debian/fontforge-nox/usr
	-rm -rf $(CURDIR)/debian/fontforge-nox/usr/include
	-rm -rf $(CURDIR)/debian/fontforge-nox/usr/lib
	-rm -rf $(CURDIR)/debian/fontforge-nox/usr/share/fontforge
	chrpath -d $(CURDIR)/debian/fontforge-nox/usr/bin/fontforge

binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_installmenu
	dh_installmime
	dh_installman
	dh_link
	dh_strip --dbg-package=fontforge-dbg
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_python2 -ppython-fontforge
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-arch: build install build-nox install-nox binary-common

binary-indep:

binary: binary-arch
.PHONY: build clean binary-arch binary install configure \
	build-nox configure-nox install-nox
