SHELL := /bin/bash
RM = rm -rf
.DEFAULT_GOAL := help
ODSA_ENV ?= DEV
# ^ Default env variable, is overridden if already defined, such as ODSA_ENV='PROD'
PYTHON = python3
# ^ Python used for building books:  (can add options like -bb, -u, -Werror ...)
CONFIG_SCRIPT = tools/configure.py
CONFIG_SCRIPT_OPTS = --no-lms
# ^ Starting script for building books.  See --help for option descriptions
PYTHON_LINT = pylint --disable=C --reports=y
JS_LINT = yarn eslint --no-color
CSS_LINT = yarn csslint --quiet --ignore=ids,adjoining-classes
# CSSOLDLINTFLAGS = --quiet --errors=empty-rules,import,errors --warnings=duplicate-background-images,compatible-vendor-prefixes,display-property-grouping,fallback-colors,duplicate-properties,shorthand,gradients,font-sizes,floats,overqualified-elements,import,regex-selectors,rules-count,unqualified-attributes,vendor-prefix,zero-units
JSON_LINT = yarn jsonlint --quiet

.PHONY: help clean webserver

help: ## This help dialog
	@echo '   Welcome to the OpenDSA help-via-Makefile'
	@echo '   To start the OpenDSA container and webserver: docker-compose up'
	@echo '   To jump into the container: docker-compose exec opendsa bash'
	@echo '   Within the container, you can run these make commands:'
	@awk 'BEGIN {FS = ":.*##"; printf "Usage:  make \033[36m<target>\033[0m\nTargets:\n"} /^[a-zA-Z_-]+:.*?##/ { printf "  \033[36m%-10s\033[0m %s\n", $$1, $$2 }' $(MAKEFILE_LIST)
	@echo
	@echo Comprehensive list of books:   $(ALL_BOOKS)
	@echo

clean: ## Deletes all Books (!!!) and minified JS and CSS files
	- $(RM) *~
	- $(RM) Books
	- $(RM) lib/*-min.*
	- $(RM) Doc/*~
	- $(RM) Scripts/*~
	- $(RM) config/*~

webserver: ## Starts the Flask server
	@echo If using the proxy, OpenDSA URL will be: https://opendsa.localhost.devcom.vt.edu
	gunicorn -w 4 -b 0.0.0.0:8080 -t 180 app:app

.PHONY: alllint jsonlint lint lintExe csslint pylint
alllint: lint csslint jsonlint pyLint ## Combines several other linting targets

csslint: ## Runs CSS linter on files within AV/
	@echo 'running csslint'
	@$(CSS_LINT) AV/Background/*.css
	@$(CSS_LINT) AV/Design/*.css

TODOcsslint:
	@$(CSS_LINT) AV/List/*.css
	@$(CSS_LINT) AV/Sorting/*.css
	@$(CSS_LINT) AV/Hashing/*.css
	@$(CSS_LINT) AV/Searching/*.css
	#@$(CSS_LINT) AV/*.css
	@$(CSS_LINT) Doc/*.css
	@$(CSS_LINT) lib/*.css

lint: lintExe ## Runs JS linter on files in AV/ and Exercises/
	@echo 'running eslint'
	-@$(JS_LINT) AV/Background/*.js
	-@$(JS_LINT) AV/Design/*.js

TODOlintAV:
	@echo 'linting AVs'
	-@$(JS_LINT) AV/Binary/*.js
	-@$(JS_LINT) AV/General/*.js
	-@$(JS_LINT) AV/List/*.js
	-@$(JS_LINT) AV/Sorting/*.js
	-@$(JS_LINT) AV/Hashing/*.js
	-@$(JS_LINT) AV/Searching/*.js
	-@$(JS_LINT) AV/Sorting/*.js

lintExe: ## Runs JS linter on files in Exercises/
	@echo 'linting KA Exercises'
	-@$(JS_LINT) Exercises/AlgAnal/*.js
	-@$(JS_LINT) Exercises/Background/*.js
	-@$(JS_LINT) Exercises/Binary/*.js
	-@$(JS_LINT) Exercises/Design/*.js
	-@$(JS_LINT) Exercises/General/*.js
	-@$(JS_LINT) Exercises/Graph/*.js
	-@$(JS_LINT) Exercises/Hashing/*.js
	-@$(JS_LINT) Exercises/Indexing/*.js
	-@$(JS_LINT) Exercises/List/*.js
	-@$(JS_LINT) Exercises/RecurTutor/*.js
	-@$(JS_LINT) Exercises/RecurTutor2/*.js
	-@$(JS_LINT) Exercises/Sorting/*.js

TODOlintlib:
	@echo 'linting libraries'
	-@$(JS_LINT) lib/odsaUtils.js
	-@$(JS_LINT) lib/odsaAV.js
	-@$(JS_LINT) lib/odsaMOD.js
	-@$(JS_LINT) lib/conceptMap.js

jsonlint: ## Runs JSON linter on files in config/ and AV/
	@$(JSON_LINT) AV/Background/*.json
	@$(JSON_LINT) AV/Design/*.json
	@$(JSON_LINT) config/*.json
	@$(JSON_LINT) config/Old/*.json


pyLint: ## Runs python linter on files in tools/ and ODSAextensions/
	$(PYTHON_LINT) tools/*.py RST/ODSAextensions/**/*.py
	# $(PYTHON_LINT) SourceCode/Python/**/*.py # These are python 2!!!

