# -*- mode:makefile -*-
# Sleipnir - A solution for mass delivery of consumer products.
# Copyright (C) 2010, 2011 Carlos Martín <cmartin@liberalia.net>

# this makefile is based on the one founded into gpodder app.

# where's python
PYTHON:=$(shell which python)
SHELLPATH:=$(PWD)/src/sleipnir/frontends/handset

PROJECT=$(shell export PYTHONPATH=${SHELLPATH}; ${PYTHON} -c "import constants; print constants.__namespace__")
VERSION=$(shell export PYTHONPATH=${SHELLPATH}; ${PYTHON} -c "import constants; print constants.__version__")
RELEASE=$(shell export PYTHONPATH=${SHELLPATH}; ${PYTHON} -c "import constants; print constants.__release__")
MODNAME=$(shell export PYTHONPATH=${SHELLPATH}; ${PYTHON} -c "import constants; print constants.__modname__")
PKGNAME=$(shell export PYTHONPATH=${SHELLPATH}; ${PYTHON} -c "import constants; print constants.__appname__")
ENTRIES=$(shell export PYTHONPATH=${SHELLPATH}; ${PYTHON} -c "import constants; print constants.__entry_point__.split('.')[1]")

# Common directories
RESOURCES_DIR=data
PO_DIR=$(RESOURCES_DIR)/po

# Desktop file
DESKTOP_FILE=$(RESOURCES_DIR)/$(BINNAME).desktop
DESKTOP_FILE_IN=$(addsuffix .in,$(DESKTOP_FILE))

