APPNAME=catfish
PREFIX=/usr/local
PYTHON=`which python`
LANGUAGE_FILES=$(patsubst po/%.po, locale/%/LC_MESSAGES/$(APPNAME).mo, $(wildcard po/*.po))
DESTDIR=

all: $(LANGUAGE_FILES)
	$(PYTHON) ./build.py build $(APPNAME)
	sed -e s,%prefix%,$(PREFIX), $(APPNAME).in > $(APPNAME)
	chmod +x $(APPNAME)

locale/%/LC_MESSAGES/$(APPNAME).mo: po/%.po
	mkdir -p $(dir $@)
	msgfmt $< -o $@

install: all
	install -d $(DESTDIR)/$(PREFIX)/bin
	install $(APPNAME) $(DESTDIR)/$(PREFIX)/bin

	install -d $(DESTDIR)/$(PREFIX)/share/$(APPNAME)
	install $(APPNAME).pyc $(DESTDIR)/$(PREFIX)/share/$(APPNAME)
	install $(APPNAME).glade $(DESTDIR)/$(PREFIX)/share/$(APPNAME)

	install -d $(DESTDIR)/$(PREFIX)/share/icons/hicolor/scalable/apps
	install $(APPNAME).svg $(DESTDIR)/$(PREFIX)/share/icons/hicolor/scalable/apps
	ln -sf $(DESTDIR)/$(PREFIX)/share/icons/hicolor/scalable/apps/$(APPNAME).svg $(DESTDIR)/$(PREFIX)/share/$(APPNAME)/$(APPNAME).svg

	install -d $(DESTDIR)/$(PREFIX)/share/doc/$(APPNAME)
	install AUTHORS $(DESTDIR)/$(PREFIX)/share/doc/$(APPNAME)
	install COPYING $(DESTDIR)/$(PREFIX)/share/doc/$(APPNAME)
	install ChangeLog $(DESTDIR)/$(PREFIX)/share/doc/$(APPNAME)
	install INSTALL $(DESTDIR)/$(PREFIX)/share/doc/$(APPNAME)
	install README $(DESTDIR)/$(PREFIX)/share/doc/$(APPNAME)

	install -d $(DESTDIR)/$(PREFIX)/share/applications
	install --mode=644 $(APPNAME).desktop $(DESTDIR)/$(PREFIX)/share/applications

	cp -rf locale $(DESTDIR)/$(PREFIX)/share
	ln -sf $(DESTDIR)/$(PREFIX)/share/locale $(DESTDIR)/$(PREFIX)/share/$(APPNAME)/locale

uninstall:
	rm -f $(DESTDIR)/$(PREFIX)/share/applications/$(APPNAME).desktop
	rm -rf $(DESTDIR)/$(PREFIX)/share/$(APPNAME)
	rm -rf $(DESTDIR)/$(PREFIX)/share/icons/hicolor/scalable/apps/$(APPNAME).svg
	rm -rf $(DESTDIR)/$(PREFIX)/share/doc/$(APPNAME)
	# FIXME: Uninstall locales
	rm -f $(DESTDIR)/$(PREFIX)/bin/$(APPNAME)

clean:
	rm -Rf locale
	rm -f *.pyc
	rm -f $(APPNAME)
	rm -f Makefile
	rm -Rf deb

deb: all
	$(MAKE) install DESTDIR=$(CURDIR)/deb
	fakeroot dpkg -b deb $(APPNAME).deb
	$(MAKE) uninstall DESTDIR=$(CURDIR)/deb
