#!/bin/sh

# TO DO:
 # This script needs some volunteer work
 # It currently gets the path through bmx6 and babeld, 
 # but it should actually be able to detect any protocol is being used and 
 # use a strategy for each case.

result=''
# if 8.8.8.8 is accessible, print internet path
if check-internet; then
    #BMX6 Path to INternet
    if [[ -f '/usr/lib/opkg/info/lime-proto-bmx6.control' ]]; then 
        default_dev=`ip r | grep "default dev" | cut -d' ' -f3`;
        gw=`bmx6 -c show tunnels | grep $default_dev | grep inet4 | awk '{ print $10 }'`;
        for node in `mtr -6 -r -c 1 $gw.mesh  | grep "\.|" |  awk '{ print $2}' | cut -d'.' -f1`; do
                nodename=$(wget --no-check-certificate http://$path/cgi-bin/hostname -qO -)
                path="$path,{\"ip\":\"$node\",\"hostname\":\"$nodename\"}"
        done
        result="[${path#?}]"
    
    #Babeld Path to Internet
    elif [[ -f '/usr/lib/opkg/info/lime-proto-babeld.control' ]]; then
        path=''
        for ip in `mtr -4 -r --no-dns -c1 8.8.8.8 | grep "\.|" |  awk '{ print $2}' | grep '^10\.'`; do
            name=$(nslookup $ip | grep name | cut -d '=' -f 2 | sed "s/ //" | cut -d '.' -f 1)
            if [[ "$name" = "" ]]; then
                name=$(wget --no-check-certificate http://$ip/cgi-bin/hostname -qO -)
            fi
            path="$path,{\"ip\":\"$ip\",\"hostname\":\"$name\"}"
        done
        result="[${path#?}]"
    fi
fi

printf $result

if [ ! -z "$result" ] && [ "$1" = "save" ] ; then
    echo -n -e "$result" > /tmp/last_internet_path
    newSum=$(md5sum /tmp/last_internet_path | cut -d' ' -f 1)
    oldSum=$(md5sum /etc/last_internet_path | cut -d' ' -f 1)
    if [ "$newSum" != "$oldSum" ]; then
        echo -n -e "$result" > /etc/last_internet_path
        logger 'Last path to internet updated'
    else 
        logger 'Last path to internet has no changed'
    fi
fi
