#!/bin/sh

action=$1

if [ "$action" = "on" ];then
	result=`curl -H 'glinet: 1' -s -k http://127.0.0.1/rpc -d "{\"jsonrpc\":\"2.0\",\"method\":\"call\",\"params\":[\"\",\"tor\",\"get_config\",{}],\"id\":1}" | jsonfilter -e @.result`
	countries=`echo $result | jsonfilter -e @.countries`
	manual=`echo $result | jsonfilter -e @.manual`
	if [ "$manual" = "true" ];then
		curl -H 'glinet: 1' -s -k http://127.0.0.1/rpc -d "{\"jsonrpc\":\"2.0\",\"method\":\"call\",\"params\":[\"\",\"tor\",\"set_config\",{\"enable\":true,\"manual\":true,\"countries\":$countries}],\"id\":1}"
	else
		curl -H 'glinet: 1' -s -k http://127.0.0.1/rpc -d "{\"jsonrpc\":\"2.0\",\"method\":\"call\",\"params\":[\"\",\"tor\",\"set_config\",{\"enable\":true,\"manual\":false}],\"id\":1}"
	fi
fi

if [ "$action" = "off" ];then
	result=`curl -H 'glinet: 1' -s -k http://127.0.0.1/rpc -d "{\"jsonrpc\":\"2.0\",\"method\":\"call\",\"params\":[\"\",\"tor\",\"get_config\",{}],\"id\":1}" | jsonfilter -e @.result`
	countries=`echo $result | jsonfilter -e @.countries`
	manual=`echo $result | jsonfilter -e @.manual`
	if [ "$manual" = "true" ];then
		curl -H 'glinet: 1' -s -k http://127.0.0.1/rpc -d "{\"jsonrpc\":\"2.0\",\"method\":\"call\",\"params\":[\"\",\"tor\",\"set_config\",{\"enable\":false,\"manual\":true,\"countries\":$countries}],\"id\":1}"
	else
		curl -H 'glinet: 1' -s -k http://127.0.0.1/rpc -d "{\"jsonrpc\":\"2.0\",\"method\":\"call\",\"params\":[\"\",\"tor\",\"set_config\",{\"enable\":false,\"manual\":false}],\"id\":1}"
	fi
fi

sleep 5
