#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2014 Jonas Smedegaard <dr@jones.dk>
# Description: Main Debian packaging script for Debian Design Blends
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# 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, see <http://www.gnu.org/licenses/>.

include /usr/share/cdbs/1/rules/buildcore.mk
include /usr/share/cdbs/1/rules/debhelper.mk

CDBS_BUILD_DEPENDS +=, boxer, boxer-data

_pkg-bulk = $(shell perl -0777 -n \
	-e 's/ \\\n / /' \
	-e ' and m,^d-i pkgsel/include string ([^\n]*),ms' \
	-e ' and print $$1' \
	content/$1/preseed.cfg)
pkg-desktop-bulk = $(call _pkg-bulk,desktop)
pkg-desktop-graphics-bulk = $(call _pkg-bulk,desktop/graphics)
pkg-desktop-animation-bulk = $(call _pkg-bulk,desktop/animation)
pkg-desktop-web-bulk = $(call _pkg-bulk,desktop/web)
pkg-desktop-desktop = $(shell perl -n \
	-e 's,^tasksel tasksel/desktop multiselect ,, and print $$_' \
	content/desktop/preseed.cfg)
pkg-desktop-tasks = $(shell perl -n \
	-e 's,^tasksel tasksel/first multiselect ,, and print $$_' \
	content/desktop/preseed.cfg)
pkg-desktop = $(strip \
	$(pkg-desktop-desktop:%=task-%-desktop$(comma)) \
	$(pkg-desktop-tasks:%=task-%$(comma)) \
	$(patsubst %,%$(comma),$(filter-out %-,$(pkg-desktop-bulk))))
pkg-desktop-graphics = $(strip \
	$(filter-out $(pkg-desktop),\
		$(patsubst %,%$(comma),\
			$(filter-out %-,$(pkg-desktop-graphics-bulk)))))
pkg-desktop-animation = $(strip \
	$(filter-out $(pkg-desktop),\
		$(patsubst %,%$(comma),\
			$(filter-out %-,$(pkg-desktop-animation-bulk)))))
pkg-desktop-web = $(strip \
	$(filter-out $(pkg-desktop),\
		$(patsubst %,%$(comma),\
			$(filter-out %-,$(pkg-desktop-web-bulk)))))
pkg-desktop-avoid = $(strip \
	$(patsubst %-,%$(comma),$(filter %-,$(pkg-desktop-bulk))))
pkg-desktop-graphics-avoid = $(strip \
	$(patsubst %-,%$(comma),\
		$(filter-out $(pkg-desktop-bulk),\
			$(filter %-,$(pkg-desktop-graphics-bulk)))))
pkg-desktop-animation-avoid = $(strip \
	$(patsubst %-,%$(comma),\
		$(filter-out $(pkg-desktop-bulk),\
			$(filter %-,$(pkg-desktop-animation-bulk)))))
pkg-desktop-web-avoid = $(strip \
	$(patsubst %-,%$(comma),\
		$(filter-out $(pkg-desktop-bulk),\
			$(filter %-,$(pkg-desktop-web-bulk)))))

CDBS_DEPENDS_design-desktop = $(pkg-desktop)
CDBS_DEPENDS_design-desktop-graphics = $(pkg-desktop-graphics)
CDBS_DEPENDS_design-desktop-animation = $(pkg-desktop-animation)
CDBS_DEPENDS_design-desktop-web = $(pkg-desktop-web)
CDBS_CONFLICTS_design-desktop-strict = $(pkg-desktop-avoid)
CDBS_CONFLICTS_design-desktop-graphics-strict = $(pkg-desktop-graphics-avoid)
CDBS_CONFLICTS_design-desktop-animation-strict = $(pkg-desktop-animation-avoid)
CDBS_CONFLICTS_design-desktop-web-strict = $(pkg-desktop-web-avoid)

build/design-desktop:: content/desktop/preseed.cfg design-desktop-tidy

content/desktop/preseed.cfg:
	$(MAKE)

# check extracted packages and extract tidying script
CDBS_BUILD_DEPENDS +=, shellcheck
design-desktop-tidy: content/desktop/preseed.cfg
	# packages to include
	test -n "$(pkg-desktop)"
	# packages to include for -graphics
	test -n "$(pkg-desktop-graphics)"
	# packages to include for -animation
	test -n "$(pkg-desktop-animation)"
	# packages to include for -web
	test -n "$(pkg-desktop-web)"
	# packages to exclude
	test -n "$(pkg-desktop-avoid)"
	# packages to exclude only for -graphics (should be empty)
	test -z "$(pkg-desktop-graphics-avoid)"
	# packages to exclude only for -animation (should be empty)
	test -z "$(pkg-desktop-animation-avoid)"
	# packages to exclude only for -web (should be empty)
	test -z "$(pkg-desktop-web-avoid)"
	perl -0777 -n \
		-e 's,.*d-i preseed/late_command string ,#!/bin/sh\n,ms' \
		-e ' and print $$_' \
		$< > $@
	shellcheck -e SC2016 -e SC2034 -e SC2046 -e SC2048 -e SC2086 $@

binary-predeb/design-blends::
	find debian/design-data/usr/share/design-blends/data \
		-name script.sh \
		-exec chmod +x {} +

DEB_DH_INSTALL_ARGS_design-blends = content/* usr/share/design-blends/data
DEB_DH_INSTALL_ARGS_design-desktop = design-desktop-tidy usr/sbin

clean::
	$(MAKE) clean
	rm -f design-desktop-tidy
