#!/bin/bash
# Tweak the stock ubuntu cloud-init config

if [ ${DIB_DEBUG_TRACE:-0} -gt 0 ]; then
    set -x
fi
set -eu
set -o pipefail

# cloud-init May not actually be installed
mkdir -p /etc/cloud/cloud.cfg.d

if [ ! -v DIB_CLOUD_INIT_ETC_HOSTS ]; then
    echo "WARNING: In the future the default setting for manage_etc_hosts will not be overridden by this element."
    echo "WARNING: Set DIB_CLOUD_INIT_ETC_HOSTS to 'localhost' to preserve current behavior."
fi

DIB_CLOUD_INIT_ETC_HOSTS=${DIB_CLOUD_INIT_ETC_HOSTS-localhost}
if [ -n "$DIB_CLOUD_INIT_ETC_HOSTS" ]; then
    dd of=/etc/cloud/cloud.cfg.d/10_etc_hosts.cfg << EOF
manage_etc_hosts: $DIB_CLOUD_INIT_ETC_HOSTS
EOF
fi
