# '<' (less than) shortcut
lt = $(shell expr $(1) \< $(2))

# python version checks
PYTHONVERSION=$(shell python -c "print __import__('sys').version[:3]")
PYTHONMAJOR=$(shell echo $(PYTHONVERSION) | cut -d'.' -f1)
PYTHONMINOR=$(shell echo $(PYTHONVERSION) | cut -d'.' -f2)
ifeq ($(PYTHONVERSION),)
$(error PycaWM needs Python >= 2.5 and < 3.X)
endif
ifneq ($(PYTHONMAJOR),2)
$(error PycaWM needs Python >= 2.5 and < 3.X, not $(PYTHONVERSION))
endif
ifeq ($(call lt,$(PYTHONMINOR),5),1)
$(error PycaWM needs Python >= 2.5 and < 3.X, not $(PYTHONVERSION))
endif

prefix = /usr/local
XSESSIONDIR=$(DESTDIR)/usr/share/xsessions
BIN=$(DESTDIR)${prefix}/bin
CONFDIR=$(DESTDIR)/etc/xdg
DATADIR=$(DESTDIR)${prefix}/share
MODULEDIR=$(DESTDIR)/usr/lib/python$(PYTHONVERSION)/site-packages

install:
	install -d $(XSESSIONDIR) $(BIN) $(CONFDIR)/PycaWM $(DATADIR)/PycaWM $(MODULEDIR)/pycawm $(MODULEDIR)/pycawm/plugins
	install -m644 PycaWM.desktop $(XSESSIONDIR)
	sed -i "s=/usr/local=$(prefix)=" $(XSESSIONDIR)/PycaWM.desktop
	install -m755 pycawm_launcher utils/pycarepl utils/pycagenconf $(BIN)
	install -m755 conf/config-example.py $(CONFDIR)/PycaWM/config.py
	install -m644 conf/*.png $(DATADIR)/PycaWM
	install -m644 pycawm/*.py $(MODULEDIR)/pycawm
	install -m644 pycawm/plugins/*.py  $(MODULEDIR)/pycawm/plugins
	python -O -c "from compileall import compile_dir; compile_dir('$(MODULEDIR)/pycawm')"


uninstall:
	if [ -f $(XSESSIONDIR)/PycaWM.desktop ] ; then rm -f $(XSESSIONDIR)/PycaWM.desktop; fi
	if [ `ls -al $(XSESSIONDIR) | wc -l` -lt 4 ]; then rmdir $(XSESSIONDIR); fi
	rm -f $(BIN)/pycawm_launcher $(BIN)/pycarepl $(BIN)/pycagenconf
	rm -rf $(MODULEDIR)/pycawm
	rm -rf $(CONFDIR)/PycaWM
	rm -rf $(DATADIR)/PycaWM
	if [ `ls -al $(CONFDIR) | wc -l` -lt 4 ]; then rmdir $(CONFDIR); fi

clean:
	rm -f pycawm/*.py{c,o} pycawm/plugins/*.py{c,o}
	rm -f pycawm/*~ conf/*~ utils/*~ *~
