#!/bin/sh

set -e

ME=$(basename $0)
SCRIPT=$(readlink -f ../../../odi-gen-net-config)

T=$(mktemp -d /tmp/odi-gen-net-config-sysid.XXXXXX)
trap 'rm -rf ${T}' EXIT

# A full render with --hostname and the package installed writes the
# pin with the FQDN passed on the command line.
mkdir -p ${T}/etc/network ${T}/var/lib/dpkg/info
touch ${T}/var/lib/dpkg/info/openvswitch-switch.list
"${SCRIPT}" --root ${T} --release bookworm --hostname host01.example.com --static-iface type=normal,iface0=eth0,addr=192.168.1.10/24
grep -q '^OVS_CTL_OPTS="--system-id=host01.example.com"$' ${T}/etc/default/openvswitch-switch

# Without --hostname nothing is written.
rm -rf ${T}/etc
"${SCRIPT}" --root ${T} --release bookworm --static-iface type=normal,iface0=eth0,addr=192.168.1.10/24
if test -f ${T}/etc/default/openvswitch-switch ; then
	echo "the pin must not be written without --hostname"
	exit 1
fi

# Without the openvswitch-switch package installed nothing is
# written: the conffile install would archive it as .dpkg-old.
rm -rf ${T}/etc ${T}/var
"${SCRIPT}" --root ${T} --release bookworm --hostname host01.example.com --static-iface type=normal,iface0=eth0,addr=192.168.1.10/24
if test -f ${T}/etc/default/openvswitch-switch ; then
	echo "the pin must not be written before the package install"
	exit 1
fi

echo "OK: $ME"
