PY?=python3
PELICAN?=pelican
PELICANOPTS=

BASEDIR=$(CURDIR)
INPUTDIR=$(BASEDIR)/content
BUILDDIR=$(BASEDIR)/build
OUTPUTDIR=$(BASEDIR)/output
CONFFILE=$(BASEDIR)/pelicanconf.py
PUBLISHCONF=$(BASEDIR)/publishconf.py

SSH_HOST=fangmeier.tech
SSH_PORT=22
SSH_USER=caleb
SSH_TARGET_DIR=/var/www/thesis


DEBUG ?= 0
ifeq ($(DEBUG), 1)
	PELICANOPTS += -D
endif

RELATIVE ?= 0
ifeq ($(RELATIVE), 1)
	PELICANOPTS += --relative-urls
endif

help:
	@echo 'Makefile for a pelican Web site                                           '
	@echo '                                                                          '
	@echo 'Usage:                                                                    '
	@echo '   make html                           (re)generate the web site          '
	@echo '   make pdf                            (re)generate the pdf               '
	@echo '   make clean                          remove the generated files         '
	@echo '   make publish                        generate using production settings '
	@echo '   make serve [PORT=8000]              serve site at http://localhost:8000'
	@echo '   make rsync_upload                   upload the web site via rsync+ssh  '
	@echo '                                                                          '
	@echo 'Set the DEBUG variable to 1 to enable debugging, e.g. make DEBUG=1 html   '
	@echo 'Set the RELATIVE variable to 1 to enable relative urls                    '
	@echo '                                                                          '

build/%.html: content/%.md
	@./convert.py $*.md html

build/%.tex: content/%.md
	@./convert.py $*.md tex

00_html: build/00_abstract.html
01_html: build/01_introduction.html
02_html: build/02_standard_model.html
03_html: build/03_lhc_and_cms_detector.html
04_html: build/04_simulation_and_reconstruction.html
05_html: build/05_tttt_measurement.html
06_html: build/06_pixel_assembly.html
07_html: build/07_conclusion.html

00_tex: build/00_abstract.tex
01_tex: build/01_introduction.tex
02_tex: build/02_standard_model.tex
03_tex: build/03_lhc_and_cms_detector.tex
04_tex: build/04_simulation_and_reconstruction.tex
05_tex: build/05_tttt_measurement.tex
06_tex: build/06_pixel_assembly.tex
07_tex: build/07_conclusion.tex


html: 00_html 01_html 02_html 03_html 04_html 05_html 06_html 07_html
	$(PELICAN) $(BUILDDIR) -o $(OUTPUTDIR) -s $(CONFFILE) $(PELICANOPTS)
	$(PELICAN) $(BUILDDIR) -o $(OUTPUTDIR) -s $(PUBLISHCONF) $(PELICANOPTS)

pdf: 00_tex 01_tex 02_tex 03_tex 04_tex 05_tex 06_tex 07_tex
	cd $(BUILDDIR); rubber thesis
	[ -f $(BUILDDIR)/thesis.pdf ] && cp $(BUILDDIR)/thesis.pdf $(OUTPUTDIR)

clean:
	[ ! -d $(OUTPUTDIR) ] || rm -rf $(OUTPUTDIR)
	cd $(BUILDDIR); git clean -f -X

serve:
ifdef PORT
	cd $(OUTPUTDIR) && $(PY) -m pelican.server $(PORT)
else
	cd $(OUTPUTDIR) && $(PY) -m pelican.server
endif

rsync_upload:
	rsync -e "ssh -p $(SSH_PORT)" -P -rvzc --delete $(OUTPUTDIR)/ $(SSH_USER)@$(SSH_HOST):$(SSH_TARGET_DIR) --cvs-exclude


.PHONY: html pdf help clean serve publish rsync_upload
