#!/bin/bash
# Command line translator based on google translate engine
# by Monsoft
# (c) 2012 Ascot

# Check if curl is installed
curl -V >/dev/null 2>&1|| { echo "I require curl but it's not installed. Aborting." >&2; exit 1;} 

# Check internet connectivity
curl -v www.google.co.uk 2>&1|grep -m1 "HTTP/1.1" >/dev/null 2>&1|| { echo "I require internet connection. Aborting." >&2; exit 1;}
# Main

if [ $# -ne 3 ]
	then
		echo -e "Command line language translator by Monsoft"
		echo -e "\nAvailable languages:"
		echo -e "---------------------\n"
		curl -s --user-agent "Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.34 (KHTML, like Gecko) QupZilla/1.3.1 Safari/534.34" "http://translate.google.com/m?&mui=sl"|grep -Eo 'sl=[a-z]{2}">[^>]*<'|sed 's/sl=//g;s/">/ -> /g;s/<//g'|tr "\n" ","|awk -F, '{ for ( i=1; i<=NF;i=i+2) print $i "\t\t\t"$(i+1)}'
               	echo -e "\nUsage: `basename $0` From To Sentence"
               	echo -e "------------------------------------"
		echo -e "Example:\n`basename $0` en pl how"
		echo -e "`basename $0` en pl \"How they do it\""

		exit 
fi




FROM=$1
TO=$2
SENTENCE=`echo $3|tr " " "+"`

echo -e "Command line language translator by Monsoft"
echo -e "--------------------------------------------\n"
echo "${FROM} -> ${TO}"
curl -s --user-agent "Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.34 (KHTML, like Gecko) QupZilla/1.3.1 Safari/534.34" "http://translate.google.com/m?hl=en&sl=${FROM}&tl=${TO}&ie=UTF-8&prev=_m&q=${SENTENCE}"|sed -n 's/.*class="t0">//;s/<.*$//p'
echo -e "\n"
