#!/bin/bash
# TalkingClock
# Talking clock at any time 
# Author  : Serge Le Tyrant
# License : CC-BY

twvers="1.0"

# Requires the installation (in repository) of:
# - for the voice message: gspeech pico2wave-shell pico-tts
# - for translation into your language (by default use the system language): crow-translate (example of a basic command: crow -b text)

# Parameters:
# Required: either 1 parameter (language) OR none (default="fr-FR")
# TalkingClock $Lang

# Supported languages : 
# Lang="en-GB"
# Lang="en-US"
# Lang="de-DE"
# Lang="it-IT"
# Lang="fr-FR" (default)
DefaultLang="fr-FR"
Lang=$DefaultLang

TranslationEngine="libretranslate"
# TranslationEngine="lingva"
# TranslationEngine="yandex"
# TranslationEngine="bing"
# TranslationEngine="google"

echo "TalkingClock: Talking clock v."$twvers
echo "help: use the -h parameter"
echo "Selected parameters: TalkingClock "$1

# Checking the input parameters (requested language, help):

if [[ $# -eq 1 ]] && [[ $1 = "-h" ]]; then
echo "It requires the installation (in Arch|AUR repositories) of: gspeech pico2wave-shell pico-tts crow-translate"
echo "Launching:"
echo "TalkingClock (without parameter)"
echo "TalkingClock Lang"
echo "Lang: en-GB, en-US, de-DE, it-IT, fr-FR (default)"
exit

elif [[ $# -eq 1 ]]; then
Lang=$1

fi

# Working directory
Rep=$(cd $( dirname ${BASH_SOURCE[0]} ) && pwd )
if [[ -z "$Rep" ]]; then
Rep=$(pwd)
fi
#Rep=$(echo $Rep | sed 's/ /\\ /g') 
# cd "$Rep" should work but does not
# WARNING: Quotation marks are required to designate this variable so that this script works when launched from a path containing spaces
cd "$Rep"

play /usr/share/sounds/Smooth/stereo/_desktop-login.oga

# the bc command allows to do numerical calculation under bash (allows here to remove the superfluous '0' to avoid pronouncing them)
Hour=$(date +%H | bc)":"
Minute=$(date +%M)

PhoneticHour=${Hour//:/\ heure}
PhoneticMinute=$(date +%M | bc | sed 's/0//g')

# Special case for 21h (mispronounced in French: vingt-et-une-heure vs vingt-et-un-heure), 0h for minuit, une vs un, etc.. (sorry for the spelling, priority to the pronunciation):
if [[ "$PhoneticHour" == "21 heure" ]]; then
PhoneticHour="vingté une heure"
elif [[ "$PhoneticHour" == "0 heure" ]]; then
PhoneticHour="minuit"
fi

if [[ "$PhoneticMinute" == "1" ]]; then
PhoneticMinute="une"
elif [[ "$PhoneticMinute" == "21" ]]; then
PhoneticMinute="vingté-une"
elif [[ "$PhoneticMinute" == "31" ]]; then
PhoneticMinute="trenté-une"
elif [[ "$PhoneticMinute" == "41" ]]; then
PhoneticMinute="quaranté-une"
elif [[ "$PhoneticMinute" == "51" ]]; then
PhoneticMinute="cinquanté-une"
fi

# The sed command to remove the "00" on the minutes is needed to pronounce "15h" instead of "15h0
#HeureMinute=$(echo "Il est $Heure $Minute" | sed 's/Il est 0/Il est /g')
HourMinute="Il est $Hour$Minute"
PhoneticHourMinute="Il est $PhoneticHour $PhoneticMinute"


# Translation into the desired language via the selected engine ------------------------------------------------------------------------------------
if [ $Lang != "fr-FR" ]; then
# Engine's language specification for translation is only 2 characters long: Lang="en-GB"=>"en"
HourMinute=$(crow -e $TranslationEngine -b -t "${Lang%%-*}" $HourMinute)
PhoneticHourMinute=$HourMinute
fi

# Reading of the message
# Note: gspeech-cli does not support reading a variable containing a complete sentence (in this case it reads only the first word), but it does support a file containing the same message
notify-send -i /usr/share/icons/gnome/48x48/status/stock_appointment-reminder.png -t 80000 "Talking Clock" "$HourMinute"
gspeech-cli  -l $Lang -i "$PhoneticHourMinute" -o speech.wav && aplay speech.wav

# We end with the greeting
source "$Rep"/TalkingHaveANiceDay $Lang

# Request for short weather forecast (otherwise parameter = false)
source "$Rep"/TalkingWeather $Lang true
