# Simple recursive makefile  + Makefile for contest

OUT=out
PROBLEMS=./problems
GRAPHICS=graphics# graphics for the batch (series)
PGRAPHICS=$(PROBLEMS)/graphics# graphics for problems
ROOMS_SRC=./rooms

# constants for given year
YEAR := 11
DATE := 2017-02-17

.SUFFIXES:
.PHONY: all clean cleanall tasks solutions results


# == Main targets ==
#all: rules tasks solutions results anketa prezence rooms proof fksdb-imports
all:  tasks proof solutions results
include Makefile-old-graphics.inc
include $(PROBLEMS)/Makefile-fyz.inc

dep: $(PROBLEMS)/Makefile-fyz.inc $(PROBLEMS)/problem-sort.tex
tasks: dep $(OUT)/zadani-obalka.pdf $(OUT)/zadani-balicek.pdf
solutions: dep $(OUT)/reseni.pdf $(OUT)/reseni-print.pdf
results: dep $(OUT)/vysledky.pdf
proof: $(OUT)/korektury.pdf

$(eval PROBS=$(addsuffix }, $(addprefix $${problem_, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68))) # :-(
PROBDEP= $(addprefix $(PROBLEMS)/, $(PROBS))
GRAPHDEP=opr.eps ozn.eps neozn.eps
SERIEDEP=$(PROBDEP) $(addprefix $(GRAPHICS)/, $(GRAPHDEP)) common.tex naboj_team.tex $(PROBLEMS)/sort.tex

TEAMSCOUNT=$(shell cat naboj_team.tex | wc -l)
TEAMSEXTRA=0
#TEAMSCOUNT=1 # DELETE ONLY FOR PRINTING!

XELATEX_WEB=xelatex -output-directory $(@D) -jobname $(basename $(@F)) '\def\UseOption{web}\input{$<}' || (rm -f $@ && false)
XELATEX_PRINT=xelatex -output-directory $(@D) -jobname $(basename $(@F)) '\def\UseOption{print}\def\teamscount{$(TEAMSCOUNT)}\input{$<}' || (rm -f $@ && false)

# TODO { and }
TEX_ESCAPE=sed 's/{\(.\+\)\\/{\1\\textbackslash/' | sed 's/\([\#%&$$_]\)/\\\1/' | sed 's/~/\\textasciitilde/' | sed 's/\^/\\textasciicircum/'
# TODO ugly thing, gets rid of problematic unicode characters
UNICODE_ESCAPE=sed 's/α/$$\\alpha$$/' | sed 's/π/$$\\pi$$/' | sed 's/∨/$$\\vee$$/'

$(PROBLEMS)/Makefile-fyz.inc: $(OUT)/Makefile-fyz.inc
	cp $< $@

$(OUT)/Makefile-fyz.inc: $(PROBLEMS)/problem-sort.tex poradi_uloh.tex
	xelatex -output-directory $(OUT) poradi_uloh.tex

$(OUT)/sort.tex: dep
#	echo "Run \"make dep\" first for make dependencies!"
#	pause

$(PROBLEMS)/sort.tex: $(OUT)/sort.tex
	cp $< $@

XELATEXZO=xelatex -output-directory $(@D) -jobname '$(basename $(@F))' '\def\UseOption{print,obalka}\def\teamscount{$(TEAMSCOUNT)}\input{$<}' || (rm -f $@ && false)
$(OUT)/zadani-obalka.pdf: zadani.tex $(SERIEDEP)
	$(XELATEXZO)
	$(XELATEXZO)

XELATEXZB=xelatex -output-directory $(@D) -jobname '$(basename $(@F))' '\def\UseOption{print,balicek}\def\teamscount{$(TEAMSCOUNT)}\input{$<}' || (rm -f $@ && false)
$(OUT)/zadani-balicek.pdf: zadani.tex $(SERIEDEP)
	$(XELATEXZB)
	$(XELATEXZB)

$(OUT)/reseni.pdf: reseni.tex $(SERIEDEP)
	$(XELATEX_WEB)
	$(XELATEX_WEB)

XELATEX2=xelatex -output-directory $(@D) -jobname '$(basename $(@F))' '\def\classoptions{twoside}\def\UseOption{print}\input{$<}' || (rm -f $@ && false)
$(OUT)/reseni-print.pdf: reseni.tex $(SERIEDEP)
	$(XELATEX2)
	$(XELATEX2)

$(OUT)/vysledky.pdf: vysledky.tex $(SERIEDEP)
	$(XELATEX_PRINT)
	$(XELATEX_PRINT)

$(OUT)/korektury.pdf: korektury.tex $(SERIEDEP)
	$(XELATEX_WEB)
	$(XELATEX_WEB)

clean:
	find $(OUT) -type f -not \( -name "*.pdf" -o -name ".*" \) | xargs rm

cleanall:
	rm -f $(OUT)/*
	rm -f teams.xml teams.tex naboj_team.csv
	rm -f $(PROBLEMS)/Makefile-fyz.inc $(PROBLEMS)/sort.tex
#	ls $(GRAPHICS) $(PGRAPHICS) | grep -v '\(mp\|plt\|eps\|jpg\|png\)$$' | xargs rm
