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

# Copyright (C) 2010-2023 Pädagogisches Landesinstitut Rheinland-Pfalz
# Copyright (C) 2022-2023 Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

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

include /usr/share/dpkg/pkg-info.mk

VER  = $(shell echo $(DEB_VERSION) | cut -d "~" -f1)
SHELL := /bin/bash

%:
	dh $@

override_dh_install:

	# run debhelper install first
	dh_install

	###
	### locale files
	###

	# first, some locale related clean-ups...
	rm debian/*/usr/share/locale/LINGUAS
	rm debian/*/usr/share/locale/*.pot
	rm debian/*/usr/share/locale/*.in
	rm debian/debian-edu-router-config/usr/share/locale/*/LC_MESSAGES/debian-edu-router-fai.po
	rm debian/debian-edu-router-fai/usr/share/locale/*/LC_MESSAGES/debian-edu-router-config.po


	# then, create .mo files (and drop .po files)
	find debian/*/usr/share/locale/ -name "*.po" | while read pofile; do \
	    sed -i $${pofile} -Ee "s/(^\"Project-Id-Version: )(PACKAGE VERSION)(.*\")/\\1$(VER)\\3/"; \
	    msgfmt --check -o $${pofile/.po/.mo} $${pofile}; \
	done
	find debian/*/usr/share/locale/ -name "*.po" -delete

	# and, rename shell files and drop .sh suffix
	find debian/*/usr/sbin/ -name "*.sh" | while read shfile; do \
	    mv $${shfile} $${shfile/.sh/}; \
	done

override_dh_clean:
	# as developers, we might build from a DEB src:pkg containing i18n translation cruft...
	rm -f po/de/LC_MESSAGES/debian-edu-router-config.mo
	rm -f po/de/LC_MESSAGES/debian-edu-router-fai.mo
	rm -f debian/po/*.mo
	dh_clean
