#!/bin/sh
# Copyright (C) 2022 iopsys Software Solutions AB
# Author: AMIN Ben Romdhane <amin.benromdhane@iopsys.eu>

. /usr/share/libubox/jshn.sh
. /usr/share/bbfdm/scripts/bbf_api

serverselection_list() {
	json_add_object "serverselection"
	json_add_string "hostlist" "str"
	json_add_string "port" "str"
	json_add_string "iface" "str"
	json_add_string "ip_proto" "str"
	json_add_string "nbr_of_rep" "str"
	json_add_string "timeout" "str"
	json_add_string "protocol_used" "str"
	json_add_string "proto" "str"
	json_add_string "cancel" "str"
	json_close_object
}

serverselection_error() {
	json_init
	json_add_string "Status" "$1"
	json_add_string "FastestHost" ""
	json_add_string "IPAddressUsed" ""
	json_add_int "MinimumResponseTime" "9999"
	json_add_int "AverageResponseTime" "0"
	json_add_int "MaximumResponseTime" "0"
	json_dump

	# Store data in dmmap_diagnostics for both protocols (cwmp/usp)
	[ "$3" = "both_proto" ] && {
		$UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.DiagnosticState="$1"
		$UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.FastestHost=""
		$UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.MinimumResponseTime=9999
		$UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.AverageResponseTime=0
		$UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.MaximumResponseTime=0
		$UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.IPAddressUsed=""
		$UCI_COMMIT_BBF_DMMAP
	}
}

