#!/bin/sh
#
# Opennet Firmware
# 
# Copyright 2010 Rene Ejury <opennet@absorb.it>
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
# 	http://www.apache.org/licenses/LICENSE-2.0
# 


print_status()
{
	autosearch=$(uci -q get on-openvpn.gateways.autosearch);
	output="\nautosearch: $autosearch";
	if [ "$autosearch" == "on" ]; then
		sort_by_etx=$(uci -q get on-openvpn.gateways.vpn_sort_criteria);
		output="$output\tsort by: $sort_by_etx"
	fi

	echo -e "$output"
	echo -e "\nno.\tactive\tIP\t\tDist/ETX\tVPN\tAge"
	echo '/route' | nc localhost 2006 > /tmp/olsrd_txtinfo_vpnstatus.txt
	current_gw_ipaddr=$(uci -q get openvpn.opennet_user.remote)
	index=0;
	while [ -n "$(uci -q show on-openvpn.@gateway[$index])" ] ; do
		gw_ipaddr=$(uci -q get on-openvpn.@gateway[$index].ipaddr);
		age=$(uci -q get on-openvpn.@gateway[$index].age);
		status=$(uci -q get on-openvpn.@gateway[$index].status);
		active=""; if [ "$gw_ipaddr" == "$current_gw_ipaddr" ]; then active="*"; fi;
		dist=$(awk 'BEGIN {FS="[/\x09]+"} $1=="'$gw_ipaddr'" {print $4"/"$5;}' /tmp/olsrd_txtinfo_vpnstatus.txt);
		if [ ${#dist} -lt 8 ]; then dist="$dist\t"; fi
		: $((index++))
		echo -e "$index\t$active\t$gw_ipaddr\t$dist\t$status\t$age"
	done
	echo

	rm /tmp/olsrd_txtinfo_vpnstatus.txt
}

print_usage()
{
	echo
	echo "*** vpn_status supports the following options: ***"
	echo "toggle_auto    - will toggle the autosearch mode"
	echo "toggle_etx     - will toggle sort by etx or hops"
	echo "add_gw \$IP     - will expect IP address as param"
	echo "del_gw \$POS    - will expect number of gateway"
	echo "move_top \$POS  - will expect number of gateway"
}


move_top()
{
	uci -q set on-openvpn.gateways.autosearch="off"
	position=$1
	if [ "$(uci -q show on-openvpn.@gateway[$position])" ] ; then
		uci -q reorder on-openvpn.@gateway[$position]=0;
		index=0;
		while [ -n "$(uci -q show on-openvpn.@gateway[$index])" ] ; do
			uci -q rename on-openvpn.@gateway[$index]="gate_"$((++index));
		done
		uci commit on-openvpn;							# commit after every change to prevent uci memory problems
	fi
}

add_gw()
{
	uci -q set on-openvpn.gateways.autosearch="off"
	ipaddr=$1
	index=0;
	while [ -n "$(uci -q show on-openvpn.@gateway[$index])" ] ; do : $((index++)); done
	: $((index++))
	uci -q set on-openvpn.gate_${index}=gateway;
	uci -q set on-openvpn.gate_${index}.ipaddr=$ipaddr;
	uci commit on-openvpn;							# commit after every change to prevent uci memory problems
}

del_gw()
{
	uci -q set on-openvpn.gateways.autosearch="off"
	position=$1
	if [ "$(uci -q show on-openvpn.@gateway[$position])" ] ; then
		uci -q del on-openvpn.@gateway[$position];
		index=0;
		while [ -n "$(uci -q show on-openvpn.@gateway[$index])" ] ; do
			uci -q rename on-openvpn.@gateway[$index]="gate_"$((++index));
		done
		uci commit on-openvpn;							# commit after every change to prevent uci memory problems
	else echo "postion $position not valid"
	fi
}

toggle_etx()
{
	sort_by_etx=$(uci -q get on-openvpn.gateways.vpn_sort_criteria);
	if [ "$sort_by_etx" == "etx" ]; then sort_by_etx="metric";
	else sort_by_etx="etx"; fi
	uci -q set on-openvpn.gateways.vpn_sort_criteria=$sort_by_etx;
	uci commit on-openvpn;							# commit after every change to prevent uci memory problems
}

toggle_auto()
{
	autosearch=$(uci -q get on-openvpn.gateways.autosearch);
	if [ "$autosearch" == "on" ]; then autosearch="off";
	else autosearch="on"; fi
	uci -q set on-openvpn.gateways.autosearch=$autosearch;
	uci commit on-openvpn;							# commit after every change to prevent uci memory problems
}

param2=$2
case "$1" in
	"move_top")
		if [ -n "$param2" ]; then
			move_top $((param2-1));
		else echo "ERROR: move_top requires two parameters"
		fi
		print_status;
	;;
	"add_gw")
		if [ -n "$param2" ]; then
			add_gw $param2;
		else echo "ERROR: add_gw requires two parameters"
		fi
		print_status;
	;;
	"del_gw")
		if [ -n "$param2" ]; then
			del_gw $((param2-1));
		else echo "ERROR: del_gw requires two parameters"
		fi
		print_status;
	;;
	"toggle_etx")
		toggle_etx;print_status;
	;;
	"toggle_auto")
		toggle_auto;print_status;
	;;
	*)
		print_status;print_usage;
	;;
esac






