#!/bin/sh -eu
src=$(dpkg-parsechangelog -SSource)

have=no
if dpkg -l dietlibc-dev >/dev/null 2>/dev/null ; then
	have=yes
fi

if dpkg-vendor --derives-from Ubuntu ; then
	have=no
fi

want=no
IFS=', '
set -- ${DEB_BUILD_PROFILES:-}
for profile ; do
	if [ "${profile}" = "pkg.${src}.diet" ] ; then
		want=yes
	fi
done

if [ "${want}" = yes ] && [ "${have}" = no ] ; then
	printf '$(error %s)' "${msg}" > debian/diet.mk
else
	cat << EOF > debian/diet.mk
HAVE_DIETLIBC = ${have}
WANT_DIETLIBC = ${want}
EOF
fi
if [ "${want}" = yes ] ; then
	built_using=$(dpkg-query -f'${source:Package} (= ${source:Version})' -W dietlibc-dev)
	cat << EOF >> debian/diet.mk
override_dh_gencontrol:
	dh_gencontrol -- -VBuilt-Using="${built_using}"
EOF
fi
printf 'debian/diet.mk'
