#!/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

## -

#ACUÉRDATE DE QUE SON 5, timer, minino-timer, kloink.wav, el .desktop y el icono information.png

case $LANG in
    ca*)
	QUEDAN="Queden"
	PARA="per"
	MINUTOS="minuts"
	EN_MINUTOS="Temps en minuts."
	TEXT1="Missatge recordatori:"
	TEXT2="Recordatori en"
	TEXT3="Temps esgotat"
	IDIOMA="ca"
	TIMER="Temporitzador"
    ;;
    es*)
	QUEDAN="Quedan"
	PARA="para"
	MINUTOS="minutos"
	EN_MINUTOS="Tiempo en minutos."
	TEXT1="Mensaje recordatorio:"
	TEXT2="Recordatorio en"
	TEXT3="Tiempo agotado"
	IDIOMA="es"
	TIMER="Temporizador"
    ;;
    gl*)
	QUEDAN="Restan"
	PARA="para"
	MINUTOS="minutos"
	EN_MINUTOS="Tempo en minutos."
	TEXT1="Mensaxe de recordatorio:"
	TEXT2="Recordatorio en"
	TEXT3="Tempo esgotado"
	IDIOMA="es"
	TIMER="Temporizador"
    ;;
    *)
	QUEDAN="Remaining"
	PARA="to"
	MINUTOS="minutes"
	EN_MINUTOS="Time in minutes."
	TEXT1="Reminder message:"
	TEXT2="Reminder in"
	TEXT3="Time out"
	IDIOMA="en"
	TIMER="Timer"
    ;;
esac



mkdir /tmp/minino-timer 

#Función para el formato de mensajes OSD

function OSD(){
aosd_cat --position=1 --y-offset=30 --back-color=yellow\
	    --fore-color=black --back-opacity=255\
	    --shadow-color=white --padding=20 --fade-in=20\
	    -u 7000 -n "arial 12" 
}

function OSD2(){
aosd_cat --position=1 --y-offset=30 --back-color=black\
	    --fore-color=white --back-opacity=255\
	     --padding=5 --fade-in=20\
	    -u 3000 -n "arial 12" 
}


# Sleep funciona en segundos, así que multiplicamos por 60 para que transcurran minutos
TIEMPO="$1"
INFO="$2"
TIEMPO=$(($TIEMPO*60))

# Si se elige la opción de ... pedir tiempo en minutos
if [ "$TIEMPO" = "0" ];then
    # Si se elige la opción de información
    if [ "$INFO" = "info" ];then
	cd /tmp/minino-timer
	for i in $(ls);do
	    #Borra los archivos ya pasados
	    fin=`echo $i`
	    hora=`date +%s`
	    if (( $fin < $hora ));then
		rm $i
	    fi
	    #segundos_que_quedan=$((`echo $i`-`date +$s`))
	done
	rm /tmp/info-timer
	for i in $(ls);do
	    fin=`echo $i`
	    hora=`date +%s`
	    mensaje=`cat $i`
	    queda=$(($fin-$hora))
	    tantos=$(($queda/60))
	    if [ "$mensaje" = "" ];then
		echo "$QUEDAN $tantos $MINUTOS $PARA: ¡klin!">>/tmp/info-timer
	    else
		echo "$QUEDAN $tantos $MINUTOS $PARA: $mensaje">>/tmp/info-timer
	    fi
	done
	yad --window-icon appointment-soon --title "$TIMER" --text-info --center --width=300 --height=200 --filename /tmp/info-timer
	exit 0
    fi
    TIEMPO=`yad --window-icon appointment-soon --title "$TIMER" --entry --text "$EN_MINUTOS"`
	if [ "$?" = "1" ];then
	    exit 0
	fi

    TIEMPO=$(($TIEMPO*60))
fi

# Pedir el mensaje recordatorio

# Mensaje recordatorio:
MENSAJE=`yad --window-icon appointment-soon --entry --text "$TEXT1" --title "$TIMER"`
if [ "$?" = "1" ];then
    exit 0
fi
# Si no hay mensaje sonará una campana
if [ "$MENSAJE" = "" ];then

    SEGUNDO_EN_QUE_ACABA=$((`date +%s`+$TIEMPO))
    echo "$MENSAJE">/tmp/minino-timer/$SEGUNDO_EN_QUE_ACABA
    # Recordatorio en ...minutos
    echo "$TEXT2 $(($TIEMPO/60)) min" | OSD2 &
	sanduhr -m "$TEXT3" "+${TIEMPO}s" &
	#sanduhr -m "$MENSAJE" "+${TIEMPO}s" &
    sleep $TIEMPO;
    # Tiempo agotado
    echo "$TEXT3" | OSD &
    aplay /usr/local/share/sounds/kloink.wav

else
    SEGUNDO_EN_QUE_ACABA=$((`date +%s`+$TIEMPO))
    echo "$MENSAJE">/tmp/minino-timer/$SEGUNDO_EN_QUE_ACABA
    echo "$TEXT2 $(($TIEMPO/60)) min" | OSD2 &
	#sanduhr -m "$TEXT3" "+${TIEMPO}s" &
	sanduhr -m "$MENSAJE" "+${TIEMPO}s" &
    sleep $TIEMPO;
    echo "$MENSAJE" | OSD &
    mpg123 /usr/local/share/sounds/sound.mp3
    hayinternet=`hayinternet`
    if [ "$hayinternet" = "up" ];then
	#hablanatural "$MENSAJE"
	habla "$MENSAJE"
    else
	espeak -v $IDIOMA "$MENSAJE"
    fi
fi
