# This is the Makefile
#
# 4/2/18, PH

# project name
PROJ = org-mode-poster

#####################################
# Usually no edits below this line
#####################################
# Output directory

# Source directory
SRC = src

# External
EXT = ext

# directory for additional pdf files
LIB = lib

POSTER = $(SRC)/$(PROJ)_poster.pdf

# executables
RM = rm -Rf
TEX = xelatex -interaction nonstopmode -shell-escape 
BIBTEX = bibtex
EMACSINIT = $(EXT)/$(PROJ)_dotemacs 
EMACS = emacs -l ../$(EMACSINIT)
EMACSMSARGS = --batch -f org-latex-export-to-latex --kill
EMACSPARGS =  --batch -f org-beamer-export-to-latex --kill
VIEWBIN = pdfview
PDFMERGEBIN = ext/pdfmerge
CPBIN = cp
MKDIRBIN = mkdir

# list org files
ORGFILES = $(SRC)/$(PROJ)_poster.org

# list tex files
TEXFILES = $(ORGFILES:$(SRC)/$(PROJ)_poster.org=$(SRC)/$(PROJ)_poster.tex)


# list additional library files
PDFLIB = $(wildcard $(LIB)/$(PROJ)*.*)

# indicator files to show tex has run
TEXOUTFILES = $(TEXFILES:$(SRC)/%.tex=$(SRC)/%.aux)

# replace tex with pdf to get pdf tex files
PDFTEXFILES = $(TEXOUTFILES:$(SRC)/%.aux=$(SRC)/%.pdf)

# Rule for $(TEXFILES)
# Convert every org file to LaTeX this is done from within the subfolder
# so be careful with relative paths
$(SRC)/%.tex: $(SRC)/%.org $(PDFLIB) $(SRC)/beamerthemeph.sty
	@if [ "$(notdir $<)" = "$(PROJ)_poster.org" ]; then \
		echo "Exporting poster from org to LaTeX" \
		&& cd $(SRC) && $(EMACS) $(PROJ)_poster.org $(EMACSPARGS); \
	fi

# Rule for $(TEXOUTFILES)
# Run every tex file this is done from within the subfolder so be
# careful with relative paths
$(SRC)/%.aux: $(SRC)/%.tex $(PDFLIB)
	cd $(SRC) && $(TEX) $(notdir $<)
	cd $(SRC) && $(TEX) $(notdir $<) 

# Default entry
all: poster 

# make poster
poster: tex

# run tex files
tex: $(TEXOUTFILES) $(TEXFILES) 

viewposter: poster
	pdfview $(POSTER)

.PHONY: clean texclean Rclean

clean: texclean 

texclean: 
	$(RM) $(TEXOUT)/$(PROJ)*.tex
	$(RM) $(TEXOUT)/$(PROJ)*.aux

test:
	@echo $(POSTER) $(TEXFILES) $(ORGFILES)
