# Debian packaging wrapper: fetch upstream android_translation_layer/libopensles-standalone
# and build it with meson. The source is downloaded at build time rather than
# vendored here; Debian build fixes live in debian/patches/ and are applied to
# the fetched tree (and proposed upstream).

UPSTREAM_URL  ?= https://gitlab.com/android_translation_layer/libopensles-standalone.git
UPSTREAM_REF  ?= 605a83f47263a022427afb6e95801bd39b459b78
SRC           ?= upstream

PREFIX        ?= /usr
LIBDIR        ?= lib
DESTDIR       ?=
MESON_ARGS    ?=
BUILDDIR      := output

PATCHES := $(sort $(wildcard debian/patches/*.patch))

.PHONY: all download patch build install clean

all: build

$(SRC)/.downloaded:
	rm -rf $(SRC)
	git init -q $(SRC)
	cd $(SRC) && git remote add origin $(UPSTREAM_URL) \
		&& git fetch -q --depth 1 origin $(UPSTREAM_REF) \
		&& git checkout -q --detach FETCH_HEAD
	touch $@

download: $(SRC)/.downloaded

$(SRC)/.patched: $(SRC)/.downloaded $(PATCHES)
	@for p in $(PATCHES); do echo "applying $$p"; patch -d $(SRC) -p1 < "$(CURDIR)/$$p"; done
	touch $@

patch: $(SRC)/.patched

$(BUILDDIR)/build.ninja: $(SRC)/.patched
	meson setup $(BUILDDIR) $(SRC) --prefix=$(PREFIX) --libdir=$(LIBDIR) --buildtype=plain $(MESON_ARGS)

build: $(BUILDDIR)/build.ninja
	meson compile -C $(BUILDDIR)

install: build
	DESTDIR=$(DESTDIR) meson install --no-rebuild -C $(BUILDDIR)

clean:
	rm -rf $(SRC) $(BUILDDIR)

distclean: clean
	dh_clean