# i18n suport
MESSAGESPOT=$(RESOURCES_DIR)/messages.pot
I18NSOURCES=$(wildcard $(SHELLPATH)/*.py)
I18NDESKTOP=$(addsuffix .h,$(DESKTOP_FILE_IN))

# Binary test script
BINNAME=$(PROJECT)
BINNAME_ENTRY=main
BINNAME_MODULE=$(PROJECT).shell.runner

DEVELOP=$(shell  export PYTHONPATH=$(PWD)/src;${PYTHON} -c "import $(PROJECT).$(ENTRIES)" >/dev/null 2>&1; echo $$?)
DEVELOP_FRONTENDS=src/$(PROJECT)/$(ENTRIES)

all: help
help:
	@echo "make clean       - Get rid of scratch and byte files"
	@echo "make distclean   - Do a 'make clean' + remove 'dist/'"
	@echo "make headlink    - Print URL for the current Git head"
	@echo "make install     - Install on local system"
	@echo "make lint        - Check for coding standard violations & flakes"
	@echo "make messages    - Update messages.pot + .po files + .mo files"
	@echo "make release     - Create source tarball in 'dist/'"
	@echo "make releasetest - Run some tests before the release"
	@echo "make test        - Run $(PKGNAME) in local directory"
	@echo "make unittest    - Run doctests + unittests"

install: messages $(DESKTOP_FILE)
	$(PYTHON) setup.py install

sdist:
release: distclean
	$(PYTHON) setup.py sdist

clean:
	$(PYTHON) setup.py clean
	@rm -rf MANIFEST build .coverage htmlcov $(MESSAGESPOT)		 \
	src/$(PKGNAME).egg-info/ .profile data/locale $(BINNAME) *.egg   \
	$(I18NDESKTOP) $(DESKTOP_FILE)
	@find data -name '*.?[o*]' -exec rm {} \;
	@find . -name '*.py[oc]' -exec rm {} \;
	@find . -name '*~' -exec rm {} \;

distclean: clean
	rm -rf dist


#### i18n Support #####

messages: $(MESSAGESPOT)
	make -C $(PO_DIR)

$(MESSAGESPOT): $(I18NSOURCES) $(I18NDESKTOP)
	xgettext -k_:1 -kN_:1 -kN_:1,2 -o $(MESSAGESPOT) $^

$(DESKTOP_FILE): $(DESKTOP_FILE_IN) $(PO_DIR)
	intltool-merge -d -u data/po $< $@

$(DESKTOP_FILE_IN).h: $(DESKTOP_FILE_IN)
	intltool-extract --quiet --type=gettext/ini $<


#### Test Stuff ####

$(BINNAME):
	@[ -d $(DEVELOP) ] || make "$(DEVELOP_FRONTENDS)"
	@echo -e 							     \
	"#!/usr/bin/python\n#AUTOGENERATED. DO NOT EDIT!!\n"                 \
	"\n\"\"\"Sleipnir Runner \"\"\"\n"				     \
	"\nimport sys\n\nsys.path.insert(0, 'src')"		     	     \
	"\nfrom sleipnir.frontends.handset import constants\n"               \
	"\nfrom setuptools import Distribution"				     \
	"\nfrom pkg_resources import EntryPoint\n"	             	     \
	"\nif __name__ == '__main__':\n"				     \
	"\n    Distribution().fetch_build_eggs(constants.__requires__)"	     \
	"\n    EntryPoint(\"@MODNAME@.application\","			     \
	"\"@PKGNAME@.application\", (\"Application\",)).load(False)"         \
	"\n    exit(EntryPoint(\"@BINNAME@\", \"@BINNAME_MODULE@\","         \
	"(\"@BINNAME_ENTRY@\",)).load(False)())\n" > $(@)

	@sed -i s/@PKGNAME@/$(PKGNAME)/g               $(@)
	@sed -i s/@MODNAME@/$(MODNAME)/g               $(@)
	@sed -i s/@BINNAME@/$(BINNAME)/g               $(@)
	@sed -i s/@BINNAME_ENTRY@/$(BINNAME_ENTRY)/g   $(@)
	@sed -i s/@BINNAME_MODULE@/$(BINNAME_MODULE)/g $(@)
	@chmod +x $(@)

test: $(BINNAME)
	@echo -ne '\033]0;$(BINNAME) $(BINNAME_ENTRY) runner\007'
	@COVERAGE=`which coverage 2>/dev/null`; 		     	     \
	[ -f $$COVERAGE ] && COVERAGE="$$COVERAGE run --branch --timid";     \
	echo $$COVERAGE $(BINNAME);
	@$$COVERAGE $(PWD)/$(BINNAME)

unittest:
	@[ -d $(DEVELOP) ] || make "$(DEVELOP_FRONTENDS)"
	$(PYTHON) setup.py test

releasetest: unittest $(DESKTOP_FILE)
	desktop-file-validate $(DESKTOP_FILE)
	make -C $(PO_DIR) validate


#### Devel Stuff ####

# Check for common & easily catchable Python mistakes.
# Ignore the exit code in pyflakes, so 
# that pep8 is always run when "make lint"
pyflakes:
	-@PYFLAKES=`which pyflakes 2>/dev/null`;	\
	[ -f $$PYFLAKES ] && $$PYFLAKES $(SHELLPATH)

# Check for coding standard violations.
pep8:
	-@PEP8=`which pep8 2>/dev/null`;		\
	if [ -f $$PEP8 ]; then				\
	    find . -name '*.py' -print0 | xargs -0 	\
		$$PEP8 --ignore E221,E222 --repeat;	\
	    find . -name '*.py' -print0 | xargs -0 	\
		$$PEP8 --ignore E221,E222 --statistics;	\
	fi

# Check for coding standard violations & flakes.
lint: pyflakes pep8

# This only works in a Git working commit, and assumes that the local Git
# HEAD has already been pushed to the main repository. It's mainly useful
# for the maintainer to quickly generate a commit link that can be
# posted online in bug trackers and mailing lists.
headlink:
	@echo http://git.liberalia.net/git/commit/`git show-ref HEAD | head -c8`


#### API documentation ####

apidocs:
	pydoctor --add-package $(PROJECT) 		\
		 --make-html --html-output=doc/api 	\
		 --project-name=$(PROJECT)

edit-apidocs:
	pydoctor --add-package $(PROJECT) 		\
		 --make-html --html-output=doc/api 	\
		 --project-name=$(PROJECT)		\
		 --verbose-about=epydoc2stan2 		\
		 --verbose-about=epydoc2stan2 		\
		 --verbose-about=server 		\
		 --verbose-about=server --local-only 	\
		 --server --edit

.PHONY: all test unittest release releasetest install 	\
	manpage clean distclean messages help headlink 	\
	lint pyflakes pep8 sdist
