#!/bin/bash

set -e

RULESET="table inet x {
	set y {
		type inet_service
		flags interval
		auto-merge
	}
}"

nft -f - <<< $RULESET

tmpfile=$(mktemp)
echo $tmpfile
echo -n "add element inet x y { " > $tmpfile
for ((i=0;i<65535;i+=2))
do
	echo -n "$i, " >> $tmpfile
	if [ $i -eq 65534 ]
	then
		echo -n "$i" >> $tmpfile
	fi
done
echo "}" >> $tmpfile

nft -f $tmpfile

tmpfile2=$(mktemp)
echo $tmpfile2
for ((i=1;i<65535;i+=2))
do
	echo "$i" >> $tmpfile2
done

tmpfile3=$(mktemp)
echo $tmpfile3
shuf $tmpfile2 > $tmpfile3
i=0
cat $tmpfile3 | while read line && [ $i -lt 10 ]
do
	time nft add element inet x y { $line }
	i=$((i+1))
done

for ((i=0;i<10;i++))
do
	from=$(($RANDOM%65535))
	to=$(($from+100))
	echo "$from-$to"
	nft add element inet x y { $from-$to }
	nft get element inet x y { $from-$to }
	from2=$(($from+10))
	to2=$(($to-10))
	echo "$from, $to, $from2-$to2"
	nft delete element inet x y { $from, $to, $from2-$to2 }
	from=$(($from+1))
	nft get element inet x y { $from }
	to=$(($to-1))
	nft get element inet x y { $to }
	from2=$(($from2-1))
	echo $from2
	nft get element inet x y { $from2 }
	to2=$(($to2+1))
	echo $to2
	nft get element inet x y { $to2 }
done
