PKG_NAME = axmsg
PY_PKG_NAME = axmsg

REPORT_DIR := $(srcdir)./reports

#-------------------------------------------------------------------------------

SHELL := /bin/bash

#-------------------------------------------------------------------------------

# BLD_PY_VER - is set from the environment
# It controls which version of Python is used to run tests, pylint, etc, by
# controlling which virtualenv is expected.
# Its value can be once of: 27, 32, 34
# Note: 34 is not available on Debian 7
# Note: 32 is not available on Debian 8

# Note these two are recursive variables
VIRTUALENV_DIR = $(VIRTUALENV_DIR_BASE)_$(BLD_PY_VER)
VENV_CONF = $(srcdir)./virtualenv_$(BLD_PY_VER).conf

VIRTUALENV_DIR_BASE := ./.build_venv
VENV_PREP := $(srcdir)./virtualenv-prep.sh

#-------------------------------------------------------------------------------

default:
	@echo "Nothing is built by default. Please inspect Makefile for targets and their intended purposes."

$(REPORT_DIR):
	mkdir -p $(REPORT_DIR)

# virtualenv setup targets
venv: $(VIRTUALENV_DIR)/bin/activate

$(VIRTUALENV_DIR)/bin/activate: $(VENV_PREP) $(VENV_CONF)
	@echo "Creating or recreating virtualenv..."
	@# VIRTUAL_ENV is exported and prepended to PATH by virtualenv
	@# VIRTUALENV_DIR is defined in this Makefile
	@# Temporarily remove the VIRTUAL_ENV from PATH (if present), then proceed
	@export VIRTUALENV_DIR=$(VIRTUALENV_DIR) \
		export PKG_NAME=$(PKG_NAME) \
		export PATH=$${PATH#$$VIRTUAL_ENV/bin:}; \
		export PATH=$${PATH#$$VIRTUAL_ENV/bin:}; \
		$(VENV_PREP) --yes $(VENV_CONF)

confirm_in_venv:
	@if [ -z "$(BLD_PY_VER)" ] ; \
	then \
		echo; \
		echo "**************************************************************"; \
		echo; \
		echo " *** No BLD_PY_VER environment variable defined. ***"; \
		echo; \
		echo -en "BLD_PY_VER determines which Python version is" \
		echo -en " being used to run tests, pylint, etc.\n"; \
		echo -en 'The options are "27", "32" and "34"'; \
		echo -en ' Note that only 34 is not available on Debian 7, and 32 is'; \
		echo -en " not available on Debian 8.\n"; \
		echo      "Here's an example for how to run this: "; \
		echo; \
		echo "    export BLD_PY_VER=27"; \
		echo '    source ./build_venv_$$BLD_PY_VER/bin/activate'; \
		echo "    make $(MAKECMDGOALS)"; \
		echo; \
		echo "**************************************************************"; \
		exit 1; \
	fi
	@if ! [ -d "$(VIRTUALENV_DIR)" ] ; \
	then \
		echo; \
		echo "**************************************************************"; \
		echo; \
		echo "                 *** No virtualenv ***"; \
		echo; \
		echo -en "Please run the following command to prepare the Python"; \
		echo     " virtualenv required for this BLD_PY_VER:"; \
		echo; \
		echo "    VIRTUALENV_DIR=$(VIRTUALENV_DIR) PKG_NAME=$(PKG_NAME) $(VENV_PREP) $(VENV_CONF)"; \
		echo; \
		echo "**************************************************************"; \
		exit 1; \
	fi
	@python -c 'import sys; print(sys.real_prefix)' &>/dev/null || (\
		echo "**************************************************************"; \
		echo; \
		echo "        *** You are not running in a virtualenv. ***"; \
		echo; \
		echo "Please run this command before running make $(MAKECMDGOALS):"; \
		echo ; \
		echo "    source $(VIRTUALENV_DIR)/bin/activate"; \
		echo ; \
		echo "**************************************************************"; \
		exit 1)
	@ABS_VENV_DIR="$$(cd $(VIRTUALENV_DIR); pwd)" ; \
	if ! [[ $$PATH =~ ^$$ABS_VENV_DIR/bin.* ]] ; \
	then \
		echo "**************************************************************"; \
		echo; \
		echo "   *** You are not running in the correct virtualenv. ***"; \
		echo; \
		echo "Please run this command before running make $(MAKECMDGOALS): "; \
		echo ; \
		echo "    source $(VIRTUALENV_DIR)/bin/activate"; \
		echo ; \
		echo "**************************************************************"; \
		exit 1 ; \
	fi

pylint: confirm_in_venv venv $(REPORT_DIR)
	@echo "Running pylint now..."
	@pylint --rcfile=$(srcdir)./pylintrc $(PKG_NAME) > \
			$(REPORT_DIR)/pylint.out; \
		echo "pylint return code (31 or less is OK): $$?"
	@echo "pylint report written to $(REPORT_DIR)/pylint.out"

test: confirm_in_venv venv $(REPORT_DIR)
	nosetests --config ./nose.cfg || exit 1

clean:
	rm -rf reports
	rm -rf .coverage
	rm -rf *.egg-info
	rm -rf .eggs
	find . -name "*.egg" -delete
	find . -name "*.pyc" -delete
	find . -name "__pycache__" -delete

.PHONY: test clean
