#!/usr/bin/make -f
# -*- makefile -*-

# Use bash and not the default sh
SHELL := /bin/bash

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

.PHONY: update

source := $(shell dpkg-parsechangelog -S Source)
pyfiles := $(shell find ./calamares-modules -name '*.py' -print 2>/dev/null)
tsfiles := $(shell find ./ts -name "*.ts" | grep -v "en_US" 2>/dev/null)
#desktopfls := $(shell find . -wholename "./debian" -prune -o -wholename "./conf" -prune -o -name "*.desktop" -print 2>/dev/null)
desktopfls := $(shell find ./conf -maxdepth 1 -name '*.desktop' -print 2>/dev/null)

# Create qm from the ts files
# https://www.gnu.org/software/make/manual/make.html#Pattern-Rules
%.qm : %.ts
	lrelease $< -qm ./calamares/branding/solydxk/lang/$(basename $(notdir $<)).qm

%.mo : %.po
	mkdir -p ./po/mo/$(basename $(notdir $<))/LC_MESSAGES/
	msgfmt $< -o ./po/mo/$(basename $(notdir $<))/LC_MESSAGES/$(source).mo

%:
	dh $@

override_dh_installsystemd:
	dh_installsystemd --name=packages-non-free

override_dh_auto_clean:
	# Cleanup first
	find ./calamares/branding/solydxk/lang -type f -name "*.qm" -delete
	rm -rf ./po/mo
	mkdir ./po/mo
	# Create .pot file if it does not exist
	touch ./po/$(source).pot

override_dh_auto_install: update $(patsubst %.ts,%.qm,$(tsfiles)) $(patsubst %.po,%.mo,$(wildcard ./po/*.po))

override_dh_builddeb:
	dh_builddeb
	# Cleanup build directory when done
	rm -rf ./debian/$(source)

update:
	# Get translations from Transifex
	tx pull -a

	# Create the lang directory if it does not exist
	mkdir -p ./calamares/branding/solydxk/lang
	
	# Scan for .py files
	@ xgettext \
		--keyword=_ \
		--language=Python \
		--output=./po/$(source).pot \
		--package-name=$(source) \
		--package-version='' \
		--from-code=UTF-8 \
		$(pyfiles) && echo "Python files scanned for translations"

	# Update base ts file
	@ cd ./calamares/branding/solydxk; lupdate show.qml -no-obsolete -source-language en_US -ts ../../../ts/calamares-solydxk_en_US.ts -target-language en_US

	# Get strings from the desktop file
	@ for LAUNCHER in $(desktopfls); do \
		sed -r \
			-e '/^(Name|Comment|GenericName|Keywords)\[/d' \
			-e 's/^(Name=|Comment=|GenericName=|Keywords=)/_\1/' \
			$$LAUNCHER > $$LAUNCHER.in ; \
		intltool-extract --type=gettext/ini $$LAUNCHER.in ; \
		xgettext \
			--join-existing \
			--keyword=N_:1 \
			--output ./po/$(source).pot \
			$$LAUNCHER.in.h ; \
	done && echo "Desktop files scanned for translations"

	# Fix charset
	@ find ./po -type f -name "*.po*" -exec sed -i 's/charset=CHARSET/charset=UTF-8/' {} \;

	# Apply desktop file modifications
	@ for LAUNCHER in $(desktopfls); do \
		intltool-merge --quiet --desktop-style ./po $$LAUNCHER.in $$LAUNCHER ; \
		rm $$LAUNCHER.in.h $$LAUNCHER.in ; \
	done && echo "Desktop files updated with new translations"
	
	# Push ts file to Transifex
	#tx push -s

