
LAB_PATH ?= ../lab-windows

SHELL := bash
.ONESHELL:
.SHELLFLAGS := -eu -o pipefail -c
.DELETE_ON_ERROR:
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules

DIFF_VERSION ?= v3.1

main_tex ?= ihsm_paper
brief_tex ?= ihsm_tech_report
sub_stem ?= cant-touch-this-ihsm-paper

VERSION_STRING := $(shell git describe --tags --long --dirty)
SHORT_VERSION_STRING := $(shell git describe --tags)

all: ${main_tex}.pdf ${brief_tex}.pdf

.PHONY: submission-outputs
submission: diff cover-letter ${main_tex}.pdf
	cp ${main_tex}.pdf ${sub_stem}-${SHORT_VERSION_STRING}.pdf
	cp ${main_tex}_diff.pdf ${sub_stem}-${SHORT_VERSION_STRING}-diff.pdf
	#cp cant-touch-this-ihsm-paper-v2.0.pdf cant-touch-this-ihsm-paper-v2.0-original.pdf
	#cp tches-22-01-changes.pdf ${sub_stem}-${SHORT_VERSION_STRING}-cover-letter.pdf
	#rm -f ${sub_stem}-${SHORT_VERSION_STRING}-supplementary.zip
	#zip -r ${sub_stem}-${SHORT_VERSION_STRING}-supplementary.zip ${sub_stem}-${SHORT_VERSION_STRING}-diff.pdf ${sub_stem}-${SHORT_VERSION_STRING}-cover-letter.pdf ${sub_stem}-${SHORT_VERSION_STRING}-revised.pdf cant-touch-this-ihsm-paper-v2.0-original.pdf
	rm -f ${sub_stem}-${SHORT_VERSION_STRING}-latex.zip
	zip -r ${sub_stem}-${SHORT_VERSION_STRING}-latex.zip ${main_tex}.tex ihsm.bib version.tex \
		prototype_pic2.jpg \
		concept_vis_one_axis.pdf \
		attack-robot.pdf \
		ihsm_shaft_countermeasures_a.pdf \
		ihsm_shaft_countermeasures_b.pdf \
		ihsm_shaft_countermeasures_c.pdf \
		proto_3d_design.jpg \
		rotor_stator.jpg \
		mesh_gen_viz.pdf \
		mesh_scan_crop.jpg \
		ir_tx_schema.pdf \
		photolink_schematic.pdf \
		fig-acc-trace-steps-run50.pdf \
		fig-acc-trace-stacked-run50.pdf \
		fig-acc-theory-meas-run50.pdf \
		iacrtrans.cls \
		CC-BY.pdf
	rm -f ${sub_stem}-${SHORT_VERSION_STRING}-supplementary.zip
	cat README_supplementary-in.txt version.tex > README_supplementary.txt
	zip ${sub_stem}-${SHORT_VERSION_STRING}-supplementary.zip README_supplementary.txt
	cd ../prototype
	zip -r $(abspath ${sub_stem}-${SHORT_VERSION_STRING}-supplementary.zip) fw mech_pcbs sensor-analysis \
		--exclude 'fw/upstream/*' 'fw/venv/*' 'sensor-analysis/*.pdf' 'fw/*/.git/*' 'fw/*/__pycache__/*' \
		'fw/build/upstream/*' 'fw/build/src/*' 'fw/build/generated/*' 'fw/build/*.dot' 'fw/build/rotohsm_proto_rotor.*' \
		'sensor-analysis/.ipynb_checkpoints/*' 'fw/test_run.sqlite3'

.PHONY: cover-letter
cover-letter: tches-22-01-changes.pdf

.PHONY: diff
diff: ${main_tex}_diff.pdf

ihsm_paper_diff.tex: ihsm_paper.tex ihsm.bib
	python3 diffinator.py $^ $(DIFF_VERSION) > $@

%.pdf: %.tex ihsm.bib version.tex
	pdflatex -shell-escape $<
	biber $* 
	pdflatex -shell-escape $<

version.tex: ${main_tex}.tex ${brief_tex}.tex ihsm.bib
	echo "${VERSION_STRING}" > $@

resources/%.pdf: $(LAB_PATH)/%.ipynb
	jupyter-nbconvert --to=pdf --output-dir=resources --output=$* --LatexExporter.template_file=resources/nbexport.tplx $^

.PHONY: clean
clean:
	rm -f ${main_tex}.aux ${main_tex}.bbl ${main_tex}.bcf ${main_tex}.log ${main_tex}.blg
	rm -f ${main_tex}.out ${main_tex}.run.xml texput.log 
	rm -f ${main_tex}_diff.*
	rm -f ${brief_tex}.aux ${brief_tex}.bbl ${brief_tex}.bcf ${brief_tex}.log ${brief_tex}.blg
	rm -f ${brief_tex}.out ${brief_tex}.run.xml texput.log 
	rm -f README_supplementary.txt

