#!/bin/sh
# Copyright (C) 2015 PIVA Software <www.pivasoftware.com>
# 	Author: MOHAMED Kallel <mohamed.kallel@pivasoftware.com>

[ "$1" != "run" ] && return

UCI_GET_VARSTATE="/sbin/uci -q ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} get"
UCI_SET_VARSTATE="/sbin/uci -q ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} set"

ipping_get() {
	local val=`$UCI_GET_VARSTATE $1`
	echo ${val:-$2}
}

ipping_launch() {
	local i res ba stc times sc1 success_count failure_count min_time avg_time max_time avg_time_sum min max
	[ "`$UCI_GET_VARSTATE easycwmp.@local[0].DiagnosticsState`" != "Requested" ] && return
	local host=`ipping_get easycwmp.@local[0].Host`
	local cnt=`ipping_get easycwmp.@local[0].NumberOfRepetitions 3`
	local dsize=`ipping_get easycwmp.@local[0].DataBlockSize 64`
	local timeout=`ipping_get easycwmp.@local[0].Timeout 1000`
	echo 
	[ "$host" = "" ] && return
	timeout=$((timeout/1000))
	[ "$timeout" = "0" ] && timeout = "1"
	success_count=0
	avg_time_sum=0
	min=9999999
	max=0
	i=0
	while [ $i -lt $cnt ]; do
		let i++
		res=$(ping -q -c 1 -s $dsize -W $timeout $host 2>&1)
		ba=`echo "$res" | grep "bad address"`
		[ -n "$ba" ] && { $UCI_SET_VARSTATE easycwmp.@local[0].DiagnosticsState=Error_CannotResolveHostName; return; }
		stc=`echo "$res" | grep "packets received"`
		[ -z "$stc" ] && { $UCI_SET_VARSTATE easycwmp.@local[0].DiagnosticsState=Error_Other; return; }
		times=`echo "$res" | grep "round-trip"`
		[ -z "$times" ] && continue
		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 $1 }'`	
		avg_time=`echo $times | awk -F'[=/ ]' '{ print $2 }'`
		max_time=`echo $times | awk -F'[=/ ]' '{ print $3 }'`
		min_time=${min_time:-0}
		avg_time=${avg_time:-0}
		max_time=${max_time:-0}
		min_time=$(awk "BEGIN{print $min_time * 1000}")
		avg_time=$(awk "BEGIN{print $avg_time * 1000}")
		max_time=$(awk "BEGIN{print $max_time * 1000}")
		[ $min_time -lt $min ] && min=$min_time
		[ $max_time -gt $max ] && max=$max_time
		avg_time_sum=$((avg_time_sum+avg_time))
	done
	failure_count=$((cnt-success_count))
	[ $success_count -gt 0 ] && avg_time=$((avg_time_sum/success_count)) || avg_time=0
	min_time=$min
	max_time=$max
	$UCI_SET_VARSTATE easycwmp.@local[0].DiagnosticsState=Complete
	$UCI_SET_VARSTATE easycwmp.@local[0].SuccessCount=$success_count
	$UCI_SET_VARSTATE easycwmp.@local[0].FailureCount=$failure_count
	$UCI_SET_VARSTATE easycwmp.@local[0].AverageResponseTime=$avg_time
	$UCI_SET_VARSTATE easycwmp.@local[0].MinimumResponseTime=$min_time
	$UCI_SET_VARSTATE easycwmp.@local[0].MaximumResponseTime=$max_time
	event_dignostic
}

event_dignostic() {
	local e=1
	local i=0
	
	/usr/bin/killall -s USR1 easycwmpd
	
	return 0
	
	while [ "$e" != 0 -a $i -lt 200 ]; do	
		ubus -t 1 call tr069 inform '{"event":"8 DIAGNOSTICS COMPLETE"}'
		e=$?
		[ "$e" != "0" ] && sleep 1;
		let i++
	done
}

ipping_launch
