#!/bin/bash

. debian/debian.env

vars=$1
any_signed=$2

. $vars

[ "$provides" != '' ] && provides="$provides, "

if [ "$is_sub" = "" ]; then
	flavour=$(basename $vars | sed 's/.*\.//')
	stub="${DEBIAN}/control.d/flavour-control.stub debian/control.d/flavour-buildinfo.stub"
	if [ "$any_signed" = 'true' ]; then
		sign_me_pkg=""
		sign_me_txt=""
		sign_peer_pkg=""
	else
		sign_me_pkg=""
		sign_me_txt=""
		sign_peer_pkg=""
	fi
else
	flavour=$(basename $vars .vars)
	stub=${DEBIAN}/sub-flavours/control.stub
fi

cat $stub | grep -v '^#' | sed \
	-e "s#FLAVOUR#$flavour#g"		\
	-e "s#DESC#$desc#g"			\
	-e "s#ARCH#$arch#g"			\
	-e "s#SUPPORTED#$supported#g"		\
	-e "s#TARGET#$target#g"			\
	-e "s#BOOTLOADER#$bootloader#g" 	\
	-e "s#=PROVIDES=#$provides#g"		\
	-e "s#=CONFLICTS=#$conflicts#g"		\
	-e "s#=SIGN-ME-PKG=#$sign_me_pkg#g"	\
	-e "s#=SIGN-ME-TXT=#$sign_me_txt#g"	\
	-e "s#=SIGN-PEER-PKG=#$sign_peer_pkg#g"

while read package version extras
do
	module="$package"
	module_type=

	# Module arch parameters are skipped here, so a package section will
	# be generated for each flavour, and its Architecture will be set to
	# all architectures with that flavour. Even that is being generated,
	# it doesn't follow all of them will be built. That's to work-around
	# dkms_exclude/dkms_include that manipulates supported architectures
	# in $(DEBIAN)/rules.d/$(arch).mk.
	for param in $extras; do
		case "$param" in
		modulename=*) module="${param#modulename=}" ;;
		type=*) module_type="${param#type=}" ;;
		*) continue ;;
		esac
	done

	[ "$module_type" = "standalone" ] || continue

	cat debian/control.d/flavour-module.stub | grep -v '^#' | sed	\
		-e "s#ARCH#$arch#g"		\
		-e "s#MODULE#$module#g"		\
		-e "s#FLAVOUR#$flavour#g"
done <"${DEBIAN}/dkms-versions"
