# Copyright (C) 2013, 2018 The Meme Factory, Inc.  http://www.meme.com/
#
#    This file is part of Gombe-MI.
#
#    Gombe-MI is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Gombe-MI.  If not, see <http://www.gnu.org/licenses/>.
#
# Karl O. Pinc <kop@meme.com>
#
# Makefile for pyramid app setup and maintenance.
# 
# See the INSTALL file for initial setup that must be done by root.
#
# See: http://www.pylonsproject.org/

PYTHON_EXE := python3
PYRAMID_VERSION := 1.9
MODWSGI_VENV := /usr/local/lib/pyramid_env
DEVEL_VENV   := ../devel_venv

VIRTUALENV := virtualenv -p $(PYTHON_EXE)

PROJECT_NAME := GMI_Pyramid
DEVEL_STUFF  := $(PROJECT_NAME).egg-info distribute-0.6.10.tar.gz dist build \
                $(PROJECT_NAME).egg

.PHONY: help
help:
	@echo 'Targets are:'
	@echo '  help               This help'
	@echo '  modwsgi_venv       Create python virtual env into which live'
	@echo '                       app is installed and run with'
	@echo '                       Apache mod_wsgi.'
	@echo '  ../devel_venv      Create local virtual env into which devel'
	@echo '                       app is installed and run.'
	@echo '  install_prod       Install production app into modwsgi_venv.'
	@echo '  configure_prod     Install production.ini into modwsgi_venv.'
	@echo '  install_devel      Install development app into devel_venv.'
	@echo '  run_devel          Run the app in development mode.'
	@echo '  test_devel         Run unit tests in the development venv.'
	@echo '  clean_devel_venv   Destroy devel_venv.'
	@echo '  clean_prod         Destroy modwsgi_venv.'
	@echo '  clean              Destroy all generated development files.'
	@echo '  maintainerclean    Start development entirely over.'
	@echo
	@echo 'Production related changes must be made through this Makefile'
	@echo 'or with a umask of 0002 to support collaboration with gombemi'
	@echo 'Unix group members.'

# Note that we'd like to make modwsgi_venv depend on $(MODWSGI_VENV) and
# have the work done there.  This way other production targets could also
# depend on $(MODWSGI_VENV).  But the directory itself must be made by root.

.PHONY: modwsgi_venv
modwsgi_venv:
	# Allow UPG collaboration.
	umask 0002 ; \
	$(VIRTUALENV) --no-site-packages $(MODWSGI_VENV) ; \
	$(MODWSGI_VENV)/bin/easy_install "pyramid==$(PYRAMID_VERSION)" ; \
	printf '%s\n%s\n%s\n%s\n' \
	       'from pyramid.paster import get_app, setup_logging' \
	       "ini_path = '$(MODWSGI_VENV)/production.ini'"       \
	       'setup_logging(ini_path)'                           \
	       "application = get_app(ini_path, 'main')"           \
	  > $(MODWSGI_VENV)/pyramid.wsgi

.PHONY: make_egg
make_egg:
	rm -rf dist
	$(MODWSGI_VENV)/bin/python setup.py bdist_egg
	ln -sf dist/$(PROJECT_NAME)*.egg $(PROJECT_NAME).egg

.PHONY: install_prod
install_prod: make_egg
	umask 0002 ; \
	$(MODWSGI_VENV)/bin/easy_install $$(readlink $(PROJECT_NAME).egg)

.PHONY: configure_prod
configure_prod:
	umask 0002 ; \
	cp production.ini $(MODWSGI_VENV)

$(DEVEL_VENV):
	# Create venv
	$(VIRTUALENV) --no-site-packages $(DEVEL_VENV)
	# Install pyramid
	$(DEVEL_VENV)/bin/easy_install "pyramid==$(PYRAMID_VERSION)"

.PHONY: install_devel
install_devel: $(DEVEL_VENV)
	$(DEVEL_VENV)/bin/python setup.py develop

.PHONY: test_devel
test_devel:
	$(DEVEL_VENV)/bin/python setup.py test -q

.PHONY: run_devel
run_devel:
	$(DEVEL_VENV)/bin/pserve --reload development.ini

.PHONY: clean_devel_venv
clean_devel_venv:
	rm -rf $(DEVEL_VENV)

.PHONY: clean_prod
clean_prod:
	# Leave the $(MODWSGI_VENV) directory intact since it's ownwed by
	# root and setgid with g=rwx.
	rm -rf $(MODWSGI_VENV)/*

.PHONY: clean
clean:
	rm -rf $(DEVEL_STUFF)

.PHONY: maintainerclean
maintainterclean: clean_devel_venv clean
