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

# An IPv4-only BGP setup with force-frr and an explicit router-id=:
# this used to fail with "Unable to determine a BGP router ID", as the
# whole router-id= handling was nested inside the addr6= block.
mkdir -p ${T}/v4/etc/frr
printf 'bgpd=no\nzebra=yes\n' > ${T}/v4/etc/frr/daemons
"${SCRIPT}" --root ${T}/v4 --release bookworm \
	--static-iface 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,forcefrr=yes,forcefrrnic1=enp0s4,forcefrrvlan=10,forcefrrnic2=enp0s5,router-id=192.168.101.131
diff -r ${FIXTURES}/router-id-forcefrr-v4/etc ${T}/v4/etc

# The explicit router-id= wins over the addr6=-derived one, and the
# router-id= validation now also runs without any addr6=.
mkdir -p ${T}/dual/etc/frr
printf 'bgpd=no\nzebra=yes\n' > ${T}/dual/etc/frr/daemons
"${SCRIPT}" --root ${T}/dual --release bookworm \
	--static-iface 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,forcefrr=yes,forcefrrnic1=enp0s4,forcefrrvlan=10,forcefrrnic2=enp0s5,addr6=2001:db8:1::131/64,router-id=192.168.101.131
grep -q '^ bgp router-id 192.168.101.131$' ${T}/dual/etc/frr/frr.conf

set +e
"${SCRIPT}" --root ${T}/v4 --release bookworm \
	--static-iface type=normal,iface0=enp0s4,addr=192.168.101.131/24,router-id=not.a.quad >${T}/out 2>&1
RC=$?
set -e
if [ "${RC}" = "0" ] || ! grep -q "must be a dotted quad" ${T}/out ; then
	echo "${ME}: a bogus router-id= must fail even without addr6="
	cat ${T}/out
	exit 1
fi
