#!/usr/bin/make -f

# The upstream version lives in two places that must agree: AC_INIT in
# configure.ac, which becomes PACKAGE_VERSION and so the User-Agent the
# binary reports, and debian/changelog, which is what the .deb claims to
# be.  Bump only the changelog and the package installs a binary that
# tells repository operators it is the older version -- silently, since
# nothing else notices.  Compared here so that drift is a build failure
# rather than a puzzle in somebody's access log.
# The regex deliberately avoids a literal parenthesis: make counts parens
# to find the end of $(shell ...), so an unbalanced one in the script
# breaks the parse of this file.  [^,]* skips over AC_INIT's first
# argument instead.
# The comparison below is a prefix check rather than equality because
# the build server rewrites the changelog version, appending its own
# stamp and revision -- 0.2.0-1 arrives as something like
# 0.2.0-1+260824100003-3db1, and stripping a single revision suffix no
# longer recovers the upstream version.  Requiring the changelog version
# to be the upstream version followed by a hyphen accepts all of those
# while still failing 0.2.10-1 against 0.2.1.
UPSTREAM_VERSION := $(shell sed -n 's/^AC_INIT[^,]*, *\[\([^]]*\)\].*/\1/p' configure.ac)
CHANGELOG_VERSION := $(shell dpkg-parsechangelog -SVersion | sed -e 's/^[0-9]*://')

# The library major from -version-number is the soname, and so the name of
# the binary package that ships it.  debian/libaept0.install installs
# usr/lib/*/libaept.so.* -- a glob -- so bumping the major without renaming
# the package produces a package called libaept0 containing libaept.so.1,
# and the build succeeds.  dpkg-shlibdeps then tells dependents they need
# libaept1, which nothing provides, and the breakage lands at install time
# on somebody else's machine.
SONAME_MAJOR := $(shell sed -n 's/^libaept_la_LDFLAGS *= *-version-number *\([0-9][0-9]*\).*/\1/p' src/Makefile.am)

%:
	dh $@ --with python3

override_dh_auto_configure:
	@test -n "$(UPSTREAM_VERSION)" \
	    || { echo "debian/rules: could not read AC_INIT from configure.ac" >&2; exit 1; }
	@case "$(CHANGELOG_VERSION)" in \
	    "$(UPSTREAM_VERSION)"|"$(UPSTREAM_VERSION)-"*) ;; \
	    *) echo "debian/rules: version mismatch -- configure.ac says $(UPSTREAM_VERSION), which is not the base of debian/changelog's $(CHANGELOG_VERSION)" >&2; exit 1 ;; \
	esac
	@test -n "$(SONAME_MAJOR)" \
	    || { echo "debian/rules: could not read -version-number from src/Makefile.am" >&2; exit 1; }
	@grep -q "^Package: libaept$(SONAME_MAJOR)\$$" debian/control \
	    || { echo "debian/rules: the library is libaept.so.$(SONAME_MAJOR), but debian/control declares no 'Package: libaept$(SONAME_MAJOR)' -- bumping the major renames the binary package, and debian/libaept*.install, debian/control and python/aeltra/aept/_ffi.py all need to follow" >&2; exit 1; }
	dh_auto_configure

override_dh_auto_build:
	dh_auto_build
	scdoc < aept.1.scd > debian/aept.1
	python3 -m build --wheel --no-isolation python/

override_dh_auto_install:
	dh_auto_install
	python3 -m installer --destdir=debian/python3-aept python/dist/aept-*.whl

override_dh_auto_clean:
	rm -f debian/aept.1
	rm -rf python/dist/ python/build/ python/aept.egg-info/
	dh_auto_clean
