# Copyright (C) 2012-2014, 2016-2018, 2024  Luke T. Shumaker <lukeshu@parabola.nu>
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
# This file is part of Parabola Libretools.
#
# Libretools 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.
#
# Libretools 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, see <http://www.gnu.org/licenses/>.

include $(dir $(lastword $(MAKEFILE_LIST)))/../../config.mk
include $(topsrcdir)/build-aux/Makefile.head.mk

libretools.pkg = librelib

libretools.src.devtools  = common.sh.in archroot.sh.in
libretools.out.libs     += common.sh    archroot.sh    conf.sh

libretools.out.bins := $(filter-out indent %.gen,$(libretools.out.bins)) librexgettext
libretools.out.libexecs = indent

# Build ##############################################################

$(outdir)/conf.sh: $(var.)sysconfdir $(var.)pkgconfdir

$(outdir)/librexgettext: $(srcdir)/librexgettext.in $(srcdir)/embedlib.sh
	sed $$'/@EMBEDLIB.SH@/{r $(outdir)/embedlib.sh\nd;}' < $< | $(libretools.edit) | install -m755 /dev/stdin $@

$(outdir)/embedlib.sh: $(srcdir)/embedlib.sh.gen $(srcdir)/messages.sh $(srcdir)/common.sh
	$(abspath $^) > $@

# Translate ##########################################################

$(outdir)/blacklist.sh.pot: $(srcdir)/blacklist.sh $(srcdir)/librexgettext
	@echo "OUT $@"
	@{ \
		sed -n '/^# Usage:/,/()/{ /^#/ { =; p; } }' $< | \
			sed -E -e 's/^# (.*)/msgid "\1"\nmsgstr ""\n/' \
			       -e 's|^[0-9]*$$|#. embedded usage text\n#: $(<F):&|' && \
		cd $(<D) && ./librexgettext --simple=_l:2 $(<F) && \
	:; } | $(libretools.pofmt) > $@
$(outdir)/common.sh.pot    : LIBREXGETTEXT_FLAGS += --simple=_l:2
$(outdir)/conf.sh.pot      : LIBREXGETTEXT_FLAGS += --simple=_l:2
$(outdir)/librelib.pot     : LIBREXGETTEXT_FLAGS += --simple=_l:2
$(outdir)/messages.sh.pot  : LIBREXGETTEXT_FLAGS += --simple=_l:2
$(outdir)/librexgettext.pot: LIBREXGETTEXT_FLAGS += --simple=errusage

######################################################################

include $(topsrcdir)/build-aux/Makefile.tail.mk
