# -*-Makefile-*-

depth = .

SUBDIRS = python scripts \
	flower lily \
	mf ly \
	tex ps scm \
	po \
	elisp vim \
	input \
	$(documentation-dir)


## this convoluted construction is necessary, since we don't know the
## value of DOCUMENTATION here.
documentation-dir=$(if $(findstring no,$(DOCUMENTATION)),,Documentation)

SCRIPTS = configure autogen.sh smart-autogen.sh smart-configure.sh
README_FILES = COPYING COPYING.FDL DEDICATION ROADMAP HACKING \
  LICENSE LICENSE.DOCUMENTATION
TOPDOC_FILES = AUTHORS INSTALL README NEWS
TOPDOC_TXT_FILES = $(addprefix $(top-build-dir)/Documentation/topdocs/$(outdir)/,$(addsuffix .txt,$(TOPDOC_FILES)))
IN_FILES := $(call src-wildcard,*.in)

RELEASE_FILES = ChangeLog RELEASE-COMMIT
RELEASE_OUT_FILES = $(RELEASE_FILES:%=$(outdir)/%)
OUT_DIST_FILES += $(RELEASE_OUT_FILES)
INSTALLATION_DIR=$(local_lilypond_datadir)
INSTALLATION_FILES=$(config_make) VERSION
GENERATED_BUILD_FILES=configure aclocal.m4 autogen.sh
# bootstrap stepmake:
#
STEPMAKE_TEMPLATES=toplevel po install
LOCALSTEPMAKE_TEMPLATES=lilypond

include $(depth)/make/stepmake.make

.PHONY: test info website

dist: local-dist $(GENERATED_BUILD_FILES) top-doc refresh-release-files .gitfilelist
	@cd $(top-src-dir) && \
	  if test -n "`find . -name .git`" -a '(' -n "`git diff HEAD | head -1`" \
	    -o -n "`git status | grep -i untracked`" ')'; \
	  then echo "***   dist must been rolled on a clean git tree;"; \
	  echo "uncommitted changes or untracked files detected, aborting."; \
	  exit 1; fi
	rm -rf $(distdir)
	$(MAKE) local-dist $(distdir)
	chmod -R a+r $(distdir)
	chmod  a+x `find $(distdir) -type d -print`
	$(LN) $(TOPDOC_TXT_FILES) $(distdir)
	$(LN) $(GENERATED_BUILD_FILES:%=$(src-dir)/%) .gitfilelist $(distdir)
	cd $(top-src-dir) && xargs $(buildscript-dir)/mass-link hard . $(distdir) <$(top-build-dir)/.gitfilelist
	if test -n "`find $(top-src-dir) -name .git`"; then \
	  COMMIT_DATE=`git --git-dir=$(top-src-dir)/.git log -1 --format=%cD`; \
	  find $(distdir) | xargs touch -d "$$COMMIT_DATE"; fi
	(cd ./$(depth)/$(outdir); $(TAR) -cf -  --owner=0 --group=0 $(DIST_NAME) | gzip -9 > $(DIST_NAME).tar.gz)
	rm -rf $(distdir)

ifneq ($(shell find $(top-src-dir) -name .git),)
.gitfilelist: $(top-src-dir)/.git/index
	cd $(top-src-dir) && git ls-files >$(top-build-dir)/$@
local-clean-filelist:
	rm -f .gitfilelist
else
.gitfilelist: $(src-dir)/.gitfilelist
	cp $< $@
local-clean-filelist:
endif

all: $(outdir)/VERSION

$(outdir)/VERSION: $(config_make) VERSION
	-mkdir -p $(outdir)
	echo $(TOPLEVEL_VERSION) > $@

$(outdir)/ChangeLog: $(outdir)/VERSION
	@echo 'See http://git.savannah.gnu.org/gitweb/?p=lilypond.git;a=log;h=refs/tags/release/$(TOPLEVEL_VERSION)-1' > $@

$(outdir)/RELEASE-COMMIT: # FIXME: any file in $(top-src-dir)/.git/ we can depend on and be sure RELEASE-COMMIT is up to date?
	git --git-dir=$(top-src-dir)/.git show HEAD | head -100 > $@