serverselection_launch() {
	input="$1"

	json_load "${input}"
	
	json_get_var hostlist hostlist
	json_get_var port port
	json_get_var iface iface
	json_get_var ip_proto ip_proto
	json_get_var nbr_of_rep nbr_of_rep
	json_get_var timeout timeout
	json_get_var protocol_used protocol_used
	json_get_var proto proto
	json_get_var cancel cancel

	if [ "${proto}" = "both_proto" ]; then
		old_pid=$(cat /tmp/serverselection_pid)

		[ -n "${old_pid}" ] && {
			cmd=$(cat /proc/"${old_pid}"/cmdline)
		}

		if [[ "${cmd}" = *serverselection* ]]; then
			kill -9 "${old_pid}"
		fi

		if [ "${cancel}" -eq "1" ]; then
			$UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.DiagnosticState="None"
			$UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.FastestHost=""
			$UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.MinimumResponseTime=9999
			$UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.AverageResponseTime=0
			$UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.MaximumResponseTime=0
			$UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.IPAddressUsed=""
			$UCI_COMMIT_BBF_DMMAP

			json_init
			json_add_string "Status" "None"
			json_add_string "FastestHost" ""
			json_add_string "IPAddressUsed" ""
			json_add_int "MinimumResponseTime" "9999"
			json_add_int "AverageResponseTime" "0"
			json_add_int "MaximumResponseTime" "0"
			json_dump

			return
		else
			echo $$ > /tmp/serverselection_pid
			$UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.DiagnosticState="Requested_running"
			$UCI_COMMIT_BBF_DMMAP
		fi
	fi

	# Assign default value
	[ -z "${nbr_of_rep}" ] && nbr_of_rep=3
	[ -z "${port}" ] && port=7
	[ -z "${protocol_used}" ] && protocol_used="ICMP"
	[ -z "${timeout}" ] && timeout=1 || timeout=$((timeout/1000))

	# Fail if hostlist is empty
	[ -z "${hostlist}" ] && {
		serverselection_error "Error_Internal" "${nbr_of_rep}" "${proto}"
		return
	}
	
	if [ -n "${iface}" ]; then
		device=$(ifstatus "${iface}" | jsonfilter -e '@.l3_device')

		# If no device was found, return error
		[ -z "${device}" ] && {
			serverselection_error "Error_NoRouteToHost" "${nbr_of_rep}" "${proto}"
			return
		}
	else
		device=""
	fi

	micros=1000
	success_count=0
	avg_time_sum=0
	avg_time_host=9999999
	min=9999999
	max=0
	i=0

	for host in $(echo "$hostlist" | tr "," "\n"); do
		if [ "$protocol_used" = "ICMP" ]; then
			ip_addr_used=$(get_ip_addr_used "${host}" "${ip_proto}" "${iface}")
			if [ "$ip_proto" = "IPv4" ]; then ip_proto="-4"; elif [ "$ip_proto" = "IPv6" ]; then ip_proto="-6"; else ip_proto=""; fi
			
			while [ $i -lt "$nbr_of_rep" ]; do
				i=$((i+1))

				if [ -z "${ip_proto}" ]; then
					if [ -z "${device}" ]; then
						res=$(ping -q -c 1 -W "${timeout}" "${host}" 2>&1)
					else
						res=$(ping -q -c 1 -W "${timeout}" -I "${device}" "${host}" 2>&1)
					fi
				else
					if [ -z "${device}" ]; then
						res=$(ping -q "${ip_proto}" -c 1 -W "${timeout}" "${host}" 2>&1)
					else
						res=$(ping -q "${ip_proto}" -c 1 -W "${timeout}" -I "${device}" "${host}" 2>&1)
					fi
				fi

				ba=$(echo "$res" | grep "bad address")
				[ -n "$ba" ] && {
					serverselection_error "Error_CannotResolveHostName" "${nbr_of_rep}" "${proto}"
					return
				}

				ba=$(echo "$res" | grep "unknown host")
				[ -n "$ba" ] && {
					serverselection_error "Error_CannotResolveHostName" "${nbr_of_rep}" "${proto}"
					return
				}

				stc=$(echo "$res" | grep "received")
				[ -z "$stc" ] && {
					serverselection_error "Error_Other" "${nbr_of_rep}" "${proto}"
					return
				}

				times=$(echo "$res" | grep "min/avg/max")
				[ -z "$times" ] && break

				sc1=$(echo "$stc" | awk '{print $4}')
				sc1=${sc1:-0}
				success_count=$((success_count+sc1))
				times=$(echo "$times" | awk -F'=' '{ print $2 }')
				min_time=$(echo "$times" | awk -F'[=/ ]' '{ print $2 }')	
				avg_time=$(echo "$times" | awk -F'[=/ ]' '{ print $3 }')
				max_time=$(echo "$times" | awk -F'[=/ ]' '{ print $4 }')
				min_time=${min_time:-0}
				avg_time=${avg_time:-0}
				max_time=${max_time:-0}
				min_time=$(echo "$min_time" "$micros" | awk '{printf "%3.0f\n",$1*$2}')
				avg_time=$(echo "$avg_time" "$micros" | awk '{printf "%3.0f\n",$1*$2}')
				max_time=$(echo "$max_time" "$micros" | awk '{printf "%3.0f\n",$1*$2}')
				[ "$min_time" -lt "$min" ] && min="$min_time"
				[ "$max_time" -gt "$max" ] && max="$max_time"
				avg_time_sum=$((avg_time_sum+avg_time))
			done
		else
			ip_addr_used=$(get_ip_addr_used "${host}" "${ip_proto}" "${iface}")
			if [ "$ip_proto" = "IPv4" ]; then ip_proto="--protocol 4"; elif [ "$ip_proto" = "IPv6" ]; then ip_proto="--protocol 6"; else ip_proto=""; fi
			
			while [ $i -lt "$nbr_of_rep" ]; do
				i=$((i+1))

				res=$(udpechoclientd -c 1 -t "${timeout}" --host "${host}" --port "${port}" "${ip_proto}" -i "${device}" 2>&1)

				ba=$(echo "$res" | grep "Can't Resolve Host Name")
				[ -n "$ba" ] && {
					serverselection_error "Error_CannotResolveHostName" "${nbr_of_rep}" "${proto}"
					return
				}

				ba=$(echo "$res" | grep "RCVD")
				[ -z "$ba" ] && {
					serverselection_error "Error_CannotResolveHostName" "${nbr_of_rep}" "${proto}"
					return
				}

				times=$(echo "$res" | grep "rtt")
				[ -z "$times" ] && continue

				sc1=$(echo "$res" | grep "Rcvd" | awk -F': ' '{print $3}' | awk -F'(' '{ print $1 }')
				sc1=${sc1:-0}
				success_count=$((success_count+sc1))
				max_time=$(echo "$times" | awk -F': ' '{ print $2 }' | awk -F'ms' '{ print $1 }')
				min_time=$(echo "$times" | awk -F': ' '{ print $3 }' | awk -F'ms' '{ print $1 }')
				avg_time=$(echo "$times" | awk -F': ' '{ print $4 }' | awk -F'ms' '{ print $1 }')
				min_time=${min_time:-0}
				avg_time=${avg_time:-0}
				max_time=${max_time:-0}
				min_time=$(echo "$min_time" $micros | awk '{printf "%3.0f\n",$1*$2}')
				avg_time=$(echo "$avg_time" $micros | awk '{printf "%3.0f\n",$1*$2}')
				max_time=$(echo "$max_time" $micros | awk '{printf "%3.0f\n",$1*$2}')
				[ "$min_time" -lt "$min" ] && min="$min_time"
				[ "$max_time" -gt "$max" ] && max="$max_time"
				avg_time_sum=$((avg_time_sum+avg_time))
			done
		fi

		[ "${success_count}" -gt 0 ] && avg_time=$((avg_time_sum/success_count)) || avg_time=0
		[ "${avg_time}" -ne 0 ] && [ "${avg_time}" -lt "${avg_time_host}" ] && avg_time_host="$avg_time" && min_time_host="$min" && max_time_host="$max" && fasthost="$host"
		success_count=0
		avg_time_sum=0
		min=9999999
		max=0
		i=0
	done

	json_init
	json_add_string "Status" "Complete"
	json_add_string "FastestHost" "${fasthost}"
	json_add_string "IPAddressUsed" "${ip_addr_used}"
	json_add_int "MinimumResponseTime" "${min_time}"
	json_add_int "AverageResponseTime" "${avg_time}"
	json_add_int "MaximumResponseTime" "${max_time}"
	json_dump

	# Store data in dmmap_diagnostics for both protocols (cwmp/usp)
	[ "${proto}" = "both_proto" ] && {
		$UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.DiagnosticState="Complete"
		$UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.FastestHost="${fasthost}"
		$UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.IPAddressUsed="${ip_addr_used}"
		$UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.MinimumResponseTime="${min_time_host}"
		$UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.AverageResponseTime="${avg_time_host}"
		$UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.MaximumResponseTime="${max_time_host}"
		$UCI_COMMIT_BBF_DMMAP
	}
}

if [ "$1" = "list" ]; then
	serverselection_list
elif [ -n "$1" ]; then
	serverselection_launch "$1"
else
	serverselection_error "Error_Internal" "1"
fi
