#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2014 Jonas Smedegaard <dr@jones.dk>
# Description: Main Debian packaging script for DebianParl
#
# 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

# dependencies for pkglist2preseed script
CDBS_BUILD_DEPENDS +=, libipc-system-simple-perl, libpath-tiny-perl

_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-world-bulk = $(call _pkg-bulk,desktop/all)
pkg-desktop-eu-bulk = $(call _pkg-bulk,desktop/eu)
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-world = $(strip \
	$(filter-out $(pkg-desktop),\
		$(patsubst %,%$(comma),\
			$(filter-out %-,$(pkg-desktop-world-bulk)))))
pkg-desktop-eu = $(strip \
	$(filter-out $(pkg-desktop),\
		$(patsubst %,%$(comma),\
			$(filter-out %-,$(pkg-desktop-eu-bulk)))))
pkg-desktop-avoid = $(strip \
	$(patsubst %-,%$(comma),$(filter %-,$(pkg-desktop-bulk))))
pkg-desktop-world-avoid = $(strip \
	$(patsubst %-,%$(comma),\
		$(filter-out $(pkg-desktop-bulk),\
			$(filter %-,$(pkg-desktop-world-bulk)))))
pkg-desktop-eu-avoid = $(strip \
	$(patsubst %-,%$(comma),\
		$(filter-out $(pkg-desktop-bulk),\
			$(filter %-,$(pkg-desktop-eu-bulk)))))

CDBS_DEPENDS_parl-desktop = $(pkg-desktop)
CDBS_DEPENDS_parl-desktop-world = $(pkg-desktop-world)
CDBS_DEPENDS_parl-desktop-eu = $(pkg-desktop-eu)
CDBS_CONFLICTS_parl-desktop-strict = $(pkg-desktop-avoid)
CDBS_CONFLICTS_parl-desktop-world-strict = $(pkg-desktop-world-avoid)
CDBS_CONFLICTS_parl-desktop-eu-strict = $(pkg-desktop-eu-avoid)

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

content/desktop/preseed.cfg:
	mkdir -p content
	$(MAKE)

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

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

clean::
	rm -rf content
	rm -f parl-desktop-tidy
