#!/bin/bash
# TalkingIntro
# Voice playback when my operating system starts up
# 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 the weather: weather-go (example of a basic command: weather-go -hide-icon -l "Cherbourg, France") AND the script TalkingWeather
# - for translation into your language (by default use the system language): crow-translate (example of a basic command: crow -b text)
# - for the fortune files (in french): fortune-mod-fr fortune-tontons-flingueurs-fr AND the script TalkingFortune

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

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

# Welcome message
# Requires the installation (in repository) of: gspeech pico2wave-shell pico-tts
# 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
Message="Bienvenue sur Manjaro Linux Serge ! Je te souhaite une excellente expérience sur notre distribution!"

echo "TalkingIntro: Voice playback when my operating system starts up v."$twvers
echo "help: use the -h parameter"
echo "Selected parameters: TalkingIntro "$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 weather-go fortune-mod-fr crow-translate"
echo "(fortune-mod-fr can be replaced by another fortune package, script to be adapted in this case)"
echo "Launching:"
echo "TalkingIntro (without parameter)"
echo "TalkingIntro 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"


# 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 -b -t "${Lang%%-*}" $Message)
fi
echo $Message > "$Rep"/Message.txt
echo "$Rep"/Message.txt

# Voice Message 
gspeech-cli  -l $Lang -f /"$Rep"/Message.txt -o speech.wav && aplay speech.wav

#  A little joke
source "$Rep"/TalkingFortune $Lang

#  We continue with the time
source "$Rep"/TalkingClock $Lang

# Test
# notify-send -i yast_joystick -t 80000 "Test" $Rep
# For a console test :
# gspeech-cli  -i "test"  -o speech.wav && aplay speech.wav
