#!/bin/sh
# Generate ietf-system.yang hostname based on:
#  1. Command line argument + MAC suffix
#  2. Specific hostname from qemu_fw_cfg, from Qeneth
#  3. Default hostname + MAC suffix
#
# The MAC suffix is emitted as the %m format specifier, expanded by confd
# at apply time, so the actual MAC never ends up baked into a saved
# configuration (issue #1554).
#
set -e

if [ "$1" ]; then
    sysname="$1-%m"
elif [ -f "/sys/firmware/qemu_fw_cfg/by_name/opt/hostname/raw" ]; then
    sysname="$(cat /sys/firmware/qemu_fw_cfg/by_name/opt/hostname/raw)"
else
    sysname="%h-%m"
fi

cat <<EOF
{
  "ietf-system:system": {
    "hostname": "${sysname}"
  }
}
EOF
