# -*- coding: utf-8 -*-
# Copyright (C) 2011  Michał Masłowski  <mtjm@mtjm.eu>
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.


# GNU-style variables for directories where the files will be
# installed.
prefix = /usr/local
datarootdir = $(prefix)/share


# DOCS = README.html NEWS.html

SRCS := $(shell find hclient -name '*.py' -not -name __init__.py \
		-not -name odict.py -not -name test.py)
TESTS := $(shell find tests_hclient -name '*.py')

# Language codes of all translations.
L10N := de es it pl

POS := $(patsubst %,po/%.po,$(L10N))
MOS := $(patsubst %,hclient/mo/%/LC_MESSAGES/hclient.mo,$(L10N))

# # Support both Debian's modified docutils script names and the
# # upstream ones.
# ifdef RST2HTML
# else ifeq ($(shell which rst2html 2>/dev/null),)
# RST2HTML=rst2html.py
# else
# RST2HTML=rst2html
# endif

# Find coverage.
ifdef COVERAGE
else ifneq ($(shell which coverage2 2>/dev/null),)
COVERAGE=coverage2
else ifneq ($(shell which coverage 2>/dev/null),)
COVERAGE=coverage
endif

ifndef PYTHON
ifneq ($(shell which python2 2>/dev/null),)
PYTHON=python2
else
PYTHON=python
endif
endif

# Commands for installation of files.
INSTALL = install
INSTALL_DATA = $(INSTALL) -m 644

# all: $(DOCS) Makefile
all: $(MOS)

# Might be useful in packaging scripts calling setup.py with other
# arguments than in the above rule.
install-data:
	mkdir -p $(DESTDIR)$(datarootdir)/icons/hicolor/scalable/apps
	mkdir -p $(DESTDIR)$(datarootdir)/icons/hicolor/48x48/apps
	mkdir -p $(DESTDIR)$(datarootdir)/icons/hicolor/96x96/apps
	mkdir -p $(DESTDIR)$(datarootdir)/applications
	$(INSTALL_DATA) media/h-client.svg \
		$(DESTDIR)$(datarootdir)/icons/hicolor/scalable/apps/h-client.svg
	for size in 48 96; do \
	$(INSTALL_DATA) media/h-client-$$size.png \
		$(DESTDIR)$(datarootdir)/icons/hicolor/$${size}x$${size}/apps/h-client.png; \
	done
	$(INSTALL_DATA) h-client.desktop \
		$(DESTDIR)$(datarootdir)/applications/h-client.desktop

po/%.po: po/hclient.pot
	@if [ -f $@ ] ; then \
		msgmerge -U $@ po/hclient.pot ; \
	else \
		msginit -o $@ -i po/hclient.pot ; \
	fi

hclient/mo/%/LC_MESSAGES/hclient.mo: po/%.po
	@if ! [ -f hclient/mo/$*/LC_MESSAGES/__init__.py ] ; then \
		mkdir -p hclient/mo/$*/LC_MESSAGES ; \
		for parent in hclient/mo/$* \
			hclient/mo/$*/LC_MESSAGES ; do \
			cp hclient/mo/__init__.py $$parent/ ; \
		done ; \
	fi
	msgfmt -o $@ $<

po/hclient.pot: $(SRCS)
	@xgettext -o $@ $(SRCS)

ifdef COVERAGE
check: $(SRCS) $(TESTS) Makefile
	PYTHONPATH=.:$$PYTHONPATH \
		$(COVERAGE) run --source=hclient --branch \
			-m tests_hclient.__init__
	$(COVERAGE) html --omit='*hclient/odict.py,*hclient/test.py,*hclient/*__init__*'
else
check: $(SRCS) $(TESTS) Makefile
	PYTHONPATH=.:$$PYTHONPATH \
		$(PYTHON) -m tests_hclient.__init__
endif

pyflakes: $(SRCS) Makefile
	@-pyflakes $(SRCS)

pylint-srcs: $(SRCS) Makefile
	@-pylint \
	    --output-format=colorized \
	    --include-ids=y \
	    --report=n \
	    --disable=W0312 \
	    --generated-members=__subclasses__ \
	    $(SRCS)

pylint-tests: $(TESTS) Makefile
	@-pylint \
	    --output-format=colorized \
	    --include-ids=y \
	    --report=n \
	    --disable=W0312 \
	    --disable=W0401 \
	    --disable=C0301 \
	    --max-public-methods=60 \
	    $(TESTS)

pep8: $(SRCS) Makefile
	@-pep8 \
		--ignore=W191 \
		$(SRCS) $(TESTS)

missing-locals: $(SRCS) Makefile
	@# TODO it would be better to check also if the other lines are
	@# present.
	@-bad_files=$$(grep -FL '# Local Variables:' $(SRCS) $(TESTS)) ; \
		if [ ! -z "$${bad_files}" ] ; \
		then echo "The following files don't specify Emacs" \
			"local variables:" ; \
		echo "$${bad_files}" ; \
		exit 1 ; fi

style: pyflakes pylint-srcs pylint-tests pep8 missing-locals Makefile

%.html: %.txt Makefile
	$(RST2HTML) $< $@

.PHONY: all pep8 style check pyflakes pylint pylint-src pylint-tests \
	missing-locals install-data

.SECONDARY: $(POS)
