#!/usr/bin/make -f
# debian/rules for LilyPond in Debian.
#
# This is free software; see the GNU General Public Licence
# version 2 or later for copying conditions.  There is NO warranty.
#

include VERSION

export MAILADDRESS = lilypond@packages.debian.org

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
CFLAGS := $(shell dpkg-buildflags --get CFLAGS)
CXXFLAGS := $(shell dpkg-buildflags --get CXXFLAGS)
#ifneq (,$(filter $(DEB_HOST_ARCH),))
#  config_opt = --disable-optimising
#  CFLAGS := $(filter-out -O%, $(CFLAGS))
#  CXXFLAGS := $(filter-out -O%, $(CXXFLAGS))
#else
ifneq (,$(filter $(DEB_BUILD_OPTIONS),noopt))
  config_opt = --disable-optimising
else
  config_opt = --enable-optimising
endif
#endif

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	CPU_COUNT := $(firstword $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS))))
	DOC_OPTIONS := -j$(CPU_COUNT) CPU_COUNT=$(CPU_COUNT)
endif
DOC_OPTIONS += WEB_TARGETS="offline"

########################################################################
# START: Definitions from the old guile-1.8 (1.8.8+1-10) package
########################################################################

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

ifeq (alpha,$(shell dpkg-architecture -qDEB_HOST_ARCH))
  # The -O2 option breaks make check on alpha right now.
  guile_deb_cflags := -Os -g
else ifeq (sh4,$(shell dpkg-architecture -qDEB_HOST_ARCH))
  # Renesas SH(sh4) needs -mieee.  Without it, test-conversion fails.
  # (Bug: 531378)
  guile_deb_cflags := -O2 -g -mieee
else
  guile_deb_cflags := -O2 -g
endif

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -g
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

guile_configure_args = \
  --host=$(DEB_HOST_GNU_TYPE) \
  --build=$(DEB_BUILD_GNU_TYPE) \
  --with-threads=no \
  --disable-error-on-warning \
  --prefix=/usr

ifeq (hurd-i386,$(shell dpkg-architecture -qDEB_HOST_ARCH))
  guile_configure_args += LIBS=-lpthread
endif

########################################################################
# END: Definitions from the old guile-1.8 (1.8.8+1-10) package
########################################################################

export LILYPOND_VERSION := $(MAJOR_VERSION).$(MINOR_VERSION).$(PATCH_LEVEL)

export GUILE_CFLAGS := -Wdate-time -D_FORTIFY_SOURCE=2 -I$(CURDIR)/out/guile/usr/include
export GUILE_LIBS := -lguile -Wl,-z,relro -lgmp -lcrypt -lm -lltdl -L$(CURDIR)/out/guile/usr/lib
export GUILE_LOAD_PATH := $(CURDIR)/out/guile/usr/share/guile/1.8
export LD_LIBRARY_PATH := $(CURDIR)/out/guile/usr/lib:$(LD_LIBRARY_PATH)
export PATH := $(CURDIR)/out/guile/usr/bin:$(PATH)
export PKG_CONFIG_PATH := $(CURDIR)/guile18

%.reloc: %.reloc.in
	sed < $< > $@ \
		-e 's!@LILYPOND_VERSION@!$(LILYPOND_VERSION)!g'

%.sh: %.sh.in
	sed < $< > $@ \
		-e 's!@DEB_HOST_MULTIARCH@!$(DEB_HOST_MULTIARCH)!g' \
		-e 's!@LILYPOND_VERSION@!$(LILYPOND_VERSION)!g'

# Do not use debhelper's autoreconf, the build currently
# fails with this option and we take care of that
# ourselves anyway.
%:
	dh $@ --without autoreconf --with python3

override_dh_auto_configure:
	$(eval unexport GUILE_LOAD_PATH)
	cd guile18 && ./autogen.sh
	cd guile18 && CFLAGS="$(guile_deb_cflags)" ./configure ${guile_configure_args}
	cd guile18 && $(MAKE) -j$(CPU_COUNT) ${guile_make_args}
	$(MAKE) -C guile18 install DESTDIR=$(CURDIR)/out/guile
	./autogen.sh --noconfigure
	GUILE_LOAD_PATH="$(GUILE_LOAD_PATH)" \
		dh_auto_configure -- --disable-checking --enable-debugging $(config_opt)

execute_after_dh_auto_build-indep:
	$(MAKE) $(DOC_OPTIONS) doc

ifneq (,$(filter $(DEB_HOST_ARCH), hppa hurd-i386 kfreebsd-amd64 kfreebsd-i386))
override_dh_auto_test-arch:
	# Allow tests to fail on select architectures (since 2.20.0):
	#  * hppa: fails at snippet-names-e9c0d84b44e6ce39576ac3a756562e43.ly
	#  * kfreebsd-amd64 and kfreebsd-i386: missing imagemagick.q16 etc.
	#  * hurd-i386: certain tests fail with guile-1.8?
	-dh_auto_test -a
