#!/bin/bash
# TalkingInk
# Voice playback of your printer's ink level (if supported)
# Author  : Serge Le Tyrant
# License : CC-BY

twvers="1.2"

# Requires the installation (in repository) of:
# - for the voice message: <del>gspeech</del> python-gtts 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 "ink" package : ink
# Supported printers : https://libinklevel.sourceforge.net/#supported

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

# 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 "TalkingInk: Voice playback of your printer's ink level (if supported) v."$twvers
echo "help: use the -h parameter"
echo "Selected parameters: TalkingInk "$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 ink"
echo "(fortune-mod-fr can be replaced by another fortune package, script to be adapted in this case)"
echo "Launching:"
echo "TalkingInk (without parameter)"
echo "TalkingInk 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"

#echo "Info imprimante" > encre.txt
#ink -p usb >> encre.txt

Prov=$(ink -p usb)
echo $Prov > "$Rep"/ink.txt

# Removal of texts that interfere with the pronunciation of the text
Supp='ink 0.5.3 (c) 2018 Markus Heinz EPSON Epson Stylus SX235'
sed -i -e "s|$Supp||g"  "$Rep"/ink.txt
#sed -i -e "s|EPSON Epson Stylus SX235||g"  "$Rep"/ink.txt

sed -i -e "s|Photoblack|Black|g"  "$Rep"/ink.txt
sed -i -e "s|%|%, |g"  "$Rep"/ink.txt
Prov=$(cat $Rep"/ink.txt")

# Translation of the 'Ink' command message into the user's language
Prov=$(crow -e $TranslationEngine -b -t "${Lang%%-*}" $Prov)

# To be adapted below according to your language and the selected fortune package
Prov="Info imprimante : "$Prov
echo $Prov > "$Rep"/ink.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/devices/printer.png -t 80000 "TalkingInk" "$Prov"
#gspeech-cli  -l $Lang -f "$Rep"/ink.txt -o speech.wav && aplay speech.wav
gtts-cli  -l $Lang -f "$Rep"/ink.txt -o speech.mp3 && mpg123 speech.mp3

mpg123 '/mnt/Svg2/Audio/Effets_sonores/mp3/Battle City Dendy-freeringtonesdownload.info.mp3'
 
