#!/bin/bash
# Run script with sudo!
# NOTE: Rebuild IAP with DUMMY_PROG option enabled to skip any read/write ioperations during the test.

ETH=enp5s0
IP=$1
MTU_MIN=400
MTU_MAX=1500

total=()
failed=()
success=()

echo "Test MTUs: $MTU_MIN - $MTU_MAX"
for (( size=$MTU_MIN; size<=$MTU_MAX; size++ ))
do
	ifconfig enp5s0 mtu $size
	MTU=$(ifconfig $ETH | grep MTU | awk '{print $5}' | awk -F ":" '{print $2}')
	total+=($MTU)
	./tools/upload.sh $IP > /dev/null 2>&1
	if [ $? -eq 0 ]
	then
		echo "MTU $MTU - OK"
		success+=($MTU)
	else
		echo "MTU $MTU - Error"
		failed+=($MTU)
		# IAP will reboot automaticaly by timeout
		echo "Waiting IAP reboot timeout..."
		sleep 25
	fi
	sleep 5
done

echo "Success: ${#success[@]}/${#total[@]}"
if [ ${#failed[@]} -ne 0 ]
then
	echo "Failed: ${#failed[@]}/${#total[@]}"
	echo "Failed MTUs: ${failed[@]}"
fi