# Define the title of the project
TITLE="Article Template"

# Define the Overleaf directory path in Dropbox
OVERLEAF_DIR="$(HOME)/Dropbox/Apps/Overleaf/$(TITLE)"

# Get the current date in YYYYMMDD format
DATE := $(shell date +%Y%m%d)

# Get the current working directory
WORK_DIR="$(PWD)"

# Default target to build the main PDF
all: main.pdf

# Rule to build a PDF from a .tex file and refs.bib
%.pdf: %.tex refs.bib
	@latexmk -pdf $<  # Compile the .tex file to PDF using latexmk
	@latexmk -c >/dev/null  # Clean up auxiliary files generated by latexmk

# Rule to clean up auxiliary files generated during compilation
clean:
	@rm -rf *.bbl *.dvi *.log *.bak *.aux *.blg *.idx *.ps *.toc *.out *.snm *.nav *.xml *.bcf *.spl *.synctex.gz *~ *.aux *.blg *.fdb_latexmk *.fls *.log*.synctex* *-blx.bib *SAVE-ERROR >/dev/null
	@echo "Junk files removed"

# Rule to clean all files except main.pdf and main.zip
clean-all:
	@find ./ -mindepth 1 -maxdepth 1 ! -name "main.pdf" ! -name "main.zip" -exec rm -rf {} \;

# Rule to remove comments from the main.tex file
remove-comments:
	## Removing comments
	@cp -vp main.tex main-backup.tex  # Backup the original main.tex
	@latexpand --empty-comments main.tex > main-stripped.tex  # Expand the LaTeX file and remove comments
	@sed -i '/^\s*%/d' main-stripped.tex  # Remove lines that are only comments
	@cp -vp main-stripped.tex main.tex  # Replace the original main.tex with the stripped version
	@rm -rf main-stripped.tex  # Remove the temporary stripped file

# Rule to create a zip archive of the project
archive: main.zip

# Rule to create a zip file from .tex and other files
%.zip: %.tex ./*
	#@$(MAKE) remove-comments
	@zip -u -r $@ ./* -x "*.zip" -x "main.pdf" -x "*.synctex.gz" -x "*.bbl" -x "main-*.tex" || \
		if [[ $$? -eq 12 ]]; then echo "Nothing to update"; exit 0; fi
	#@cp -vp main-backup.tex main.tex >/dev/null
	#@rm -rf main-backup.tex >/dev/null

# Rule to update the Overleaf project in Dropbox folder
overleaf:
	#@$(MAKE) clean
	@echo "Updating Overleaf" && rsync -arv --delete --exclude-from=exclude.txt $(WORK_DIR)/ $(OVERLEAF_DIR)

# Rule to update everything (placeholder)
update:
	echo "Update everything"
	#@rsync -arv $(OVERLEAF_DIR)/ $(WORK_DIR)
	@git fetch --all
	@for branch in $$(git branch | sed 's/^.//'); do \
		echo -e "\e[32mUpdate '$$branch' branch\e[0m"; \
		git checkout $$branch; \
		git pull $${1:-origin} $$branch || break; \
	done
	@git checkout master

# Rule to push changes to the Overleaf branch
push-to-overleaf:
	echo "Push to Overleaf"
	git checkout overleaf
	git add --all .
	git commit -S -m "push changes to Overleaf $(shell date +'%Y-%m-%d  %H:%M:%S %Z')"
	git push -u overleaf overleaf:master
	git checkout master

# Rule to push changes to the GitHub master branch
push-to-github:	
	echo "Push to GitHub"
	git checkout master
	git add --all .
	git commit -S -m "push changes to GitHub $(shell date +'%Y-%m-%d  %H:%M:%S %Z')"
	git push -u origin master

# Rule to push changes from the Overleaf branch to GitHub
push-to-github-overleaf:
	git checkout overleaf
	#git add --all .	
	#git commit -S -m "push changes to GitHub overleaf branch $(shell date +'%Y-%m-%d  %H:%M:%S %Z')"
	git push -u origin overleaf
	git checkout master

# Rule to merge the Overleaf branch into the master branch
merge-overleaf-to-master:
	git checkout master
	git merge --no-commit --no-ff --allow-unrelated-histories overleaf
	git commit -S -m "Merge overleaf onto master $(shell date +'%Y-%m-%d  %H:%M:%S %Z')"
	git push -u origin master

# Rule to merge the master branch into the Overleaf branch
merge-master-to-overleaf:
	git checkout overleaf
	git merge --no-commit --no-ff --allow-unrelated-histories master
	rm -rf Makefile make.bat exclude.txt .devcontainer README.md && latexmk -C
	git commit -S -m "Merge master onto overleaf $(shell date +'%Y-%m-%d  %H:%M:%S %Z')"
	git push -u overleaf overleaf:master
	git checkout master
