#!/bin/bash
# TalkingHaveANiceDay
# Politeness according to the time of day
# 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")
# TalkingHaveANiceDay $Lang

# To be adapted according to your language
Morning="Bonne matinée"
Lunchtime="Bonne appétit !"
Afternoon="Bonne après-midi !"
Evening="Bonne soirée !"

# 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 "TalkingHaveANiceDay: Politeness according to the time of day v."$twvers
echo "help: use the -h parameter"
echo "Selected parameters: TalkingHaveANiceDay "$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 "TalkingHaveANiceDay (without parameter)"
echo "TalkingHaveANiceDay 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"

   currenttime=$(date +%H:%M)
   if [[ "$currenttime" > "00:00" ]] && [[ "$currenttime" < "11:55" ]]; then
     Message=$Morning
   elif [[ "$currenttime" > "11:55" ]] && [[ "$currenttime" < "13:00" ]]; then
     Message=$Lunchtime
   elif [[ "$currenttime" > "12:55" ]] && [[ "$currenttime" < "18:00" ]]; then
     Message=$Afternoon
   else
     Message=$Evening
   fi

# Translation into the desired language (via Google (default), Bing or Yandex) ------------------------------------------------------------------------------------
if [ $Lang != $DefaultLang ]; then
# Google's language specification is only 2 characters long: Lang="en-GB"=>"en"
Message=$(crow -e $TranslationEngine -b -t "${Lang%%-*}" $Message)
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
echo $Message > "$Rep"/Message.txt
gspeech-cli -l $Lang -s 0.9 -f "$Rep"/Message.txt -o speech.wav && aplay speech.wav
