# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided this notice is
# preserved.  This file is offered as-is, without any warranty.
# Names of contributors must not be used to endorse or promote products
# derived from this file without specific prior written permission.


# Commands
RM=rm -f
MKDIR=mkdir -p
LATEX2PDF=$(MKDIR) build && cd src && \
	pdflatex -output-format pdf -output-directory ../build

# Package and archive
PACKAGE=telepathwords
SRC_FILES_TO_ARCHIVE=src/*.tex src/*.png makefile


all: text presentation


open-text: text
	xdg-open 'build/text.pdf' &

open-presentation: presentation
	xdg-open 'build/presentation.pdf' &


text: build/text.pdf

build/text.pdf: \
		src/text*.tex \
		src/preamble.fr.tex src/preamble.tex
	$(eval tmp_dir=$(shell mktemp --directory))
	$(LATEX2PDF) -output-directory '${tmp_dir}' 'text.tex'
	$(LATEX2PDF) -output-directory '${tmp_dir}' 'text.tex'
	mv -- '${tmp_dir}/text.pdf' 'build/'


presentation: build/presentation.pdf

build/presentation.pdf: \
		src/presentation*.tex \
		src/preamble.fr.tex src/preamble_beamer.tex src/preamble.tex
	$(eval tmp_dir=$(shell mktemp --directory))
	$(LATEX2PDF) -output-directory '${tmp_dir}' 'presentation.tex'
	$(LATEX2PDF) -output-directory '${tmp_dir}' 'presentation.tex'
	mv -- '${tmp_dir}/presentation.pdf' 'build/'


clean: clean-tmp clean-build
	cd src && make --file=../makefile clean-tmp clean-latex

clean-tmp:
	$(RM) -- \
		*~ .\#* \#* \
		*.swp *.swap *.SWP *.SWAP \
		*.bak *.backup *.BAK *.BACKUP \
		*.sav *.save *.SAV *.SAVE \
		*.autosav *.autosave \
		*.log *.log.* error_log* log/ logs/ \
		.cache/ .thumbnails/ \
		.CACHE/ .THUMBNAILS/

clean-archives:
	$(RM) -rf -- \
		*.deb *.rpm *.exe *.msi *.dmg *.apk *.ipa \
		*.DEB *.RPM *.EXE *.MSI *.DMG *.APK *.IPA \
		*.tar.* *.tgz *.gz *.bz2 *.lz *.lzma *.xz \
		*.TAR.* *.TGZ *.GZ *.BZ2 *.LZ *.LZMA *.XZ \
		*.zip *.7z *.rar *.jar \
		*.ZIP *.7Z *.RAR *.JAR \
		*.iso *.ciso *.img *.gcz *.wbfs \
		*.ISO *.CISO *.IMG *.GCZ *.WBFS

clean-latex:
	$(RM) -rf -- \
		*.pdf *.dvi \
		*.acn *.aux *.bcf *.cut *.fls *.glo *.ist *.lof *.nav *.out \
		*.run.xml *.snm *.toc *.vrb *.vrm *.xdy \
		*.fdb_latexmk *-converted-to.* \
		*.synctex *.synctex.xz \
		*.synctex.gz *.synctex.gzip \
		*.synctex.bz2 *.synctex.bzip \
		*.synctex.lz *.synctex.lzma \
		*.synctex.zip *.synctex.7z *.synctex.rar

clean-build:
	$(RM) -rf -- build/ Build/ BUILD/

clean-git:
	git clean -fdx