# junk me as soon as RELEASE-COMMIT FIXME: has been addressed
refresh-release-files:
	test -d $(top-src-dir)/.git && rm -f $(RELEASE_OUT_FILES)
	$(MAKE) $(RELEASE_OUT_FILES)

python-modules:
	$(MAKE) -C scripts/build
	$(MAKE) -C python

top-doc: python-modules

local-clean: local-clean-ChangeLog local-clean-filelist

local-clean-ChangeLog:
	rm -f ChangeLog

info:
	$(foreach d, $(INFO_DIRECTORIES),$(MAKE) -C $(d) out=www info && ) true

install-info: info
	$(MAKE) out=www COPY_INFO_IMAGES=1 install-info-WWW

install-help2man:
	$(MAKE) -C scripts man install-help2man
	$(MAKE) -C lily man install-help2man

install-doc:
	$(MAKE) out=www install-WWW
	$(MAKE) out=www install-info-WWW

uninstall-doc:
	$(MAKE) out=www uninstall-WWW

local-install:
	$(INSTALL) -d $(DESTDIR)$(local_lilypond_datadir)

final-install:
	@true

ifeq ($(out),www)
# All web targets, except info image symlinks and info docs are
# installed in non-recursing target from TOP-SRC-DIR
install-WWW:
	-$(INSTALL) -m 755 -d $(DESTDIR)$(webdir)
	rsync -rl --exclude='*.signature' $(outdir)/offline-root/ $(DESTDIR)$(webdir)
	$(MAKE) -C Documentation omf-local-install

install-info-WWW:
	$(foreach d, $(INFO_DIRECTORIES),$(MAKE) -C $(d) install-info && ) true

uninstall-WWW:
	rm -rf $(DESTDIR)$(webdir)
	-rmdir $(package_docdir)
	$(MAKE) -C Documentation uninstall


# For online docs with content negotiation, issue `make doc WEB_TARGETS=online'
# For both online and offline docs, issue `make doc WEB_TARGETS="offline online"'
WEB_TARGETS = offline

WEB_ROOT_FILES = $(WEB_TARGETS:%=$(outdir)/%-root/index.html)

WEB_TRACKED_FILES = $(filter-out $(outdir)/index.html, \
                    $(shell bash -O nullglob -c "echo $(outdir)/*.{midi,html,pdf,png,jpg,jpeg,txt,ly,ily,signature,css,zip,js,idx,php}")) \
                    $(shell bash -O nullglob -c "echo input/$(outdir)/*.{midi,html,pdf,png,jpg,jpeg,txt,ly,ily,signature,css,zip,js,idx,php}") \
                    $(shell bash -O nullglob -c "echo input/*/$(outdir)/*.{midi,html,pdf,png,jpg,jpeg,txt,ly,ily,signature,css,zip,js,idx,php}") \
                    $(shell bash -O nullglob -c "echo input/*/*/$(outdir)/*.{midi,html,pdf,png,jpg,jpeg,txt,ly,ily,signature,css,zip,js,idx,php}") \
                    $(shell bash -O nullglob -c "echo input/regression/lilypond-book/$(outdir)/*.{info,tex}") \
                    $(shell bash -O nullglob -c "echo Documentation/$(outdir)/*.{midi,html,pdf,png,jpg,jpeg,txt,ly,ily,signature,css,zip,js,idx,php}") \
                    $(shell bash -O nullglob -c "echo Documentation/$(outdir)/*/*.{midi,html,pdf,png,jpg,jpeg,txt,ly,ily,signature,css,zip,js,idx,php}")

WWW-post: $(top-build-dir)/.htaccess $(WEB_ROOT_FILES)

# need UTF8 setting in case this is hosted on a website.
$(top-build-dir)/.htaccess:
	echo -e 'AddDefaultCharset utf-8\nAddCharset utf-8 .html\nAddCharset utf-8 .en\nAddCharset utf-8 .nl\nAddCharset utf-8 .txt\n' > $(top-build-dir)/.htaccess

$(outdir)/offline-root/index.html: $(WEB_TRACKED_FILES)
	$(buildscript-dir)/www_post $(PACKAGE_NAME) $(TOPLEVEL_VERSION) $(outdir) offline
	find $(outdir)/offline-root -type l | xargs rm -f

