.PHONY: help

help: ## This help, enjoy
	@ grep -E '^[a-zA-Z_-]+:.*?.*## .*$$' $(MAKEFILE_LIST) \
	| awk 'BEGIN {FS = ":.*?## "}; \
	{printf "\033[36m%-30s\033[0m -- %s\n", $$1, $$2}'

init-dev-server:
	@ source .venv/bin/activate 1>/dev/null 2>/dev/null && \
	python manage.py migrate && \
	python manage.py create_grouppolicy && \
	python manage.py update_grouppolicy && \
	python manage.py translation_en_rus && \
	python manage.py init_permission


### MAIN SECTION ###
run-dev: ## Run RedAdm-server, redis-server, celery and flower
	@ make run-dev-server 1>/dev/null &
	@ make redis-start 2>/dev/null 1>/dev/null &
	@ make celery-worker 2>/dev/null 1>/dev/null &
	@ make celery-beat 2>/dev/null 1>/dev/null &
	@ make celery-flower 2>/dev/null 1>/dev/null &
	@ echo -e "--->\033[32m REDADM DEV SERVER RUN! \033[0m < with debug message"
	@ echo -e "--->\033[32m REDIS SERVER RUN! \033[0m  < silent"
	@ echo -e "--->\033[32m CELERY WORKER RUN! \033[0m < silent"
	@ echo -e "--->\033[32m CELERY BEAT RUN! \033[0m	 < silent"
	@ echo -e "--->\033[32m FLOWER RUN ON 5566 port! \033[0m	 < silent"

stop: ## Stop all tasks
	@: # TODO - need to stop more correctrly (don't touch system proc)
	@: # pkill -f "python manage.py" 2>/dev/null 1>/dev/null &
	@ pkill -f "celery" 2>/dev/null 1>/dev/null &
	@ pkill -f "flower" 2>/dev/null 1>/dev/null &
	@ pkill -f "gunicorn*" 2>/dev/null 1>/dev/null &
	@ make redis-stop 2>/dev/null 1>/dev/null &
	@ echo -e "--->\033[31m REDADM SERVER STOPPED! \033[0m"
	@ echo -e "--->\033[31m REDIS SERVER STOPPED! \033[0m"
	@ echo -e "--->\033[31m CELERY WORKER STOPPED! \033[0m"
	@ echo -e "--->\033[31m CELERY BEAT STOPPED! \033[0m"
	@ echo -e "--->\033[31m FLOWER STOPPED! \033[0m"
	@ echo -e "\n!!!\033[31m COMRAD RedAdm is stopped \033[0m"

run-dev-server: ## Run RedAdm server with virtualenv
	@: # python backend/manage.py runserver 0.0.0.0:8989
	@: #python manage.py runserver 0.0.0.0:8989 --settings=redadm.settings.dev
	@ source .venv/bin/activate 1>/dev/null 2>/dev/null && \
	python -m gunicorn -c 'configs/gunicorn.dev.py'

run-server: ## Run RedAdm developer server for PRODUCTION DEPLOY!!!
	@: # python backend/manage.py runserver 0.0.0.0:8989
	@ source .venv/bin/activate 1>/dev/null 2>/dev/null && \
	python -m gunicorn -c 'configs/gunicorn.prod.py'

### CELERY SECTION ###
celery-worker: ## Run celery workers
	@: # old command:
	@: # celery -A redadm worker --loglevel=info --pool=prefork -O fair -c 4
	@: # python -m celery -A redadm worker --concurrency 2 --loglevel=info
	@ source .venv/bin/activate && \
	python -m celery -A redadm worker --pool=prefork -O fair --logfile=/var/log/redadm/celery.log

celery-worker-stop: ## Stop celery workers
	@: # old command:
	@: # celery -A redadm worker --loglevel=info --pool=prefork -O fair -c 4
	@ source .venv/bin/activate && \
	pkill -f "celery" 2>/dev/null 1>/dev/null &

celery-beat: ## Run celery beat
	@ source .venv/bin/activate && \
	python -m celery -A redadm beat --loglevel=info

celery-beat-stop: ## Stop celery workers
	@: # old command:
	@: # celery -A redadm worker --loglevel=info --pool=prefork -O fair -c 4
	@ source .venv/bin/activate && \
	pkill -f "celery" 2>/dev/null 1>/dev/null &

