#!/bin/bash
# TalkingFortune
# Voice playback one or more Fortune files
# 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 fortune files (in french): fortune-mod-fr fortune-tontons-flingueurs-fr
# - for translation into your language (by default use the system language): crow-translate (example of a basic command: crow -b text)

# Specific to the use of the "fortune-mod-fr" package (texts in French to be translated into other languages - not sure they will make you laugh :)) notably because humor is not perceived the same way from one country to another, and depends so much on your own life experience
# Sorry if this does not suit you. You can adapt this script quite easily to use one of the many other texts in the fortune packages. There is something for everyone.

# Parameters:
# Required: either 1 parameter (language) OR none (default="fr-FR")
# TalkingFortune $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 "TalkingFortune: Voice playback one or more Fortune files v."$twvers
echo "help: use the -h parameter"
echo "Selected parameters: TalkingFortune "$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 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 "TalkingFortune (without parameter)"
echo "TalkingFortune 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"

# Eventual compilation of "Fortune" files
# All these files belong to the "fortune-mod-fr" package and are written in French
# Prov=$(fortune proverbes)
# Prov=$(fortune sciences)
# Prov=$(fortune metal-fairy-tale-fr)
# Prov=$(fortune tontonsflingueurs)
# Prov=$(fortune mysoginie)
Prov=$(fortune humour)

# Removal of characters & line breaks that interfere with the pronunciation of the text
Supp='-+-'
Prov=$(echo $Prov | sed 's/'$Supp'//g')
Prov=$(echo $Prov | sed 's/\n/ /g')

# To be adapted below according to your language and the selected fortune package
Prov="Citation du jour : "$Prov
echo $Prov > "$Rep"/fortuneoftheday.txt

# Specific to the use of the "fortune-mod-fr" package: texts in French to be Translate into the desired language (via Google (default), Bing or Yandex) ------------------------------------------------------------------------------------
if [ $Lang != "fr-FR" ]; then
# Google's language specification is only 2 characters long: Lang="en-GB"=>"en"
Prov=$(crow -e $TranslationEngine -b -t "${Lang%%-*}" $Prov)
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/emotes/face-laugh.png -t 80000 "Talking Fortune" "$Prov"
echo $Prov > "$Rep"/translatedfortuneoftheday.txt 
gspeech-cli  -l $Lang -f "$Rep"/translatedfortuneoftheday.txt -o speech.wav && aplay speech.wav

