#!/bin/sh

# Checks the meshviewer data from a merged event and tries to find links
# between FFV and the other network
#
# needs curl and jq to be installed

[ -z $1 ] && echo "input $1 not provided" && exit 0
[ ! -e $1 ] && echo "input $1 not found" && exit 0
[ ! -e nodes.json ] && curl 'https://mapdata.freifunk-vogtland.net/nodes.json' > nodes.json

echo "checking $1"

cat $1 | python -m json.tool > meshviewer.pp.json

cat nodes.json | python -m json.tool | grep "node_id" | sed 's/.*\ \"//' | sed 's/",$//' | sort -n | uniq > ffv.node_id
for node_id in $(cat meshviewer.pp.json  | grep "node_id" | sed 's/.*\ \"//' | sed 's/",$//' ); do
	grep -q $node_id ffv.node_id || echo $node_id
done > others.node_id

jq '.links[] |  .source + " " + .target + " " + .type' $1 | grep -v vpn | sed -s 's/\"//g' > links.txt

echo "checking $(wc -l links.txt) links"

cat links.txt | while read LINE; do
	P1=$(echo $LINE | awk '{ print $1 }')
	P2=$(echo $LINE | awk '{ print $2 }')
	grep -q $P1 ffv.node_id && grep -q $P2 others.node_id && echo found $P1 $P2 && continue
	grep -q $P2 ffv.node_id && grep -q $P1 others.node_id && echo found $P1 $P2 && continue
	grep -q $P1 ffv.node_id && grep -q $P2 ffv.node_id && echo FFV only link && continue 
	grep -q $P1 others.node_id && grep -q $P2 others.node_id && echo others only link && continue
	echo "link with unknown node_ids: $P1 $P2"
	grep -q $P1 ffv.node_id && echo $P1 is ffv
	grep -q $P2 ffv.node_id && echo $P2 is ffv
	grep -q $P1 others.node_id && echo $P1 is others
	grep -q $P2 others.node_id && echo $P2 is others
done

