PYTHON ?= python3
VENV ?= ${PYTHON} -m venv
# Všechny flagy, které se s venvem/virtualenvem/... mají volat patří sem. Volá se "${VENV} cesta"
VENV_PATH ?= env
# Musí být definovaná, i kdyby to měla být "."

.PHONY: all venv_check clean install install_web install_venv clean_venv clean_schema run test deploy_test deploy_prod sync_test_media sync_test_db sync_test sync_local_media sync_local_db sync_local

# activate by mělo být předpokladem ke všemu, co volá webový python (i.e. python nasazený do ${VENV}u kvůli webu, např. manage.py)
all:
	@# Just echo:
	# Install je trochu magický:
	# Spusť následující posloupnost příkazů:
	#	make install_venv
	#	. ${VENV_PATH}/bin/activate
	#	make install_web
	#
	# Pokud install_web říká Error: pg_config executable not found. nainstaluj si libpq-dev
	# Pokud chybová hláška obsahuje #include <Python.h>, nainstaluj si python3-dev
	#
	# Až skončíš s vývojem webu, spusť "deactivate". Tím zmizí '(${VENV_PATH})' ze začátku promptu.

venv_check:
	@# Pokud org nemá zapnutý venv, poradíme mu, aby si ho zapnul a spadneme. Jinak nic.
	@expr $$PATH : ".*:*$(shell pwd)/${VENV_PATH}/bin" > /dev/null && exit 0 || echo 'Není zapnutý venv, spusť ". ${VENV_PATH}/bin/activate".\nPokud není venv nainstalovaný, spusť "make install_venv"' && false

clean: clean_venv clean_schema

install: install_web

install_web: venv_check
	@# venv může být příšerně starý, takže nejdříve upgradujeme venvové věci
	pip install --upgrade pip
	pip install --upgrade setuptools
	# Instalace závislostí webu
	pip install -r requirements.txt --upgrade
	# Po vygenerování testdat spusť  ./manage.py loaddata data/*, ať máš menu a další modely
	# Pro synchronizaci flatpages spusť make sync_prod_flatpages

install_venv:
	${VENV} ${VENV_PATH}

clean_venv:
	# Možná není 100% foolproof...
	@test ! ${VENV_PATH} = . || ! echo "Smaž si prosím venv sám, nebudu mazat celý web"
	rm -rfv ${VENV_PATH}
	rm -f pip-selfcheck.json
clean_schema:
	rm -f schema_seminar.pdf schema_all.pdf

run: venv_check
	./manage.py runserver

test: venv_check
	./manage.py test -v2 seminar mamweb

# DB schemata

schema: schema_seminar.pdf schema_all.pdf

schema_seminar.pdf: venv_check
	./manage.py graph_models seminar | dot -Tpdf > schema_seminar.pdf

schema_all.pdf: venv_check
	./manage.py graph_models -a -g | dot -Tpdf > schema_all.pdf

# Deploy to current *mamweb-test* directory
deploy_test: venv_check
	@if [ ${USER} != "mam-web" ]; then echo "Only possible by user mam-web"; exit 1; fi
	@if [ `readlink -f .` != "/aux/akce/mam/www/mamweb-test" ]; then echo "Only possible in directory mamweb-test"; exit 1; fi
	@echo "Installing version from origin/test ..."
	git pull origin test
	git clean -f
	make install
	./manage.py migrate 
	./manage.py collectstatic --noinput
	(chown -R :mam . || true )
	(chmod -R g+rX,go-w . || true )
	@echo Restarting systemd unit
	systemctl --user restart mamweb-test.service
	@echo Done.

# Deploy to current *mamweb-prod* directory
deploy_prod: venv_check
	@if [ ${USER} != "mam-web" ]; then echo "Only possible by user mam-web"; exit 1; fi
	@if [ `readlink -f .` != "/aux/akce/mam/www/mamweb-prod" ]; then echo "Only possible in directory mamweb-prod"; exit 1; fi
	@echo "Backing up production DB ..."
	( cd -P .. && ./backup_prod_db.sh )
	@echo "Installing version from origin/master ..."
	git pull origin master
	git clean -f
	make install
	./manage.py migrate 
	./manage.py collectstatic --noinput
	(chown -R :mam . || true )
	(chmod -R g+rX,go-w . || true )
	@echo Restarting systemd user unit for MaM web
	systemctl --user restart mamweb-prod.service
	@echo Done.


