#
# Makefile for WiFi Radar
#

SHELL = /bin/sh

#
# These settings are what I would expect for most modern Linux distros, 
# and are what work for me unmodified on Ubuntu. -BEF-
# 
package 	= wifi-radar
sbindir 	= $(DESTDIR)/sbin
initdir 	= $(DESTDIR)/etc/init.d
sysconfdir 	= $(DESTDIR)/etc/
mandir 		= $(DESTDIR)/share/man
pixmapsdir 	= $(DESTDIR)/share/pixmaps
appsdir 	= $(DESTDIR)/share/applications

srcdir = .
top_srcdir = .
top_builddir = .

VERSION = $(shell cat $(srcdir)/docs/VERSION)

BRANCH = $(shell echo v$(shell cat $(srcdir)/docs/VERSION | cut -d. -f1-2).x)

TOPDIR := $(CURDIR)


.PHONY: all install tarball dev-docs changelog clean distclean

all:	wifi-radar.localized

install:	all
	test -d ${sysconfdir} || install -d -m 755 ${sysconfdir}
	
	test -d ${sbindir} || install -d -m 755 ${sbindir}
	install -m 755 wifi-radar.localized ${sbindir}/wifi-radar
	
	test -d ${mandir}/man1 || install -d -m 755 ${mandir}/man1
	install -m 644 man/man1/wifi-radar.1 ${mandir}/man1
	
	test -d ${mandir}/man5 || install -d -m 755 ${mandir}/man5
	install -m 644 man/man5/wifi-radar.conf.5 ${mandir}/man5
	
	test -d ${pixmapsdir} || install -d -m 755 ${pixmapsdir}
	install -m 644 pixmaps/wifi-radar.svg ${pixmapsdir}
	install -m 644 pixmaps/wifi-radar.png ${pixmapsdir}
	
	test -d ${appsdir} || install -d -m 755 ${appsdir}
	install -m 644 wifi-radar.desktop ${appsdir}


wifi-radar.localized:	wifi-radar
	cp wifi-radar wifi-radar.tmp
	perl -pi -e 's#^CONF_FILE\s+=.*#CONF_FILE = "${sysconfdir}/wifi-radar.conf"#' wifi-radar.tmp
	mv wifi-radar.tmp wifi-radar.localized

tarball: $(TOPDIR)/tmp/${package}-$(VERSION).tar.bz2.sign $(TOPDIR)/tmp/${package}-$(VERSION).tar.bz2.sha256

$(TOPDIR)/tmp/${package}-$(VERSION).tar.bz2.sign: $(TOPDIR)/tmp/${package}-$(VERSION).tar.bz2
	cd $(TOPDIR)/tmp && gpg --detach-sign -a --output ${package}-$(VERSION).tar.bz2.asc ${package}-$(VERSION).tar.bz2
	cd $(TOPDIR)/tmp && chmod 644 ${package}-$(VERSION).tar.bz2.asc
	cd $(TOPDIR)/tmp && gpg --verify ${package}-$(VERSION).tar.bz2.asc

$(TOPDIR)/tmp/${package}-$(VERSION).tar.bz2.sha256:
	cd $(TOPDIR)/tmp && sha256sum ${package}-$(VERSION).tar.bz2 > ${package}-$(VERSION).tar.bz2.sha256

$(TOPDIR)/tmp/${package}-$(VERSION).tar.bz2:
	rm -fr $(TOPDIR)/tmp
	mkdir -p $(TOPDIR)/tmp/
	git archive --format=tar --prefix=${package}-$(VERSION)/ $(BRANCH) | (cd $(TOPDIR)/tmp/ && tar xf -)
	find $(TOPDIR)/tmp/${package}-$(VERSION) -type f -exec chmod ug+r  {} \;
	find $(TOPDIR)/tmp/${package}-$(VERSION) -type d -exec chmod ug+rx {} \;
	perl -pi -e 's#^WIFI_RADAR_VERSION\s+=.*#WIFI_RADAR_VERSION = "${VERSION}"#' $(TOPDIR)/tmp/${package}-$(VERSION)/wifi-radar
	chmod 755 $(TOPDIR)/tmp/${package}-$(VERSION)/wifi-radar
	cd $(TOPDIR)/tmp && tar -ch ${package}-$(VERSION) | bzip2 > ${package}-$(VERSION).tar.bz2
	cd $(TOPDIR)/tmp && chmod 644 ${package}-$(VERSION).tar.bz2
	ls -l $(TOPDIR)/tmp/

dev-docs:
	~/inprogress/HappyDoc3-r3_1/happydoc -d docs/developer/ wifi-radar.py

changelog:
	mkdir -p $(TOPDIR)/tmp/
	git log | perl -pi -e 's/</&lt;/g; s/>/&gt;/g; s/@/@<!-- com.com -->/g;' > $(TOPDIR)/tmp/CHANGELOG

clean:
	rm -f  wifi-radar.localized
	rm -fr tmp/

distclean: clean

