DESTDIR =

UISOURCES = $(shell ls ui_*.ui)
RCSOURCES = $(shell ls *.qrc)
UIFILES   = $(patsubst ui_%.ui, ui_%.py, $(UISOURCES))
RCFILES   =$(patsubst %.qrc, %_rc.py, $(RCSOURCES))

LANGSOURCES = $(shell ls lang/*.ts)
LANGFILES = $(patsubst %.ts, %.qm, $(LANGSOURCES))

all: $(RCFILES) $(UIFILES) $(LANGFILES)

$(UIFILES): $(UISOURCES)
$(RCFILES): $(RCSOURCES)
$(LANGFILES): $(LANGSOURCES)

ui_%.py: ui_%.ui
	pyuic5 $< -o $@

%_rc.py: %.qrc
	# lauch pyrcc5 to make reproducible builds
	export QT_HASH_SEED=0; pyrcc5 $< -o $@

%.qm: %.ts live_clone.pro
	lrelease -qt=5 live_clone.pro

clean:
	rm -rf *~ $(RCFILES) $(UIFILES) $(LANGFILES)

install:
	install -d $(DESTDIR)/usr/bin
	install -d $(DESTDIR)/usr/lib/python3/dist-packages/live_clone
	install -d $(DESTDIR)/usr/lib/python3/dist-packages/live_clone/lang
	install -d $(DESTDIR)/usr/lib/python3/dist-packages/live_clone/img
	install -d $(DESTDIR)/usr/share/applications
	install -d $(DESTDIR)/usr/share/icons
	install -d $(DESTDIR)/usr/share/polkit-1/actions

	install -m 755 live-clone $(DESTDIR)/usr/bin
	install -m 644 *.py *.md $(DESTDIR)/usr/lib/python3/dist-packages/live_clone
	install -m 644 lang/*.md $(DESTDIR)/usr/lib/python3/dist-packages/live_clone/lang
	install -m 644 lang/*.qm $(DESTDIR)/usr/lib/python3/dist-packages/live_clone/lang
	install -m 644 img/* $(DESTDIR)/usr/lib/python3/dist-packages/live_clone/img
	install -m 755 live_clone.desktop $(DESTDIR)/usr/share/applications
	install -m 644 img/live_clone.png $(DESTDIR)/usr/share/icons
	# replace the relative paths ./ by absolute paths
	for f in $$(find $(DESTDIR) -name "*.md"); do \
	  sed -i 's%\./%/usr/lib/python3/dist-packages/live_clone/%' $$f; \
	done
	install -m 644 *.policy $(DESTDIR)/usr/share/polkit-1/actions

$(LANGSOURCES): *.py $(UIFILES) live_clone.pro
	pylupdate5 live_clone.pro


.PHONY: all clean install lang
