.PHONY: clean_env init_env clean_virtualenv install_packages clean install run all schema_seminar.pdf schema_all.pdf sync_test_media sync_test_db sync_test sync_local_media sync_local_db sync_local
PYTHON=python2.7
VE_VER=13.1.2
LOCAL_PYTHON=bin/python

all: install

clean: clean_env

veryclean: clean clean_virtualenv

install: virtualenv bin/python install_packages


# phony, but depends on file
make_env: ${LOCAL_PYTHON}

# phony, but fast repeated execution
install_packages: make_env
	bin/pip install -r requirements.txt --upgrade

# phony
clean_env:
	rm -rf bin/ include/ lib/ local/ share/
	rm -f pip-selfcheck.json
	rm -f schema_seminar.pdf schema_all.pdf

# binary name representing set-up env
${LOCAL_PYTHON}: virtualenv
	${PYTHON} virtualenv/virtualenv.py .



# directory name
virtualenv:
	curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-${VE_VER}.tar.gz 
	tar xvfz virtualenv-${VE_VER}.tar.gz
	mv -T virtualenv-${VE_VER} virtualenv
	rm virtualenv-${VE_VER}.tar.gz

# phony
clean_virtualenv:
	rm -rf virtualenv/
	rm -rf virtualenv-*.tar.gz

run:
	./manage.py runserver

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

# DB schemata

schema: schema_seminar.pdf schema_all.pdf

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

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

# Deploy to current *mamweb-test* directory
deploy_test:
	@if [ ${USER} != "www-mam" ]; then echo "Only possible by user www-mam"; exit 1; fi
	@if [ `pwd` != "/akce/MaM/WWW/mamweb-test" ]; then echo "Only possible in /akce/MaM/WWW/mamweb-test"; exit 1; fi
	@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 Notifying apache about the change ...
	touch mamweb/wsgi.py
	@echo Done.

# Deploy to current *mamweb-prod* directory
deploy_prod:
	@if [ ${USER} != "www-mam" ]; then echo "Only possible by user www-mam"; exit 1; fi
	@if [ `pwd` != "/akce/MaM/WWW/mamweb-prod" ]; then echo "Only possible in /akce/MaM/WWW/mamweb-prod"; exit 1; fi
	@echo "Backing up production DB ..."
	( cd .. && ./backup_prod_db.sh )
	@echo "Installing version from origin/stable ..."
	git pull origin stable
	git clean -f
	make install
	./manage.py migrate 
	./manage.py collectstatic --noinput
	(chown -R :mam . || true )
	(chmod -R g+rX,go-w . || true )
	@echo Notifying apache about the change ...
	touch mamweb/wsgi.py
	@echo Done.


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

# Sync test database with production database
sync_test_db:
	@if [ ${USER} != "www-mam" ]; then echo "Only possible by user www-mam"; exit 1; fi
	pg_dump mam-test -U mam > dump-test-`date +"%Y%m%d_%H%M"`.sql
	pg_dump -Fc mam-prod -U mam > dump-prod.sql
	pg_restore -c -d mam-test -U mam dump-prod.sql
	rm dump-prod.sql
	@echo Done.

# Sync test with production
sync_test: sync_test_media sync_test_db


# 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\
		www-mam@atrey.karlin.mff.cuni.cz:/akce/MaM/WWW/mamweb-prod/media/ ./media/
# Downloads and restores production database to local database. PostgreSQL only. 
sync_local_db:
	scp www-mam@atrey.karlin.mff.cuni.cz:`ssh www-mam@atrey.karlin.mff.cuni.cz 'ls -v /akce/MaM/WWW/backups/mam-prod-*\.pgdump | tail -n 1'` \
		./last.pgdump
	pg_restore -c -d mam -U mam last.pgdump

# Sync database and media. See above lines
sync_local: sync_media sync_db