#!/bin/bash

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>

# (c) Antonio Sánchez León <introlinux[fix@]gmail[fix.]com>
# (c) Miguel Anxo Bouzada <mbouzada[fix@]gmail[fix.]com>
# (c) GALPon MinNo Team <minino[fix@]galpon[fix.]org>
# july 2012

## -

killall simple_google_tts
killall pico2wave
killall play

case $LANG in
    ca*)
	IDIOMA="ca"
	IDIOMA2="es"
	IDIOMA3="spa"
    ;;
    es*)
	IDIOMA="es"
	IDIOMA2="es"
	IDIOMA3="spa"
    ;;
    gl*)
	IDIOMA="es"
	IDIOMA2="es"
	IDIOMA3="spa"
    ;;
    *)
	IDIOMA="en"
	IDIOMA2="en"
	IDIOMA3="eng"
    ;;
esac

ARCHIVO="$1"
SELECCION="`xclip -o`"


# Si se ha hecho clic en el icono, pues no se ha arrastrado nada sobre él, y además, no hay nada seleccionado,
# se captura una porción de la pantalla y se hace un OCR al vuelo
if [ "$1" = "" ];then

	if [ "$SELECCION" = "" ];then  # si no hay nada seleccionado se para lo que se esté leyendo
		cd /tmp
		if [ -e leyendo ];then
			echo "Parando lectura"
			killall simple_google_tts
			killall play
			killall pico2wave
			rm leyendo
			exit 0
		fi
		# Si hay algo seleccionado
		rm captura.png captura2.png captura2.txt leyendo
		touch leyendo
		minino-habla "Selección de lectura"
		scrot -q 100 -s captura.png
		gmic captura.png -gimp_normalize_local 60,64,40,12.04,0,11,0 -o captura2.png
		cuneiform -f text -l $IDIOMA3 -o captura2.txt captura2.png
		cat captura2.txt |xclip -selection clipboard
		minino-read-selection /tmp/captura2.txt
		rm leyendo
	fi
fi
	

# Si hay internet se usa el motor text2speech de google, si no, se usa pico2wave
hayinternet=`hayinternet`
if [ "$hayinternet" = "up" ];then

		if [ "$ARCHIVO" = "" ];then  # No hay archivo, hay algo seleccionado para leer

			#Intentamos averiguar el idioma del texto
			IDIOM=`echo "$SELECCION"|text_cat|awk '{print $1}'`
			if [ "$IDIOM" = "spanish" ];then
					IDIOMA="es"
			else
					IDIOMA="en"
			fi

    	    simple_google_tts -p "$IDIOMA"
	    	#simple_google_tts "$IDIOMA"
		else # Hay un archivo para leer

			#Intentamos averiguar el idioma del texto
			IDIOM=`cat "$ARCHIVO"|text_cat|awk '{print $1}'`
			if [ "$IDIOM" = "spanish" ];then
					IDIOMA="es"
			else
					IDIOMA="en"
			fi
		# Google bloquea cada dos por tres, por eso he comentado el motor de google
			simple_google_tts -p "$IDIOMA" "$ARCHIVO" 
	    	#simple_google_tts "$IDIOMA" "$ARCHIVO"
		fi
else
		#espeak -v $IDIOMA "$MENSAJE"
		if [ "$ARCHIVO" = "" ];then
		    simple_google_tts -p "$IDIOMA2"
		else
		    simple_google_tts -p "$IDIOMA2" "$ARCHIVO"
		fi
fi