$(outdir)/online-root/index.html: $(WEB_TRACKED_FILES)
	$(buildscript-dir)/www_post $(PACKAGE_NAME) $(TOPLEVEL_VERSION) $(outdir) online
endif # ifeq ($(out),www)

# For those who cannot for the life in them remember to type
# WEB_TARGETS=..., just bloody make all doc stuff, already.
all-doc:
	$(MAKE) WEB_TARGETS='online offline' doc

tree-prefix = $(outdir)
tree-bin = $(tree-prefix)/bin
tree-lib = $(tree-prefix)/lib
tree-share = $(tree-prefix)/share

# Don't put version numbers here. During development
# they make no sense, and cause EPS files to become invalid.

tree-share-prefix = $(tree-share)/lilypond/current
tree-lib-prefix = $(tree-lib)/lilypond/current

C_DIRS = flower lily
c-clean:
	$(foreach i, $(C_DIRS), $(MAKE) -C $(i) clean &&) true

src-ext = c cc yy ll hh icc py scm tex ps texi itexi tely itely sh

doc-clean: snippets-clean $(tree-share-prefix)/lilypond-force

default: $(config_h) build-dir-setup build-scripts

build-dir-setup: $(tree-share-prefix)/lilypond-force

build-scripts:
	$(MAKE) -C scripts/build

