#!/bin/sh

if [ "$1" == "discovery" ]; then
	sockets=`ls /var/run/dpinger_*.sock`
	result=`echo -n '{"data": ['`
	for i in $sockets ; do
		data=`echo $i | cut -c 18- | cut -d "~" -f 1`
		result=`echo -n $result'{"{#GWNAME}": "'`
		result=`echo -n $result"$data"`
		result=`echo -n $result'"},'`
	done
	result=`echo $result | sed 's/.$//'`
	result=`echo -n $result']}'`
elif [ "$1" == "latency" ]; then
	result=`cat /var/run/dpinger_${2}~*.sock 2> /dev/null | awk '{print $2 / 1000}' | bc | sed 's/^\./0./'`
elif [ "$1" == "packetloss" ]; then
	result=`cat /var/run/dpinger_${2}~*.sock 2> /dev/null | awk '{print $4}'`
fi

echo $result
