#!/bin/sh

set -e

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

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

SPEC=type=bond,iface0=enp0s4,iface1=enp0s5,addr=192.168.101.131/24:192.168.101.1,extra_ovs=yes

# Without --release, the Debian release is guessed from the
# /etc/os-release of the target system (under --root). An old release
# like buster gets the legacy bond-master stanzas and the explicit
# per-NIC interfaces...
mkdir -p ${T}/buster/etc
printf 'NAME="Debian GNU/Linux"\nVERSION_ID="10"\nVERSION="10 (buster)"\nVERSION_CODENAME=buster\n' > ${T}/buster/etc/os-release
"${SCRIPT}" --root ${T}/buster --static-iface ${SPEC}
rm ${T}/buster/etc/os-release
diff -r ${FIXTURES}/buster-bond/etc ${T}/buster/etc

# ...while a current release guessed the same way gets the modern
# rendering, exactly as if --release had been passed.
mkdir -p ${T}/bookworm/etc
printf 'NAME="Debian GNU/Linux"\nVERSION_ID="12"\nVERSION="12 (bookworm)"\nVERSION_CODENAME=bookworm\n' > ${T}/bookworm/etc/os-release
"${SCRIPT}" --root ${T}/bookworm --static-iface ${SPEC}
rm ${T}/bookworm/etc/os-release
diff -r ${FIXTURES}/bond/etc ${T}/bookworm/etc

# No /etc/os-release in the target at all: default to the modern
# syntax, which is what the empty --release always meant.
mkdir -p ${T}/noosr/etc/network
"${SCRIPT}" --root ${T}/noosr --static-iface ${SPEC}
diff -r ${FIXTURES}/bond/etc ${T}/noosr/etc
