#!/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

# Use gzip instead of lz4 for amd64
sed -i '/CONFIG_KERNEL_LZ4/d' ${DEBIAN}/config/amd64/config.common.amd64
sed -i '3aCONFIG_KERNEL_GZIP=y\n# CONFIG_KERNEL_LZ4 is not set' ${DEBIAN}/config/amd64/config.common.amd64

# Use xz instead of lz4 for s390x
sed -i '/CONFIG_KERNEL_LZ4/d' ${DEBIAN}/config/s390x/config.common.s390x
sed -i '3aCONFIG_KERNEL_XZ=y\n# CONFIG_KERNEL_LZ4 is not set' ${DEBIAN}/config/s390x/config.common.s390x

# Use the options with the bionic compiler
sed -i 's/# CONFIG_TUNE_Z13 is not set/CONFIG_TUNE_Z13=y/' ${DEBIAN}/config/config.common.ubuntu
sed -i 's/CONFIG_TUNE_Z15=y/# CONFIG_TUNE_Z15 is not set/' ${DEBIAN}/config/config.common.ubuntu
sed -i 's/CONFIG_MARCH_Z15_TUNE=y/CONFIG_MARCH_Z13_TUNE=y/' ${DEBIAN}/config/config.common.ubuntu

# 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

# And adjust annotations accordingly
sed -i "s/CONFIG_KERNEL_LZ4                               policy<{'amd64': 'y', 'armhf': 'n', 's390x': 'y'}>/CONFIG_KERNEL_LZ4                               policy<{'amd64': 'n', 'armhf': 'n', 's390x': 'n'}>/" ${DEBIAN}/config/annotations
sed -i "s/CONFIG_KERNEL_GZIP                              policy<{'amd64': 'n', 'armhf': 'y', 'ppc64el': 'y', 's390x': 'n'}>/CONFIG_KERNEL_GZIP                              policy<{'amd64': 'y', 'armhf': 'y', 'i386': 'y', 'ppc64el': 'y', 's390x': 'n'}>/" ${DEBIAN}/config/annotations
sed -i "s/CONFIG_KERNEL_XZ                                policy<{'amd64': 'n', 'armhf': 'n', 'ppc64el': 'n', 's390x': 'n'}>/CONFIG_KERNEL_XZ                                policy<{'amd64': 'n', 'armhf': 'n', 'ppc64el': 'n', 's390x': 'y'}>/" ${DEBIAN}/config/annotations

sed -i "s/CONFIG_TUNE_Z13                                 policy<{'s390x': 'n'}>/CONFIG_TUNE_Z13                                 policy<{'s390x': 'y'}>/" ${DEBIAN}/config/annotations
sed -i "s/CONFIG_TUNE_Z15                                 policy<{'s390x': 'y'}>/CONFIG_TUNE_Z15                                 policy<{'s390x': 'n'}>/" ${DEBIAN}/config/annotations

# 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

