#!/bin/bash -eu

. debian/debian.env

# Remove snapdragon from d-i
sed -i /snapdragon/d ${DEBIAN}/d-i/kernel-versions

# Remove snapdragon from getabis
sed -i /snapdragon/d ${DEBIAN}/etc/getabis

# We don't build snapdragon
sed -i '/_snapdragon/d' ${DEBIAN}/rules.d/arm64.mk
sed -i 's/^\(flavo.*\)\ssnapdragon\(.*\)$/\1\2/' ${DEBIAN}/rules.d/arm64.mk
rm -f ${DEBIAN}/config/arm64/config.flavour.snapdragon

# Do not enforce DMESG_RESTRICT in focal
sed -i 's/CONFIG_SECURITY_DMESG_RESTRICT=y/# CONFIG_SECURITY_DMESG_RESTRICT is not set/' ${DEBIAN}/config/config.common.ubuntu
sed -i "s/CONFIG_SECURITY_DMESG_RESTRICT                  policy<{'amd64': 'y', 'arm64': 'y', 'armhf': 'y', 'ppc64el': 'y', 's390x': 'y'}>/CONFIG_SECURITY_DMESG_RESTRICT                  policy<{'amd64': 'n', 'arm64': 'n', 'armhf': 'n', 'ppc64el': 'n', 's390x': 'n'}>/" ${DEBIAN}/config/annotations

# KCSAN cannot be enabled in focal, so make sure it's disabled in the
# annotations file as well for all arches.
sed -i "s/CONFIG_KCSAN                                    policy<{'amd64': 'n', 'arm64': '-', 'armhf': '-', 'ppc64el': '-', 's390x': '-'}>/CONFIG_KCSAN                                    policy<{'amd64': '-', 'arm64': '-', 'armhf': '-', 'ppc64el': '-', 's390x': '-'}>/" ${DEBIAN}/config/annotations

# Drop CONFIG_DEBUG_INFO_DWARF5 because we have a toolchain too old to support
# this in focal.
sed -i "/CONFIG_DEBUG_INFO_DWARF5/d" ${DEBIAN}/config/annotations
sed -i 's/.*CONFIG_DEBUG_INFO_DWARF5.*/# CONFIG_DEBUG_INFO_DWARF5 is not set/' ${DEBIAN}/config/config.common.ubuntu

# Focal does not build for i386 and thus not care. This option was made only available for
# 64bit/amd64 builds.
sed -i "s/CONFIG_INTEL_IOMMU_SVM                          policy<{'amd64': 'y', 'i386': 'y'}>/CONFIG_INTEL_IOMMU_SVM                          policy<{'amd64': 'y'}>/" ${DEBIAN}/config/annotations

# Do not enforce CONFIG_ARM64_BTI_KERNEL in Focal, as it depends on a newer
# gcc version. Note that the config itself is already removed in an earlier
# stage when 'make' is run, updating config 'GCC_VERSION' and removing
# 'ARM64_BTI_KERNEL'.
sed -i "/CONFIG_ARM64_BTI_KERNEL                         policy<{'arm64': 'y'}>/d" ${DEBIAN}/config/annotations

# Re-enable AUFS
#
# We are still supporting AUFS in focal, so make sure to re-enable it.
cat << EOF >> ${DEBIAN}/config/config.common.ubuntu
CONFIG_AUFS_BDEV_LOOP=y
# CONFIG_AUFS_BRANCH_MAX_1023 is not set
CONFIG_AUFS_BRANCH_MAX_127=y
# CONFIG_AUFS_BRANCH_MAX_32767 is not set
# CONFIG_AUFS_BRANCH_MAX_511 is not set
# CONFIG_AUFS_BR_FUSE is not set
CONFIG_AUFS_BR_HFSPLUS=y
# CONFIG_AUFS_BR_RAMFS is not set
# CONFIG_AUFS_DEBUG is not set
CONFIG_AUFS_DIRREN=y
CONFIG_AUFS_EXPORT=y
# CONFIG_AUFS_FHSM is not set
CONFIG_AUFS_FS=m
# CONFIG_AUFS_HNOTIFY is not set
CONFIG_AUFS_INO_T_64=y
# CONFIG_AUFS_RDU is not set
CONFIG_AUFS_SBILIST=y
# CONFIG_AUFS_SHWH is not set
CONFIG_AUFS_XATTR=y
EOF

TMPFILE=$(mktemp)
cat <<EOF >${TMPFILE} || true
CONFIG_AUFS_FS                                  policy<{'amd64': 'm', 'arm64': 'm', 'armhf': 'm', 'i386': 'm', 'ppc64el': 'm', 's390x': 'm'}>
CONFIG_AUFS_HNOTIFY                             policy<{'amd64': 'n', 'arm64': 'n', 'armhf': 'n', 'i386': 'n', 'ppc64el': 'n', 's390x': 'n'}>
CONFIG_AUFS_EXPORT                              policy<{'amd64': 'y', 'arm64': 'y', 'armhf': 'y', 'i386': 'y', 'ppc64el': 'y', 's390x': 'y'}>
CONFIG_AUFS_XATTR                               policy<{'amd64': 'y', 'arm64': 'y', 'armhf': 'y', 'i386': 'y', 'ppc64el': 'y', 's390x': 'y'}>
CONFIG_AUFS_FHSM                                policy<{'amd64': 'n', 'arm64': 'n', 'armhf': 'n', 'i386': 'n', 'ppc64el': 'n', 's390x': 'n'}>
CONFIG_AUFS_RDU                                 policy<{'amd64': 'n', 'arm64': 'n', 'armhf': 'n', 'i386': 'n', 'ppc64el': 'n', 's390x': 'n'}>
CONFIG_AUFS_DIRREN                              policy<{'amd64': 'y', 'arm64': 'y', 'armhf': 'y', 'i386': 'y', 'ppc64el': 'y', 's390x': 'y'}>
CONFIG_AUFS_SHWH                                policy<{'amd64': 'n', 'arm64': 'n', 'armhf': 'n', 'i386': 'n', 'ppc64el': 'n', 's390x': 'n'}>
CONFIG_AUFS_BR_RAMFS                            policy<{'amd64': 'n', 'arm64': 'n', 'armhf': 'n', 'i386': 'n', 'ppc64el': 'n', 's390x': 'n'}>
CONFIG_AUFS_BR_FUSE                             policy<{'amd64': 'n', 'arm64': 'n', 'armhf': 'n', 'i386': 'n', 'ppc64el': 'n', 's390x': 'n'}>
CONFIG_AUFS_BR_HFSPLUS                          policy<{'amd64': 'y', 'arm64': 'y', 'armhf': 'y', 'i386': 'y', 'ppc64el': 'y', }>
CONFIG_AUFS_DEBUG                               policy<{'amd64': 'n', 'arm64': 'n', 'armhf': 'n', 'i386': 'n', 'ppc64el': 'n', 's390x': 'n'}>
#
CONFIG_AUFS_EXPORT                              note<LP:1121699>
EOF
gawk -i inplace "/CONFIG_AUFS_FS                                  policy<{'amd64': 'n', 'arm64': 'n', 'armhf': 'n', 'ppc64el': 'n', 's390x': 'n'}>/{system(\"cat ${TMPFILE}\");next}1" ${DEBIAN}/config/annotations
rm -f ${TMPFILE}

# Override options in rules.d/hooks.mk (normally master does not have this
# file but it got added for generic annotations enforcement.
cat <<EOD >>${DEBIAN}/rules.d/hooks.mk
do_libc_dev_package	= false
do_doc_package		= false
do_tools_common		= false
do_tools_host		= false
EOD

