## deb/Makefile
##
##    AudioScience HPI driver
##    Copyright (C) 1997-2017  AudioScience Inc. <support@audioscience.com>
##
##    This program is free software; you can redistribute it and/or modify
##    it under the terms of version 2 of the GNU General Public License as
##    published by the Free Software Foundation;
##
##    This program is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with this program; if not, write to the Free Software
##    Foundation, Inc., 59 Temple Place, Suite 330,
##    Boston, MA  02111-1307  USA
##

.DEFAULT_GOAL := setup

abs-thisdir := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))
dist-dir-prefix = ../

include hpi_version.mk

distro-series = buster

distro-archs = amd64 i386

series-from-path = $(notdir $(patsubst %/,%,$(dir $(1))))
debpkg-changelog-files = $(addsuffix -changelog.in,$(debpkg-dir-names))

deb-build-path-prefix = $(abs-thisdir)/build/$(hpi-repo-version-string)
dest-debpkg-dirs = $(foreach series,$(distro-series), $(addprefix $(deb-build-path-prefix)/$(series)/,$(debpkg-dir-names)))

pbuilder-base-image-files = $(addprefix $(HOME)/pbuilder/, $(foreach serie,$(distro-series), $(foreach arch,$(distro-archs), $(addprefix $(serie)-$(arch), -base.tgz))))

srcdeb-prefixes = asihpi-dkms snd-asihpi-dkms asihpi-meta hpklinux python-audioscience asihpi-utils
run-all-pbuilder-dist = $(foreach serie,$(distro-series), $(foreach arch,$(distro-archs), $(foreach desc_file_prefix,$(srcdeb-prefixes), ./pbuilder-dist $(serie) $(arch) build $(wildcard $(deb-build-path-prefix)/$(serie)/$(desc_file_prefix)*.dsc); )))

get-base-image-distro = $(shell echo $(notdir $@) | cut -d '-' --output-delimiter=' ' -f1)
get-base-image-arch = $(shell echo $(notdir $@) | cut -d '-' --output-delimiter=' ' -f2)

include deb.mk

%/pkgsrc-build.stamp: %
	cd $(dir $@) && debuild -i -us -uc -S -d && touch $@

setup: $(debpkg-changelog-files) $(HOME)/.pbuilderrc $(HOME)/.pbuilder-hooks/D10apt-ftparchive

pkgsrc-build: setup $(addsuffix /pkgsrc-build.stamp,$(dest-debpkg-dirs))

$(HOME)/.pbuilderrc: pbuilderrc
	cp -a pbuilderrc $(HOME)/.pbuilderrc

$(HOME)/.pbuilder-hooks/D10apt-ftparchive: pbuilder-hook
	mkdir -p $(HOME)/.pbuilder-hooks
	cp -a pbuilder-hook $(HOME)/.pbuilder-hooks/D10apt-ftparchive

./pbuilder-dist:
	cp -a $(shell which pbuilder-dist) ./
	patch <pbuilder-dist.patch

$(pbuilder-base-image-files): setup ./pbuilder-dist
	./pbuilder-dist $(shell echo $(notdir $@) | cut -d '-' --output-delimiter=' ' -f1,2) create

pkgbin-build: pkgsrc-build $(pbuilder-base-image-files)
	$(call run-all-pbuilder-dist)

clean:
	rm -rf $(dest-debpkg-dirs)

distclean:
	rm -rf $(abs-thisdir)/build

.PHONY: setup pkgsrc-build pkgbin-build clean distclean
