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

# Standards-Version: 4.6.2
# 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

# Set the following before "make groom"
HOMEPAGE = https://salsa.debian.org/debian/debmake-doc
STDVER = 4.6.2
DHCOMPAT = 13
MAINTNM = Osamu Aoki <osamu@debian.org>

# vim: set noet ts=8:
# 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_DEPS := \
            autoconf\
            automake\
            autopoint\
            build-essential \
            cmake\
            debhelper\
            debmake\
            devscripts\
            dh-sequence-single-binary \
            gettext\
            gir1.2-gtk-3.0\
            libltdl-dev\
            libtool\
            libxml2-utils\
            lintian\
            locales-all\
            pybuild-plugin-pyproject \
            python3-all\
            python3-debian\
            python3-gi\
            quilt \
            tree \
            vim\
            w3m\
            zip

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))
ASCIIDOC_REF := $(addsuffix /ASCIIDOC_REF, $(BUILD_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:
	# === BUILDSTAMPS ===
	$(MAKE) clean
	$(MAKE) deps
	$(MAKE) prep
	$(MAKE) asciidoc
	$(MAKE) buildall
	# === SUCCESS ALL ===

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 -f debhello-*_build*/build
	-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
	-rm -rf debhello-*_build*/dsc

distclean: clean
	-rm -f $(ASCIIDOC_REF)
	-rm -f debhello-*_build*/*.slog

deps:
	# === ALL_DEPS ===
	sudo apt-get update
	sudo apt-get install $(ALL_DEPS)

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)

asciidoc:
	# === ASCIIDOC_REF ===
	$(MAKE) $(ASCIIDOC_REF)

buildall:
	# === BUILDSTAMPS ===
	$(MAKE) $(BUILDSTAMPS)

debhello-%.tar.gz: debhello-%/README.md
	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
#######################################################################
BUILD_VERS := $(patsubst debhello-%,%,$(BUILD_DIRS))
UPSTREAM_VERS := $(patsubst debhello-%,%,$(UPSTREAM_DIRS))
CHANGELOGS := $(addsuffix /debian/changelog, $(addprefix debhello-,$(UPSTREAM_VERS)))
COPYRIGHTS := $(addsuffix /debian/copyright, $(addprefix debhello-,$(UPSTREAM_VERS)))
COPYRIGHTS := $(addsuffix /debian/control, $(addprefix debhello-,$(UPSTREAM_VERS)))
READMES := $(addsuffix /debian/README.Debian, $(addprefix debhello-,$(UPSTREAM_VERS)))
WATCHS := $(addsuffix /debian/watch, $(addprefix debhello-,$(UPSTREAM_VERS)))
SRCF := $(addsuffix /debian/source/format, $(addprefix debhello-,$(UPSTREAM_VERS)))
WRAPS := $(addsuffix -wrap,$(addprefix debhello-,$(UPSTREAM_VERS)))

builds:
	# === BUILD UPSTREAM_VERS ===
	@ echo "$(BUILD_VERS)"| xargs -n1

vers:
	# === UPSTREAM_VERS ===
	@ echo "$(UPSTREAM_VERS)"| 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 $^ $@

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

debhello-%/ASCIIDOC_REF:
	{ cd ../asciidoc ; rg --vimgrep "debhello-$*" *.txt || echo "---" ; } > $@

symlinks:
	for d in $(filter-out debhello-0.0_build-1, $(BUILD_DIRS)); do $(MAKE) -C $$d symlink ; cd - ; done
