#!/bin/sh -efu

# Defaults
computer="altws"
template="COMPUTERNAME"
ai="/tmp/metadata/autoinstall.scm"

[ -s "$ai" ] || exit 0

# Try to guess enterprise-specific computer name
baremetal="$(head -n1 /sys/class/dmi/id/product_name 2>/dev/null ||:)"
case "$baremetal" in
NUC6i7KYB)
	computer="nuc"
	;;
"Graviton N15i")
	computer="graviton"
	;;
esac

# Add the MAC-address part
iface="$(ip route 2>/dev/null |grep -E '^default via ' |awk '{print $5;}')"
[ -n "$iface" ] || iface="$(ls -1 /sys/class/net/ |grep -v lo |head -n1)"
hw="$(ip link show dev "${iface:-eth0}" 2>/dev/null |tail -n1 |
	awk '{print $2;}' |sed 's,:,,g' |cut -c7-12)"
[ -z "$hw" ] || computer="$computer-$hw"

# Change template to the computer name
sed -i "s/$template/$computer/g" "$ai" ||:
