#!/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. /etc/hostname from squashfs + MAC suffix
#
set -e

gen()
{
    base_mac=$(jq -r '.["mac-address"]' /run/system.json)
    if [ -z "$base_mac" ] || [ "$base_mac" = "null" ]; then
	base_mac=00:00:00:00:00:00
    fi

    echo "$1-$(echo $base_mac | tail -c 9 | tr ':' '-')"
}

if [ "$1" ]; then
    sysname=$(gen "$1")
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=$(gen "$(cat /etc/hostname)")
fi

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