rst2json: ## Runs the rst2json.py utility
	$(PYTHON) tools/rst2json.py

JS_FNAMES = odsaUtils odsaAV odsaKA odsaMOD JSAV timeme
JS_FILES = $(foreach fname, $(JS_FNAMES), lib/$(fname).js)

CSS_FNAMES = site odsaMOD odsaStyle odsaAV odsaKA
CSS_FILES = $(foreach fname, $(CSS_FNAMES), lib/$(fname).css)

CONFIGS := $(wildcard config/*.json)
ALL_BOOKS := $(patsubst config/%.json,%,$(CONFIGS))

SLIDE_BOOKS = $(filter %slides %Slides,$(ALL_BOOKS))
SLIDE_BOOKS += CS5040Master
BOOKS = $(filter-out $(SLIDE_BOOKS),$(ALL_BOOKS))
.PHONY: $(BOOKS) $(SLIDE_BOOKS)

allbooks: Everything CS2 CS3 PL CS3slides CS3notes CS4104 VisFormalLang

BOOK_NAME: ## creates a book based off of config/BOOK_NAME.json
	@echo This is just a fake book name, go try a real one

# A Static-Pattern Rule for making Books
$(BOOKS): % : config/%.json
	$(PYTHON) $(CONFIG_SCRIPT) $< $(CONFIG_SCRIPT_OPTS)
	@echo "Created an eBook in Books/: $@"

$(SLIDE_BOOKS) : % : config/%.json
	$(PYTHON) $(CONFIG_SCRIPT) --slides $< $(CONFIG_SCRIPT_OPTS)
	@echo "Created an Slide-eBook in Books/: $@"


# Target eBooks with unique recipes below:::
CS3notes:
	$(PYTHON) $(CONFIG_SCRIPT) config/CS3slides.json -b CS3notes $(CONFIG_SCRIPT_OPTS)

CS3F18notes:
	$(PYTHON) $(CONFIG_SCRIPT) config/CS3F18slides.json -b CS3F18notes $(CONFIG_SCRIPT_OPTS)

CS5040notes:
	$(PYTHON) $(CONFIG_SCRIPT) config/CS5040slides.json -b CS5040notes $(CONFIG_SCRIPT_OPTS)

CS5040MasterN:
	$(PYTHON) $(CONFIG_SCRIPT) config/CS5040Master.json -b CS5040MasterN $(CONFIG_SCRIPT_OPTS)

CS3SS18notes:
	$(PYTHON) $(CONFIG_SCRIPT) config/CS3SS18slides.json -b CS3SS18notes $(CONFIG_SCRIPT_OPTS)
