# be sure to change this name if you build a Docker image
DOCKER-IMAGE:=msr24-work-items

# you might need to update some paths below, but probably not
PYTHON:=python3
VERBOSE:=
DATADIR=$(CURDIR)/mongo-data
MONGO=bin/mongo.sh
ARCHIVEFILE=mongodump-workitems.archive

ZIP:=zip
ZIPOPTIONS:=-u -r
ZIPIGNORES:=-x \*.DS_Store\* -x \*.gitkeep\* -x data/csv/\*
ZENODO_DOWNLOAD=$(PYTHON) bin/zenodo-download.py

DOWNLOAD:=$(PYTHON) bin/download.py $(VERBOSE)
BOATOCSV:=$(PYTHON) bin/boa-to-csv.py

JSONSCHEMA:=check-jsonschema
SED:=sed
MKDIR:=mkdir -p
CP:=cp -f


############################
# DO NOT MODIFY BELOW HERE #
############################

.PHONY: all
all: analysis

.PHONY: data
data: txt csv

include Makefile.study

Makefile.study: study-config.json bin/build-makefile.py
	$(JSONSCHEMA) --verbose --schemafile schemas/0.1.2/study-config.schema.json study-config.json
	$(PYTHON) bin/build-makefile.py > $@


####################
# packaging targets
#
.PHONY: zip package package-replication package-data package-cache package-json package-dump zenodo
zip: package
package: package-replication package-data package-cache package-json package-dump

package-replication:
	@$(CP) requirements.txt requirements.txt.save
	@$(SED) 's/>=/==/g' requirements.txt.save > requirements.txt
	@$(CP) requirements-optional.txt requirements-optional.txt.save
	@$(SED) 's/>=/==/g' requirements-optional.txt.save > requirements-optional.txt
	-$(ZIP) replication-pkg.zip $(ZIPOPTIONS) .vscode/*.json analyses/**/*.py analyses/*.py bin/**/*.py bin/*.py boa/ data/generated/* figures/ schemas/ tables/ jobs.json LICENSE Makefile Dockerfile README.md requirements.txt requirements-optional.txt study-config.json jira-dataset/*.sh jira-dataset/*.py jira-dataset/*.json jira-dataset/*.md -x bin/import.sh $(ZIPIGNORES)
	@$(CP) requirements.txt.save requirements.txt
	@$(CP) requirements-optional.txt.save requirements-optional.txt
	@$(RM) requirements.txt.save  requirements-optional.txt.save
package-data:
	-$(ZIP) data.zip $(ZIPOPTIONS) data/txt/ $(ZIPIGNORES)
package-cache:
	-$(ZIP) data-cache.zip $(ZIPOPTIONS) data/parquet/ $(ZIPIGNORES)
package-json:
	-$(ZIP) data-json.zip $(ZIPOPTIONS) data/json/ $(ZIPIGNORES)
package-dump:
	@bin/export.sh
	-$(ZIP) mongo-dump.zip $(ZIPOPTIONS) bin/import.sh $(ARCHIVEFILE) $(ZIPIGNORES)
	@$(RM) $(ARCHIVEFILE)

.PHONY: docker run-docker
docker:
	@$(CP) requirements.txt requirements.txt.save
	@$(SED) 's/>=/==/g' requirements.txt.save > requirements.txt
	@$(CP) requirements-optional.txt requirements-optional.txt.save
	@$(SED) 's/>=/==/g' requirements-optional.txt.save > requirements-optional.txt
	-docker build -t $(DOCKER-IMAGE):latest .
	@$(CP) requirements.txt.save requirements.txt
	@$(CP) requirements-optional.txt.save requirements-optional.txt
	@$(RM) requirements.txt.save  requirements-optional.txt.save

run-docker: docker
	@$(MKDIR) "$(DATADIR)"
	docker run -it -v $(shell pwd):/study -v "$(DATADIR)":/var/lib/mongodb $(DOCKER-IMAGE):latest

zenodo:
	$(PYTHON) bin/zenodo.py


##########################
# downloading from Zenodo
#
get-data:
	$(ZENODO_DOWNLOAD) data.zip

get-cache:
	$(ZENODO_DOWNLOAD) data-cache.zip

get-json:
	$(ZENODO_DOWNLOAD) data-json.zip

get-mongo:
	$(ZENODO_DOWNLOAD) mongo-dump.zip


################
# clean targets
#
.PHONY: clean clean-figures clean-tables clean-data clean-csv clean-pq clean-txt clean-gen clean-zip clean-mongo clean-all

clean: clean-figures clean-tables
	${RM} -R __pycache__ bin/__pycache__ analyses/**/__pycache__ analyses/__pycache__

clean-figures:
	${RM} figures/**/*.pdf figures/*.pdf figures/**/*.png figures/*.png

clean-tables:
	${RM} tables/**/*.tex tables/*.tex

clean-data: clean-csv clean-pq clean-txt clean-json

clean-csv:
	${RM} data/csv/**/*.csv data/csv/*.csv

clean-pq:
	${RM} data/parquet/**/*.parquet data/parquet/*.parquet

clean-txt:
	${RM} data/txt/**/*.txt data/txt/*.txt

clean-json:
	find data/json -name '*.json' -type f -delete

clean-gen:
	${RM} data/generated/**/* data/generated/*

clean-zip:
	${RM} replication-pkg.zip data.zip data-cache.zip data-json.zip mongo-dump.zip

clean-mongo:
	${RM} -R $(DATADIR)

clean-all: clean clean-data clean-gen clean-zip clean-mongo


#####################
# custom build steps
#
.PHONY: init-mongo gh-commits gh-projects gh-issues github nlp-scores cherrypicks annotation confidence mongo
init-mongo: data
	$(MONGO) analyses/makeJsons.py

gh-commits:
	$(MONGO) analyses/gh-commits.py

gh-projects:
	$(MONGO) analyses/gh-projects.py

gh-issues:
	$(MONGO) analyses/gh-issues.py

github: gh-projects gh-commits gh-issues

nlp-scores:
	$(MONGO) analyses/nlp_model_T5.py

cherrypicks:
	$(MONGO) analyses/cherrypicks.py

annotate:
	$(MONGO) analyses/annotate.py

confidence:
	$(MONGO) analyses/confidence.py

mongo: cherrypicks annotate confidence


##################
# custom analyses
#
.PHONY: score-stats score-stats-reproduce
score-stats-reproduce: analyses/score_stats.py
	$(MONGO) analyses/score_stats.py
score-stats: data score-stats-reproduce

.PHONY: dataset-allstats dataset-allstats-reproduce
dataset-allstats-reproduce: analyses/dataset_allstats.py
	$(MONGO) analyses/dataset_allstats.py
dataset-allstats: data dataset-allstats-reproduce

.PHONY: man-eval man-eval-reproduce
man-eval-reproduce: analyses/man_eval.py
	$(PYTHON) analyses/man_eval.py
man-eval: data man-eval-reproduce

analysis:: score-stats dataset-allstats man-eval
reproduce:: score-stats-reproduce dataset-allstats-reproduce  man-eval-reproduce