PO_FILES = $(call src-wildcard,$(src-depth)/po/*.po)
HELP_CATALOGS = $(PO_FILES:po/%.po=%)
CATALOGS = $(HELP_CATALOGS:lilypond=)

# Preparing LilyPond tree for build-dir exec
link-tree: $(tree-share-prefix)/lilypond-force

$(tree-share-prefix)/lilypond-force: GNUmakefile $(outdir)/VERSION
	cd $(top-build-dir)/$(outbase) && rm -rf bin lib share
	mkdir -p $(tree-bin)
	mkdir -p $(tree-share-prefix)
	mkdir -p $(tree-lib-prefix)
	mkdir -p $(tree-share-prefix)/elisp
	mkdir -p $(tree-share-prefix)/fonts
	mkdir -p $(tree-share-prefix)/fonts/otf
	mkdir -p $(tree-share-prefix)/fonts/tfm
	mkdir -p $(tree-share-prefix)/fonts/type1
	mkdir -p $(tree-share-prefix)/fonts/svg
	mkdir -p $(tree-share-prefix)/fonts/map
	mkdir -p $(tree-share-prefix)/fonts/enc
	mkdir -p $(tree-share-prefix)/tex
	cd $(tree-bin) && \
		ln -sf ../../lily/$(outconfbase)/lilypond . && \
		for i in abc2ly convert-ly etf2ly lilymidi lilypond-book lilypond-invoke-editor midi2ly musicxml2ly; \
			do ln -sf ../../scripts/$(outconfbase)/$$i . ; done
	cd $(tree-lib-prefix) && \
		ln -s ../../../../python/$(outconfbase) python
	cd $(tree-share-prefix) && \
		ln -s $(top-src-dir)/ly ly && \
		ln -s ../../../../mf mf && \
		ln -s $(top-src-dir)/ps && \
		ln -s ../../../../python/$(outconfbase) python && \
		ln -s $(top-src-dir)/scm && \
		ln -s $(top-src-dir)/scripts scripts
	cd $(tree-share-prefix)/tex && \
		ln -s $(top-src-dir)/tex source && \
		ln -s ../../../../../tex/$(outconfbase) tex-out && \
		true
	cd $(tree-share-prefix)/fonts && \
		ln -s $(top-src-dir)/mf source && \
		true
	-cd $(tree-share-prefix)/elisp && \
		ln -sf ../../../../../elisp/$(outconfbase)/lilypond-words.el . && \
		ln -s $(top-src-dir)/elisp/*.el .
	$(foreach i,$(CATALOGS), \
		(mkdir -p $(tree-share)/locale/$i/LC_MESSAGES && \
		cd $(tree-share)/locale/$i/LC_MESSAGES && \
		ln -sf ../../../../../po/$(outconfbase)/$i.mo lilypond.mo) &&) true
	touch $(tree-share-prefix)/lilypond-force

link-mf-tree: $(tree-share-prefix)/mf-link-tree

$(tree-share-prefix)/mf-link-tree: $(tree-share-prefix)/lilypond-force
	-rm -f $(tree-share-prefix)/fonts/otf/* &&  \
	rm -f $(tree-share-prefix)/fonts/svg/* &&  \
	rm -f $(tree-share-prefix)/fonts/fonts.conf &&  \
	rm -f $(tree-share-prefix)/fonts/tfm/* &&  \
	rm -f $(tree-share-prefix)/fonts/type1/* &&  \
		cd $(tree-share-prefix)/fonts/otf && \
		ln -s ../../../../../../mf/$(outconfbase)/*.otf .
	-cd $(tree-share-prefix)/fonts && \
		ln -s ../../../../../mf/$(outconfbase)/fonts.conf .
	-cd $(tree-share-prefix)/fonts/svg && \
		ln -s ../../../../../../mf/$(outconfbase)/*.svg .
	-cd $(tree-share-prefix)/fonts/svg && \
		ln -s ../../../../../../mf/$(outconfbase)/*.woff .
	-cd $(tree-share-prefix)/fonts/tfm && \
		ln -s ../../../../../../mf/$(outconfbase)/*.tfm .
	-cd $(tree-share-prefix)/fonts/type1 && \
		ln -s ../../../../../../mf/$(outconfbase)/*.pf? .
	touch $(tree-share-prefix)/mf-link-tree

TAGS.make: dummy
	etags -o $@ $(find $(top-src-dir) -name 'GNUmakefile*' -o -name '*.make')

$(config_h): config.hh.in
#
# this is to prevent people from getting
# undefined symbols  when we add them to config.h.in,
# and they blindly run "cvs update; make".
#
	@echo
	@echo ' *** $(config_h) is out of date'
	@echo ' *** Remove it and rerun autogen:'
	@echo '         rm $(config_h); ./autogen.sh'
	@echo
	@false

grand-replace:
	$(MAKE) -C scripts/build
	PATH=$(buildscript-dir):$(PATH) $(buildscript-dir)/grand-replace


################################################################
# testing

RESULT_DIR=$(top-build-dir)/out/test-results


test:
	@echo 'For tracking crashes: use'
	@echo
	@echo '        grep sourcefilename `grep -L systems.texi out/lybook-testdb/*/*log|sed s/log/ly/g`'
	@echo
	$(MAKE) -C input/regression out=test local-test
	$(MAKE) -C input/regression/midi out=test local-test
	$(MAKE) -C input/regression/musicxml out=test local-test
	$(MAKE) -C input/regression/abc2ly out=test local-test
	$(MAKE) -C input/regression/lilypond-book out=test local-test

test-baseline:
	@if test -d .git ; then \
		$(if $(shell git diff), echo "commit before base lining" && false,true) ; \
	fi
	$(MAKE)
	$(MAKE) test
	$(MAKE) out=test -C input/regression local-test-baseline
	$(MAKE) out=test -C input/regression/midi local-test-baseline
	$(MAKE) out=test -C input/regression/musicxml local-test-baseline
	$(MAKE) out=test -C input/regression/abc2ly local-test-baseline
	$(MAKE) out=test -C input/regression/lilypond-book local-test-baseline
	$(MAKE) test-snippets-clean


local-check: test
	rm -rf $(RESULT_DIR)
	mkdir -p $(RESULT_DIR)
	$(buildscript-dir)/output-distance --local-datadir --create-images --output-dir $(RESULT_DIR) \
		input/regression/out-test-baseline input/regression/out-test \
		input/regression/midi/out-test-baseline input/regression/midi/out-test 
	@find input ly -name '*.ly' -print |grep -v 'out.*/' | xargs grep '\\version' -L | grep -v "standard input" |sed 's/^/**** Missing version: /g'


test-redo:
	for a in `cat $(RESULT_DIR)/changed.txt` ; do \
		echo removing $$a* ; \
		rm -f $$a* ;\
	done
	$(MAKE) check

test-clean: test-snippets-clean
	$(MAKE) -C input/regression out=test clean

snippets-clean:
	rm -rf out/lybook-db

test-snippets-clean:
	rm -rf out/lybook-testdb

# we want this separate for security; see CG 4.2.  -gp
website:
	$(MAKE) config_make=$(config_make) \
		top-src-dir=$(top-src-dir) \
		-f $(top-src-dir)/make/website.make \
		website

