#!/bin/bash

show_ip(){
	local output=$1
	ips=()
	interfaces=()
	echo $output
	while read -r line; do
		ip=$(echo "$line" | awk '{print $2}' | cut -d'/' -f1)
		interface=$(echo "$line" | awk '{print $5}')
		ips+=("$ip")
		interfaces+=("$interface")
	done <<< "$output"
	for i in "${!ips[@]}"; do
		echo "${ips[i]} ${interfaces[i]}"
	done

}

ip_cmd='ip address show | grep 10.5'
sender_ip='10.5.1.1/24'
rec_ip='10.5.1.2/24'
rec_ip_no_code='10.5.1.2'
senders=()
recs=()
sender_eths=()
rec_eths=()
losses=()

sender_type="${1:-router}"
receiver_type="${2:-router}"
if [ "$sender_type" = "router" ]; then
	sender_ub=4
else
	sender_ub=3
fi
if [ "$receiver_type" = "router" ]; then
	rec_ub=4
else
	rec_ub=3
fi
for sender in $(seq 1 $sender_ub);
do
	sender_eth_nums=$(ssh $sender_type$sender "ip address show" | grep -c '^.*eth[0-9]:')
	sender_eth_nums=$(($sender_eth_nums-1))
	echo "$sender_type$sender has $sender_eth_nums eths"
	for sender_eth_num in $(seq 1 $sender_eth_nums);
	do
		# turn on the device
		ssh $sender_type$sender "ip link set dev eth$sender_eth_num up"
		ssh $sender_type$sender "ip address add $sender_ip dev eth$sender_eth_num"
		# for receiver in {1..4}
		for receiver in $(seq 1 $rec_ub);
		do
			receiver_eth_nums=$(ssh $receiver_type$receiver "ip address show" | grep -c '^.*eth[0-9]:')
			receiver_eth_nums=$(($receiver_eth_nums-1))
			echo "$receiver_type$receiver has $receiver_eth_nums eths"
			if [ "$sender_type" = "$receiver_type" ]; then
				if [ "$sender" -eq "$receiver" ]; then
					continue
				fi
			fi
			for receiver_eth_num in $(seq 1 $receiver_eth_nums);
			do
				ssh $receiver_type$receiver "ip link set dev eth$receiver_eth_num up"
				ssh $receiver_type$receiver "ip address add $rec_ip dev eth$receiver_eth_num"
				echo "sen $sender_type$sender eth$sender_eth_num ip $sender_ip"
				echo "rec $receiver_type$receiver eth$receiver_eth_num ip $rec_ip"
				ip_output=$(ssh $sender_type$sender $ip_cmd)
				result=$(show_ip "$ip_output")
				echo $result

				# test loss
				loss=$(ssh $sender_type$sender "ping -c 5 -W 2 -I eth$sender_eth_num $rec_ip_no_code | awk -F', ' '/packet loss/ {print \$3}' | awk '{print int(\$1)}'")
				echo $loss
				if [ "$loss" -ne 100 ]; then
					senders+=("$sender")
					recs+=("$receiver")
					sender_eths+=("$sender_eth_num")
					rec_eths+=("$receiver_eth_num")
					losses+=("$loss")
				fi


				ssh $receiver_type$receiver "ip address del 10.5.1.2/24 dev eth$receiver_eth_num"
			done
		done
		ssh $sender_type$sender "ip address del 10.5.1.1/24 dev eth$sender_eth_num"
	done
done

echo "results"

printf "%-10s %-15s %-12s %-15s %-10s\n" "sender #" "sender eth #" "receiver #" "receiver eth #" "losses #"
for i in "${!senders[@]}"; do
    printf "%-10s %-15s %-12s %-15s %-10s\n" "${senders[i]}" "${sender_eths[i]}" "${recs[i]}" "${rec_eths[i]}" "${losses[i]}"
done

# printf "sender #\t sender eth #\t receiver # \t receiver eth # \t losses # \t\n"
# for i in "${!senders[@]}"; do
#  	printf "%s\t%s\t%s\t%s\t%s\n" "${senders[i]}" "${sender_eths[i]}" "${recs[i]}" "${rec_eths[i]}" "${losses[i]}"
# done