endif

## Unfortunately, lilypond is kind of broken, and installs the wrong
## info documentation (missing images) if we call their makefile
## directly. They also don't provide an install rule which doesn't
## install the documentation, which we don't care about for the
## architecture dependent build
override_dh_auto_install-arch: debian/guile.reloc debian/lilypond.sh debian/lilypond-invoke-editor.sh
	for dir in `echo */*makefile|xargs -n 1 dirname|grep -v Documentation`; do \
		$(MAKE) --no-builtin-rules PACKAGE=LILYPOND package=lilypond -C $$dir install $(DOC_OPTIONS) prefix=$(CURDIR)/debian/tmp/usr/; \
	done
	sed -i -e '1s!$(CURDIR)/out/guile/usr/bin/guile!/usr/bin/guile!' \
		debian/tmp/usr/bin/lilypond-invoke-editor

## We need the non-Documentation arch-independent files installed by
## the arch-dependent "$(MAKE) install" too, hence the dependency
## on override_dh_auto_install-arch
override_dh_auto_install-indep: override_dh_auto_install-arch
	$(MAKE) install-doc $(DOC_OPTIONS) prefix=$(CURDIR)/debian/tmp/usr/
	$(MAKE) install-info $(DOC_OPTIONS) prefix=$(CURDIR)/debian/tmp/usr
	rm -rf $(CURDIR)/debian/tmp/usr/share/omf
	rm -rf $(CURDIR)/debian/tmp/usr/share/doc/lilypond/html/input
	# It seems debian/move_info_images_from_html_doc and
	# debian/symlink_html_images_to_info_images are no longer needed
	# for LilyPond 2.21.82 and 2.22?
	#perl debian/move_info_images_from_html_doc $(CURDIR)/debian/tmp/usr/share/info/ $(CURDIR)/debian/tmp/usr/share/doc/lilypond/html/Documentation/
	#perl debian/symlink_html_images_to_info_images $(CURDIR)/debian/tmp/usr/share/info/lilypond
	perl debian/remove_w3c_callback $(CURDIR)/debian/tmp/usr/share/doc/lilypond/ $(CURDIR)/debian/tmp/usr/share/info/

## we need to install only the jpg, css, ly and english html
## files into the doc-html package, and only the english pdfs into the
## doc-pdf package. Because dh_install doesn't support regexes, we'll
## use find to replace the contents of the .install file before we
## call dh_install
execute_before_dh_install-indep:
	/bin/echo -e 'usr/share/doc/lilypond/html/*/*/*.ly' \
		> $(CURDIR)/debian/lilypond-doc-html.install
	/bin/echo -e 'usr/share/doc/lilypond/html/*/*/*.jpg\nusr/share/doc/lilypond/html/*/*/*.css' \
		>> $(CURDIR)/debian/lilypond-doc-html.install
	find $(CURDIR)/debian/tmp/ -type f \
		-regex '.*usr/share/doc/lilypond/.*/[^\.]+.html' \
		-printf '%P\n' >> $(CURDIR)/debian/lilypond-doc-html.install
	/bin/echo -e ''> $(CURDIR)/debian/lilypond-doc-pdf.install
	find $(CURDIR)/debian/tmp/ -type f \
		-regex '.*usr/share/doc/lilypond/.*/[^\.]+.pdf' \
		-printf '%P\n' >> $(CURDIR)/debian/lilypond-doc-pdf.install
	find $(CURDIR)/debian/tmp/ -type f \
		-regex '.*usr/share/doc/lilypond/.*/[^\.]+.preview.pdf' \
		-printf '%P\n' >> $(CURDIR)/debian/lilypond-doc-pdf.install

override_dh_python3:
	/usr/share/dh-python/dh_python3 --verbose
	/usr/share/dh-python/dh_python3 --verbose usr/share/lilypond/$(MAJOR_VERSION).$(MINOR_VERSION).$(PATCH_LEVEL)/python/

override_dh_compress:
	dh_compress -X.ly -X.pdf

execute_after_dh_installdocs:
	for a in $(wildcard $(CURDIR)/debian/*.doc-base-special); do \
		package=`basename $$a .doc-base-special`; \
		install -d debian/$$package/usr/share/doc-base; \
		install -m0644 $$a debian/$$package/usr/share/doc-base/$$package; \
	done

override_dh_auto_clean:
	[ ! -f guile18/Makefile ] || $(MAKE) -C guile18 maintainer-clean
	dh_auto_clean --no-parallel
	-git restore configure || rm -f configure

override_dh_shlibdeps-arch:
	$(eval unexport LD_LIBRARY_PATH)
	dh_shlibdeps -a -lusr/lib/$(DEB_HOST_MULTIARCH)/lilypond/$(LILYPOND_VERSION)/guile

## this rule will update debian/control and the per-language install
## files; all of the files it generates/modifies should be included in
## the Debian package and should not need to be regenerated or
## modified.
update-doc-packages:
	perl debian/update_doc_packages