sync_prod_flatpages: venv_check 
	@echo Downloading current version of flatpages from mamweb-prod.
	ssh mam-web@gimli.ms.mff.cuni.cz \
	"cd /akce/mam/www/mamweb-prod; ./manage.py dumpdata flatpages --indent=2 > flat.json"
	rsync -ave ssh mam-web@gimli.ms.mff.cuni.cz:/akce/mam/www/mamweb-prod/flat.json ./flat.json
	@echo "Applying downloaded flatpages."
	./manage.py loaddata flat.json
	@echo "Done."

# Sync test media directory with production
sync_test_media:
	@if [ ${USER} != "mam-web" ]; then echo "Only possible by user mam-web"; exit 1; fi
	@if [ `readlink -f .` != "/aux/akce/mam/www/mamweb-test" ]; then echo "Only possible in /akce/mam/www/mamweb-test"; exit 1; fi
	rsync -av --delete /akce/mam/www/mamweb-prod/media/ ./media

# Sync test database with production database
sync_test_db:
	@if [ ${USER} != "mam-web" ]; then echo "Only possible by user mam-web"; exit 1; fi
	pg_dump mam_test > dump-test-`date +"%Y%m%d_%H%M"`.sql
	pg_dump -Fc mam_prod > dump-prod.sql
	pg_restore -c --if-exists -d mam_test dump-prod.sql
	rm dump-prod.sql
	@echo Done.
	
# Aggresive variant: destroy original mam_test db with 'DROP OWNED BY "mam-web";'
sync_test_db_aggressive:
	@if [ ${USER} != "mam-web" ]; then echo "Only possible by user mam-web"; exit 1; fi
	pg_dump mam_test > dump-test-`date +"%Y%m%d_%H%M"`.sql
	pg_dump -Fc mam_prod > dump-prod.sql
	@# I am not sure which shell is used, so I am calling bash to make sure
	bash -c "psql mam_test <<< 'DROP OWNED BY \"mam-web\";'"
	pg_restore -c --if-exists -d mam_test dump-prod.sql
	rm dump-prod.sql
	@echo Done.

# Sync test with production
# HACK ALERT: using aggressive variant, due to the schemas being too different.
sync_test: sync_test_media sync_test_db_aggressive


# Sync media directory with atrey. Useful for local development with production database
# Does not sync Galerie and CACHE (too huge). 
sync_local_media:
	rsync -ave ssh --exclude Galerie --exclude CACHE\
		mam-web@gimli.ms.mff.cuni.cz:/akce/mam/www/mamweb-prod/media/ ./media/
# Downloads and restores production database to local database. PostgreSQL only. 
sync_local_db:
	scp mam-web@gimli.ms.mff.cuni.cz:`ssh mam-web@gimli.ms.mff.cuni.cz 'ls -v /akce/mam/www/backups/mam_prod-*\.pgdump.xz | tail -n 1'` \
		./last.pgdump.xz
	xz -fd last.pgdump.xz
	pg_restore -c -d mam-prod  last.pgdump

# Sync database and media. See above lines
sync_local: sync_local_media sync_local_db

# Push local compiled Vue to gimli test site
push_compiled_vue_to_test:
	scp vue_frontend/webpack-stats.json mam-web@gimli:/akce/mam/www/mamweb-test/vue_frontend/
	rsync -ave ssh seminar/static/seminar/vue mam-web@gimli:/akce/mam/www/mamweb-test/seminar/static/seminar/
	ssh mam-web@gimli.ms.mff.cuni.cz 'cd /akce/mam/www/mamweb-test/ && . env/bin/activate && ./manage.py collectstatic --noinput'