#!/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-paths.XXXXXX)
trap 'rm -rf ${T}' EXIT

SPEC=type=bgp,vlannum=10,iface0=enp0s4,iface1=enp0s5,addr=192.168.101.131/24:192.168.101.1,asnum=65499,addbgpi=192.168.101.131

# Absolute paths are used as-is, relative paths are resolved against
# --root, and the defaults land in the standard places under --root.
# File flags carry the -path suffix, directory flags the -dir suffix.
# The generated contents are checked against the golden fixtures of
# the bgp and bond scenarios.
mkdir -p ${T}/root/my-frr
printf 'bgpd=no\nzebra=yes\n' > ${T}/root/my-frr/daemons
"${SCRIPT}" --root ${T}/root --release bookworm \
	--eni-path ${T}/absolute-eni \
	--frr-path my-frr/my-frr.conf \
	--frr-daemons-path ${T}/root/my-frr/daemons \
	--static-iface ${SPEC}

test -f ${T}/absolute-eni
if [ -e ${T}/root/etc/network/interfaces ] ; then
	echo "${ME}: --eni-path with an absolute path must override the default"
	exit 1
fi
diff ${FIXTURES}/bgp/etc/network/interfaces ${T}/absolute-eni
diff ${FIXTURES}/bgp/etc/frr/frr.conf ${T}/root/my-frr/my-frr.conf
grep -q '^bgpd=yes$' ${T}/root/my-frr/daemons

# The modprobe dir flag with a bond setup.
"${SCRIPT}" --root ${T}/bond --release bookworm \
	--modprobe-dir my-modprobe \
	--static-iface type=bond,iface0=enp0s4,iface1=enp0s5,addr=192.168.101.131/24:192.168.101.1
diff ${FIXTURES}/bond/etc/modprobe.d/bonding.conf ${T}/bond/my-modprobe/bonding.conf

# No --root at all defaults to the current node, which is only
# exercised through --root here: the no-root mode writes the real
# /etc and must never run in a test.
mkdir -p ${T}/slashroot/etc/frr
printf 'bgpd=no\nzebra=yes\nospfd=no\n' > ${T}/slashroot/etc/frr/daemons
"${SCRIPT}" --root ${T}/slashroot --release bookworm --static-iface ${SPEC}
diff ${FIXTURES}/bgp/etc/frr/frr.conf ${T}/slashroot/etc/frr/frr.conf
