SHELL = /bin/sh
PREFIX = /usr/local
#DESTDIR = /tmp/mpd-sima
LIBDIR = $(DESTDIR)$(PREFIX)/lib
BIN = $(DESTDIR)$(PREFIX)/bin
ETC = $(DESTDIR)$(PREFIX)/etc
DATADIR = $(DESTDIR)$(PREFIX)/share
LOCALEDIR = $(DATADIR)/locale
MANDIR = $(DATADIR)/man

all: man bin
clean:
	rm -f mpd-sima.1.gz simadb_cli.1.gz mpd-sima.cfg.5.gz mpd-sima simadb_cli

mpd-sima.1.gz: data/mpd-sima.1
	cat data/mpd-sima.1 | gzip > mpd-sima.1.gz

mpd-sima.cfg.5.gz: data/mpd-sima.cfg.5
	cat data/mpd-sima.cfg.5 | gzip > mpd-sima.cfg.5.gz

simadb_cli.1.gz:
	cat data/simadb_cli.1 | gzip > simadb_cli.1.gz

simadb_cli:
	sed 's:%DESTDIR%%PREFIX%:${DESTDIR}${PREFIX}:' data/wrappers/simadb_cli > simadb_cli

mpd-sima:
	sed 's:%DESTDIR%%PREFIX%:${DESTDIR}${PREFIX}:' data/wrappers/mpd-sima > mpd-sima

bash_completion:
	install -d $(ETC)/bash_completion.d
	install -m644 data/mpd_sima.completion.sh $(ETC)/bash_completion.d/
	mv $(ETC)/bash_completion.d/mpd_sima.completion.sh $(ETC)/bash_completion.d/mpd-sima

man: mpd-sima.1.gz simadb_cli.1.gz mpd-sima.cfg.5.gz

bin: mpd-sima simadb_cli

install: man bin
	install -d $(BIN) $(DATADIR)/mpd-sima $(MANDIR)/man1 $(MANDIR)/man5

	install -m755 mpd-sima $(BIN)
	install -m755 simadb_cli $(BIN)

	install -m644 mpd-sima.1.gz simadb_cli.1.gz $(MANDIR)/man1
	install -m644 mpd-sima.cfg.5.gz $(MANDIR)/man5

	# listing all sub folder to install (prevent a remaining .svn directory to be install)
	for sourcedir in `find src/ -name .svn -prune -o -type d -print | sed 's:src/::g'` ; do \
		install -d $(DATADIR)/mpd-sima/$$sourcedir; \
		for sourcefile in `find src/$$sourcedir -maxdepth 1 -name *pyc -o -name .svn -prune -o -print` ; do \
			install -m644 $$sourcefile $(DATADIR)/mpd-sima/$$sourcedir; \
		done \
	done

	install -m755 src/mpd_sima $(DATADIR)/mpd-sima
	install -m755 src/simadb_cli $(DATADIR)/mpd-sima

uninstall:
	rm -f $(BIN)/mpd-sima
	rm -f $(BIN)/simadb_cli
	rm -f $(MANDIR)/man1/mpd-sima.1.gz
	rm -f $(MANDIR)/man1/simadb_cli.1.gz
	rm -f $(MANDIR)/man5/mpd-sima.cfg.5.gz
	rm -f $(ETC)/bash_completion.d/mpd-sima
	rm -rf $(DATADIR)/mpd-sima

