# Build all Debian packaging examples
# Copyright (C) 2021 Osamu Aoki

# Normalize build environment (Debian default is LANG=C etc.)
LC_ALL := C.UTF-8
export LC_ALL
TERM = dumb
export TERM
ARCH := $(shell dpkg-architecture -q DEB_TARGET_ARCH)

ALL_DIRS := $(shell find . -maxdepth 1 -type d -regex '\./debhello-.*' | sed  -e 's,^\./,,')
BUILD_DIRS := $(shell find . -maxdepth 1 -type d -regex '\./debhello-.*_build.*' | sed  -e 's,^\./,,')
UPSTREAM_DIRS := $(filter-out $(BUILD_DIRS),$(ALL_DIRS))
TARGZS := $(addsuffix .tar.gz, $(UPSTREAM_DIRS))
ORIGTARGZS := $(patsubst debhello-%,debhello_%,$(addsuffix .orig.tar.gz, $(UPSTREAM_DIRS)))
BUILDMAKEFILES := $(addsuffix /Makefile, $(BUILD_DIRS))
BUILDSTAMPS := $(addsuffix -stamp, $(BUILD_DIRS))

all: prep
	# === BUILDSTAMPS ===
	$(MAKE) $(BUILDSTAMPS)
	# === SUCCESS ALL ===
prep:
	# === ALL_DIRS ===
	@ echo "$(ALL_DIRS)"| xargs -n1
	# === BUILD_DIRS ===
	@ echo "$(BUILD_DIRS)"| xargs -n1
	# === UPSTREAM_DIRS ===
	@ echo "$(UPSTREAM_DIRS)" | xargs -n1
	# === TARGZS ===
	@ echo "$(TARGZS)" | xargs -n1
	$(MAKE) $(TARGZS)
	# === ORIGTARGZS ===
	@ echo "$(ORIGTARGZS)" | xargs -n1
	$(MAKE) $(ORIGTARGZS)
	# === BUILDMAKEFILES ===
	$(MAKE) $(BUILDMAKEFILES)
	# === SUCCESS PREP ===
clean:
	-rm -f $(TARGZS)
	-rm -f $(ORIGTARGZS)
	-rm -f $(BUILDMAKEFILES)
	-rm -f $(BUILDSTAMPS)
	# way to clean build results and Makefile link
	-rm -f debhello-*_build*/*.log
	-rm -f debhello-*_build*/Makefile
	-rm -f debhello-*_build*/next.dir
	-rm -f debhello-*_build*/project
	-rm -rf debhello-*_build*/test
	# post-debmake snapshot of test
	-rm -rf debhello-*_build*/debmake
	# post-package snapshot of test
	-rm -rf debhello-*_build*/package

distclean: clean
	-rm -f debhello-*_build*/*.slog

debhello-%.tar.gz: debhello-%/LICENSE
	tar --exclude=debhello-$*/debian -cvzf $@ debhello-$*/

debhello_%.orig.tar.gz: debhello-%.tar.gz
	ln -sf $^ $@

debhello-%/Makefile: template/Makefile
	ln -sf ../$^ $@

debhello-%-stamp: debhello-%/Makefile
	$(MAKE) -C debhello-$*

#######################################################################
# Groom debhello-*/debian/* template files
#######################################################################
VERSIONS := $(patsubst debhello-%,%,$(UPSTREAM_DIRS))
CHANGELOGS := $(addsuffix /debian/changelog, $(addprefix debhello-,$(VERSIONS)))
COPYRIGHTS := $(addsuffix /debian/copyright, $(addprefix debhello-,$(VERSIONS)))
COPYRIGHTS := $(addsuffix /debian/control, $(addprefix debhello-,$(VERSIONS)))
READMES := $(addsuffix /debian/README.Debian, $(addprefix debhello-,$(VERSIONS)))
WATCHS := $(addsuffix /debian/watch, $(addprefix debhello-,$(VERSIONS)))
SRCF := $(addsuffix /debian/source/format, $(addprefix debhello-,$(VERSIONS)))
WRAPS := $(addsuffix -wrap,$(addprefix debhello-,$(VERSIONS)))

vers:
	# === VERSIONS ===
	@ echo "$(VERSIONS)"| xargs -n1

groom:
	cd template ; touch * source/*
	$(MAKE) $(CHANGELOGS) $(COPYRIGHTS) $(READMES) $(WATCHS) $(SRCF) $(CONTROLS)
	# clean whitespaces in line tail (exclude some directories)
	-find . -type d \
		-regex ".*/\.git" -prune -o \
		-regex ".*/post-gettextize" -prune -o \
		-regex ".*/png" -prune -o \
	       -type f -exec sed -i -e 's/[ \t]*$$//' '{}' \;
	$(MAKE) $(WRAPS)

debhello-%/debian/changelog: template/changelog
	mkdir -p debhello-$*/debian
	sed -ne '/debhello ($*-1/,$$p' <$^ >$@

debhello-%/debian/copyright: template/copyright
	mkdir -p debhello-$*/debian
	cp -f $^ $@

# Standards-Version: 4.5.1
# Homepage: <insert the upstream URL, if relevant>
# Homepage: https://salsa.debian.org/debian/debmake-doc
# Vcs-Git: https://salsa.debian.org/debian/debmake-doc.git
# Vcs-Browser: https://salsa.debian.org/debian/debmake-doc
HOMEPAGE = https://salsa.debian.org/debian/debmake-doc
STDVER = 4.5.1
DHCOMPAT = 13
MAINTNM = Osamu Aoki <osamu@debian.org>

debhello-%/debian/control: template/README.Debian
	mkdir -p debhello-$*/debian
	sed -i  \
		-e 's/^Section: unknown$$/Section: devel/' \
		-e 's/^Maintainer:.*$$/Maintainer: $(MAINTNM)/' \
		-e 's/debhelper-compat *( *= *[1-9][0-9]* *)/debhelper-compat (= $(DHCOMPAT))/' \
		-e 's/^Standards-Version:.*$$/Standards-Version: $(STDVER)/' \
		-e 's,^Homepage:.*$$,Homepage: $(HOMEPAGE),' \
		-e 's,^Description:.*$$,Description: Simple packaging example for debmake,' \
		-e 's,was auto-generated.*$$,is an example package.,' \
		-e 's,^ debmake(1) command provided by .*$$, (This is an example only),' \
		-e 's,^X-Python3-Version.*$$,,' \
		$@

debhello-%/debian/README.Debian: template/README.Debian
	mkdir -p debhello-$*/debian
	cp -f $^ $@

debhello-%/debian/watch: template/watch
	mkdir -p debhello-$*/debian
	cp -f $^ $@

debhello-%/debian/source/format: template/source/format
	mkdir -p debhello-$*/debian/source
	cp -f $^ $@

debhello-%-wrap:
	cd debhello-$* ; \
	wrap-and-sort

