ASSETS=requirements static
DEV_REQUIREMENTS=requirements/development.txt
DJANGO_SETTINGS_MODULE="kasu.settings"
EXCLUDE_FILES=*.pyc
PROJECT_PATH=$(CURDIR)
PYTHON=${VENV_PATH}/bin/python3
REQUIREMENTS=requirements/base.txt
SRC_PATH=${PROJECT_PATH}/src
MANAGE_PY=${PYTHON} -Wa ${SRC_PATH}/manage.py
SSH_LOGIN=kasu@kasu.at
VENV_PATH=$(PROJECT_PATH)/venv

.PHONY: clean venv

all: cleanup migrate testserver

venv: $(VENV_PATH)/bin/activate
$(VENV_PATH)/bin/activate:
	@test -d $(VENV_PATH) || python3 -m venv --clear --system-site-packages $(VENV_PATH)

dev-requirements: venv ${DEV_REQUIREMENTS}
	${PYTHON} -m pip install -qU pip
	${PYTHON} -m pip install -qUr ${DEV_REQUIREMENTS}

requirements: venv ${REQUIREMENTS}
	${PYTHON} -m pip install -qU pip
	${PYTHON} -m pip install -qUr ${REQUIREMENTS}

grunt:
	-grunt

requirements_remote:
	@echo "Installing requirements"
	ssh ${SSH_LOGIN} "virtualenv/bin/pip install -qUr ${REQUIREMENTS}"

sync_assets:
	@echo "Syncing project assets ..."
	rsync -qr --copy-links --delete ${ASSETS} ${SSH_LOGIN}:~/

sync_src:
	@echo "Syncing Sourcecode ..."
	find . -name EXCLUDE_FILES -exec rm -rf {} \;
	rsync -qr --copy-links --delete ${SRC_PATH} ${SSH_LOGIN}:~/ --exclude 'src/kasu/local_settings.py'

restart_remote:
	@echo "Rebuild and reload django..."
	ssh ${SSH_LOGIN} "~/virtualenv/bin/python ~/src/manage.py collectstatic -l --noinput -v0"
	ssh ${SSH_LOGIN} "~/init/kasu restart"

sync: sync_assets requirements_remote sync_src restart_remote

testserver: venv
	${MANAGE_PY} runserver 127.0.0.1:8000

messages: venv
	@echo "aktualisiere Übersetzungen..."
	@(for d in ${SRC_PATH}/*; do    \
    test -d $$d/locale && cd $$d && ${VENV_PATH}/bin/django-admin makemessages -l de;\
	done)
	${MANAGE_PY} compilemessages -v0

cleanup-pyc:
	@echo "lösche den Python Compiler Cache..."
	find ${SRC_PATH} -name ${EXCLUDE_FILES} -exec rm -rf {} \;

collectstatic: venv
	@echo "aktualisiere Statics"
	${MANAGE_PY}  collectstatic --noinput -c -v0

thumbnails: venv
	@echo "Erstelle Vorschaubilder"
	${MANAGE_PY} thumbnail_cleanup

cleanup: requirements messages cleanup-pyc collectstatic thumbnails

migrate: venv
	${MANAGE_PY} migrate

migrations: venv
	${MANAGE_PY} makemigrations