celery-flower: ## Run celery web-interface on port:5566
	@ source .venv/bin/activate && \
	python -m celery -A redadm flower --port=5566 &

### REDIS SECTION ###
redis-start: ## Run redis server in background
	@: # TODO - add redis port
	@redis-server 2>/dev/null 1>/dev/null &

redis-stop: ## Stop redis server in background
	@: # TODO - add redis port
	@redis-cli shutdown 2>/dev/null
	@rm dump.rdb 2>/dev/null

### PREPARE SECTION ###
set-secret: ## Run ALL project TESTS and make lint all code
	@ source .venv/bin/activate 1>/dev/null 2>/dev/null && \
	python scripts/secret_key_gen.py

install-hook: ## Pre-install hook, create dirs, set permission
	@: # TODO - change USERADD command
	@ useradd -s /usr/sbin/nologin redadm
	@ mkdir /home/redadm/ 2>/dev/null
	@ mkdir /opt/redadm/ 2>/dev/null
	@ mkdir /var/log/redadm/ 2>/dev/null
	@ chown -R redadm. /home/redadm 2>/dev/null
	@ chown -R redadm. /opt/redadm/ 2>/dev/null
	@ chown -R redadm. /var/log/redadm/ 2>/dev/null

pre-run-hook: ## Pre run hook before start server
	@ source .venv/bin/activate 1>/dev/null 2>/dev/null && \
	python scripts/secret_key_gen.py && \
	python scripts/encrypt_config.py -a "SECRET_KEY" && \
	python manage.py migrate && \
	python manage.py create_grouppolicy && \
	python manage.py update_grouppolicy && \
	python manage.py translation_en_rus && \
	python manage.py init_permission && \
	python manage.py update_1_9_2
switch-db:
	@ source .venv/bin/activate 1>/dev/null 2>/dev/null && \
	python scripts/secret_key_gen.py && \
	python scripts/encrypt_config.py -a "SECRET_KEY" && \
	python manage.py migrate && \
	python manage.py create_grouppolicy && \
	python manage.py update_grouppolicy && \
	python manage.py translation_en_rus && \
	python manage.py init_permission && \
	python manage.py update_1_9_2 &&
	python manage.py migrate_key






encrypt-config-all:
	@ source .venv/bin/activate 1>/dev/null 2>/dev/null && \
	python scripts/encrypt_config.py -a "SECRET_KEY" && \
	python scripts/encrypt_config.py -a "PASSWORD_LDAP" && \
    python scripts/encrypt_config.py -a "DB_PASSWORD"

encrypt-config-key:
	@ source .venv/bin/activate 1>/dev/null 2>/dev/null && \
	python scripts/encrypt_config.py -a "SECRET_KEY" \

encrypt-config-passldap:
	@ source .venv/bin/activate 1>/dev/null 2>/dev/null && \
	python scripts/encrypt_config.py -a "PASSWORD_LDAP"

encrypt-config-passdb:
	@ source .venv/bin/activate 1>/dev/null 2>/dev/null && \
	python scripts/encrypt_config.py -a "DB_PASSWORD"

### TESTS SECTION ###
test: ## Run ALL project TESTS and make lint all code
	@ source .venv/bin/activate 1>/dev/null 2>/dev/null && \
	python manage.py test

### DEPRECTED ###
# run: ## Run RedAdm-server, redis-server, celery and flower
# 	@ make run-server 1>/dev/null &
# 	@ make redis-start 2>/dev/null 1>/dev/null &
# 	@ make celery-worker 2>/dev/null 1>/dev/null &
# 	@ make celery-beat 2>/dev/null 1>/dev/null &
# 	@ make celery-flower 2>/dev/null 1>/dev/null &
# 	@ echo -e "--->\033[32m REDADM SERVER RUN! \033[0m < silent"
# 	@ echo -e "--->\033[32m REDIS SERVER RUN! \033[0m  < silent"
# 	@ echo -e "--->\033[32m CELERY WORKER RUN! \033[0m < silent"
# 	@ echo -e "--->\033[32m CELERY BEAT RUN! \033[0m	 < silent"
# 	@ echo -e "--->\033[32m FLOWER RUN! \033[0m	 < silent"
