#!/usr/bin/make -f

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

ifneq (,$(filter parallel=%,$(subst $(COMMA), ,$(DEB_BUILD_OPTIONS))))
	NJOBS := -j $(subst parallel=,,$(filter parallel=%,$(subst $(COMMA), ,$(DEB_BUILD_OPTIONS))))
endif

default: build

autoinit:
	./bootstrap.sh

clean-stamp:
	rm -f configure-stamp build-stamp install-stamp \
	xfonts-kapl-stamp aplus-fsf-el-stamp aplus-fsf-doc-stamp \
	aplus-fsf-stamp aplus-fsf-dev-stamp

clean: clean-stamp
	dh_testdir
	rm -f debian/buildinfo
	dh_testroot
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f print.ps
	dh_clean
# See /usr/share/doc/autotools-dev/README.Debian.gz
	-test -r /usr/share/misc/config.sub && \
		cp -f /usr/share/misc/config.sub config/config.sub
	-test -r /usr/share/misc/config.guess && \
		cp -f /usr/share/misc/config.guess config/config.guess

confflags = --prefix=/usr --libdir=/usr/lib/aplus-fsf --x-includes=/usr/include --x-libraries=/usr/lib

export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

# see /usr/share/doc/autotools-dev/README.Debian
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	confflags += --build $(DEB_HOST_GNU_TYPE)
else
	confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif


configure: configure-stamp
configure-stamp:
	dh_testdir
	dh_buildinfo generate cat
# Add here commands to configure the package.
	./configure $(confflags)
	touch $@

# CFLAGS := -g -pipe -std=gnu++98 -ffile-prefix-map=$(CURDIR)=.
# CXXFLAGS := -g -pipe -std=gnu++98 -ffile-prefix-map=$(CURDIR)=.
CFLAGS := -g -pipe -std=gnu++98
CXXFLAGS := -g -pipe -std=gnu++98
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
  CFLAGS += -O0
  CXXFLAGS += -O0
else
  CFLAGS += -O2
  CXXFLAGS += -O2
endif
export CFLAGS
export CXXFLAGS
export LDFLAGS

build-stamp: configure
	dh_testdir
	$(MAKE) $(NJOBS)
	touch $@

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp

# CURDIR is set by make
DESTDIR = $(CURDIR)/debian/tmp
export DESTDIR # so it gets passed to sub-makes

install_	:= install -o root -g root -p
install_data	:= install -o root -g root -m 644 -p
install_script	:= install -o root -g root -m 755 -p
install_program	:= install -o root -g root -m 755 -p
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
  install_program += --strip
endif
inst_flags := INSTALL='$(install_)'
inst_flags += INSTALL_DATA='$(install_data)'
inst_flags += INSTALL_SCRIPT='$(install_script)'
inst_flags += INSTALL_PROGRAM='$(install_program)'

# The install step can take quite a while, so use a stamp file for that to
# avoid redoing it unless necessary.
install: build install-stamp
install-stamp:
	dh_testdir
	dh_testroot
	dh_prep
# Install into DESTDIR with upstream's directory structure
	$(MAKE) install $(inst_flags)
	dh_lintian
	touch $@

binary: binary-indep binary-arch

# Architecture-independent
binary-indep: xfonts-kapl aplus-fsf-el aplus-fsf-doc

# Architecture-dependent
binary-arch: aplus-fsf aplus-fsf-dev

xfonts-kapl: install xfonts-kapl-stamp
aplus-fsf-el: install aplus-fsf-el-stamp
aplus-fsf-doc: install aplus-fsf-doc-stamp
aplus-fsf: install aplus-fsf-stamp
aplus-fsf-dev: install aplus-fsf-dev-stamp

export DH_OPTIONS

%-stamp: DH_OPTIONS=-p$(@:%-stamp=%)

xfonts-kapl-stamp:
	dh_testdir
	dh_testroot
	rm -fr debian/xfonts-kapl/ debian/xfonts-kapl.*.debhelper
	dh_installdirs
# Alias file must be <package>.alias; dh_install cannot rename
	cd $(DESTDIR)/usr/fonts/X11/pcf && cp -p Kapl.alias xfonts-kapl.alias
	dh_install
	dh_installdocs
	dh_installchangelogs ChangeLog
	dh_installxfonts
	dh_lintian
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb
	touch $@

aplus-fsf-el-stamp:
	dh_testdir
	dh_testroot
	rm -fr debian/aplus-fsf-el/ debian/aplus-fsf-el.*.debhelper
	dh_installdirs
	dh_install
	dh_installdocs
	dh_installemacsen --flavor=xemacs21
	dh_installchangelogs ChangeLog
	dh_lintian
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb
	touch $@

aplus-fsf-doc-stamp:
	dh_testdir
	dh_testroot
	rm -fr debian/aplus-fsf-doc/ debian/aplus-fsf-doc.*.debhelper
	dh_installdirs
	dh_install
	dh_installdocs
	dh_installchangelogs ChangeLog
	dh_lintian
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb
	touch $@

aplus-fsf-stamp:
	dh_testdir
	dh_testroot
	rm -fr debian/aplus-fsf/ debian/aplus-fsf.*.debhelper
	dh_installdirs
	dh_install
	dh_installdocs
	dh_installexamples
	dh_installmenu
	dh_installman
	dh_installchangelogs ChangeLog
	dh_lintian
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps -- -v debian/aplus-fsf/usr/bin/a+
	dh_gencontrol
	dh_md5sums
	dh_builddeb
	touch $@

aplus-fsf-dev-stamp:
	dh_testdir
	dh_testroot
	rm -fr debian/aplus-fsf-dev/ debian/aplus-fsf-dev.*.debhelper
	dh_installdirs
	dh_install
	dh_installdocs
	dh_installchangelogs ChangeLog
	dh_lintian
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb
	touch $@

.PHONY: configure build install binary-indep binary-arch binary clean \
xfonts-kapl aplus-fsf-el aplus-fsf-doc aplus-fsf aplus-fsf-dev

# Local Variables:
# compile-command: "cd .. && dpkg-buildpackage -uc -us"
# End:
