Description: Add packaging helpers for FreeDict Source packages.
 The code in debian/rules for FreeDict packages is highly repetitive. Keeping
 them up-to-date in multiple source packages is hence avoidable maintenance
 overhead.
 The make snippet is a debian/rules helper that collects common targets, rules
 and configuration for FreeDict dictionary builds. 
Author: Sebastian Humenda <shumenda@gmx.de>
Last-Update: 2026-11-16

Index: freedict-tools/mk/debian-packaging.mk
===================================================================
--- /dev/null
+++ freedict-tools/mk/debian-packaging.mk
@@ -0,0 +1,114 @@
+# Debian packaging helpers for FreeDict.
+#
+# This file is a debian/rules helper that collects common targets, rules and
+# configuration for FreeDict dictionary builds. The code in debian/rules for
+# FreeDict packages is highly repetitive. Keeping them up-to-date in multiple
+# source packages is hence avoidable maintenance overhead.
+#
+# Include this file from the package root, with FREEDICT_TOOLS set by the caller
+# before inclusion.
+
+ifneq ($(filter environment%,$(origin FREEDICT_TOOLS)),)
+$(error FREEDICT_TOOLS must be set by the including Makefile, not the environment)
+endif
+ifeq ($(strip $(FREEDICT_TOOLS)),)
+$(error FREEDICT_TOOLS must be set before including packaging.mk)
+endif
+unexport FREEDICT_TOOLS
+
+# Callers may select dictionaries before inclusion; otherwise use all language
+# pair directories. Capture the selection once for both rules and recipes.
+DICTS ?= $(patsubst %/,%,$(wildcard ???-???/))
+FREEDICT_DICTS := $(DICTS)
+
+.PHONY: override_dh_auto_build override_dh_auto_clean override_dh_auto_install \
+        fetch-orig gen-cc need-update $(FREEDICT_DICTS)
+
+# If the environment variable BUILD_MODE=dummy is set, no dictionaries are
+# built, but only dummy files are created. This is useful for test builds of the
+# package, because the actual build takes ages.
+BUILD_MODE ?= normal
+
+# Allow parallel dummy builds by adding the requested job count to MAKEFLAGS;
+# real dictionary builds require too much RAM.
+ifeq ($(BUILD_MODE),dummy)
+FREEDICT_NUMJOBS := $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
+ifneq ($(FREEDICT_NUMJOBS),)
+MAKEFLAGS += -j$(FREEDICT_NUMJOBS)
+endif
+endif
+
+override_dh_auto_build: $(FREEDICT_DICTS)
+
+$(FREEDICT_DICTS):
+ifeq ($(BUILD_MODE),normal)
+	$(MAKE) -C $@ FREEDICT_TOOLS="$(FREEDICT_TOOLS)" build-dictd build-stardict
+else
+	@# dictd
+	mkdir -p "$@/build/dictd"
+	touch "$@/build/dictd/$@.dict.dz" "$@/build/dictd/$@.index"
+	@# stardict
+	mkdir -p "$@/build/stardict"
+	touch "$@/build/stardict/$@.dict"
+	gzip -n -c < /dev/null > "$@/build/stardict/$@.idx.gz"
+	touch "$@/build/stardict/$@.ifo"
+	touch "$@/build/stardict/$@.syn"
+endif
+
+override_dh_auto_clean:
+	dh_clean
+	dh_testdir
+	rm -f build-stamp timestamp configure-stamp
+	rm -rf config.txt
+	for DICT in $(FREEDICT_DICTS); do \
+		rm -rf $$DICT/build; \
+		rm -rf debian/dict-freedict-$$DICT.dirs; \
+		rm -rf debian/dict-freedict-$$DICT.docs; \
+		rm -rf debian/dict-freedict-$$DICT.postinst; \
+		rm -rf debian/dict-freedict-$$DICT.postrm; \
+	done
+	dh_clean
+
+
+override_dh_auto_install:
+	# create debian/dict-freedict-$LANG.dirs file and maint scripts
+	for DICT in $(FREEDICT_DICTS); do \
+		sed "s/LANG/$$DICT/g" debian/dict-freedict-lang-dirs.tmpl > \
+			debian/dict-freedict-$$DICT.dirs; \
+		cp debian/postinst debian/dict-freedict-$$DICT.postinst; \
+		cp debian/postrm debian/dict-freedict-$$DICT.postrm; \
+	done
+
+	dh_installdirs
+
+	# copy files to final destination
+	set -e; for DICT in $(FREEDICT_DICTS); do \
+		cp $$DICT/build/dictd/$$DICT.dict.dz debian/dict-freedict-$$DICT/usr/share/dictd/freedict-$$DICT.dict.dz; \
+		cp $$DICT/build/dictd/$$DICT.index debian/dict-freedict-$$DICT/usr/share/dictd/freedict-$$DICT.index; \
+		install -D -m 644 debian/dict-freedict.lintian-overrides \
+			debian/dict-freedict-$$DICT/usr/share/lintian/overrides/dict-freedict-$$DICT; \
+		STARDICT_DIR=debian/stardict-freedict-$$DICT/usr/share/stardict/dic/freedict-$$DICT; \
+		install -d $$STARDICT_DIR; \
+		install -m 644 $$DICT/build/stardict/$$DICT.dict \
+			$$DICT/build/stardict/$$DICT.idx.gz \
+			$$DICT/build/stardict/$$DICT.ifo $$STARDICT_DIR/; \
+		if [ -f $$DICT/build/stardict/$$DICT.syn ]; then \
+			install -m 644 $$DICT/build/stardict/$$DICT.syn $$STARDICT_DIR/; \
+		fi; \
+	done
+
+	# generate *.docs-files:
+	sh debian/gendocs.sh
+	dh_install
+
+# this rule is explicitly here to document the source retrieval workflow
+# requires python3
+fetch-orig:
+	python3 $(FREEDICT_TOOLS)/fetchdictdata.py --orig --criteria 'sourceURL!wikdict'
+
+# generate debian/copyright and debian/control
+gen-cc:
+	python3 $(FREEDICT_TOOLS)/fetchdictdata.py --dc --criteria 'sourceURL!wikdict' --no-desc-version
+
+need-update:
+	python3 $(FREEDICT_TOOLS)/fetchdictdata.py --need-update --criteria 'sourceURL!wikdict'
