#!/bin/bash

#################################################
# Nombre a lletra
# Versió nombres en anglès
# Fins a 7 dígits
# 2020-08-30
#################################################

nombretot=$1

# Detectar llarg
llarg=${#nombretot}  

#################################################
# Funcions segons nombre de dígits
#################################################

#################################################
# Case unitats
#################################################

funun() {

if [ $nombre == "0" ]
then
nom1="zero"
fi

if [ $nombre == "1" ]
then
nom1="one"
fi

if [ $nombre == "2" ]
then
nom1="two"
fi

if [ $nombre == "3" ]
then
nom1="three"
fi

if [ $nombre == "4" ]
then
nom1="four"
fi

if [ $nombre == "5" ]
then
nom1="five"
fi

if [ $nombre == "6" ]
then
nom1="six" 
fi

if [ $nombre == "7" ]
then
nom1="seven"
fi

if [ $nombre == "8" ]
then
nom1="eight"
fi

if [ $nombre == "9" ]
then
nom1="nine"
fi

}

#################################################
# Case desenes
#################################################

fundos() {

nombreini2=$nombre

if [ $nombreini2 == "10" ]
then
nom2="ten"
fi

if [ $nombreini2 == "11" ]
then
nom2="eleven"
fi
if [ $nombreini2 == "12" ]
then
nom2="twelve"
fi
if [ $nombreini2 == "13" ]
then
nom2="thirteen"
fi
if [ $nombreini2 == "14" ]
then
nom2="fourteen"
fi
if [ $nombreini2 == "15" ]
then
nom2="fifteen"
fi
if [ $nombreini2 == "16" ]
then
nom2="sixteen"
fi
if [ $nombreini2 == "17" ]
then
nom2="seventeen"
fi
if [ $nombreini2 == "18" ]
then
nom2="eighteen"
fi
if [ $nombreini2 == "19" ]
then
nom2="nineteen"
fi
if [ $nombreini2 -ge "20" ]
then
nom2="twenty"
fi
if [ $nombreini2 -ge "30" ]
then
nom2="thirty"
fi
if [ $nombreini2 -ge "40" ]
then
nom2="forty"
fi
if [ $nombreini2 -ge "50" ]
then
nom2="fifty"
fi
if [ $nombreini2 -ge "60" ]
then
nom2="sixty"
fi
if [ $nombreini2 -ge "70" ]
then
nom2="seventy"
fi
if [ $nombreini2 -ge "80" ]
then
nom2="eighty"
fi
if [ $nombreini2 -ge "90" ]
then
nom2="ninety"
fi

if [ ${nombreini2:1:1} -ne "0" ] 
then
if [ $nombreini2 -gt 19 ] && [ $nombreini2 -lt 30 ]
then
nombre=${nombreini2:1:1}
funun
nom2=$nom2"-"$nom1
fi
if [ $nombreini2 -gt 30 ]
then
nombre=${nombreini2:1:1}
funun
nom2=$nom2"-"$nom1
fi
fi

if [ ${nombreini2:0:1} == "0" ]
then
nombre=${nombreini2:1:1}
funun
nom2=$nom1
fi

if [ ${nombreini2:0:2} == "00" ]
then
nom2=""
fi


}

#################################################
# Case tres dígits
#################################################

funtres() {

nombreini3=$nombre

if [ ${nombreini3:0:1} -ne "0" ] 
then
nombre=${nombreini3:1:2}
fundos
nombre=${nombreini3:0:1}
funun
nom3=$nom1" "hundred" "$nom2
fi

if [ ${nombreini3:0:1} == "0" ] && [ ${nombreini3:1:1} -ne "0" ] 
then
nombre=${nombreini3:1:2}
fundos
nom3=$nom2
fi

if [ ${nombreini3:0:2} == "00" ] && [ ${nombreini3:2:1} -ne "0" ] 
then
nombre=${nombreini3:2:1}
funun
nom3=$nom1
fi

if [ ${nombreini3:0:3} == "000" ]
then
nom3=""
fi

}

#################################################
# Case quatre dígits
#################################################

funquatre() {

nombreini4=$nombre


if [ ${nombreini4:0:1} -ne "0" ] 
then
echo "here1"
nombre=${nombreini4:1:3}
funtres
nombre=${nombreini4:0:1}
funun
nom4=$nom1" "thousand" "$nom3
fi

if [ ${nombreini4:0:1} == "0" ] && [ ${nombreini4:1:1} -ne "0" ] 
then
nombre=${nombreini4:1:3}
funtres
nom4=$nom3
fi

if [ ${nombreini4:0:2} == "00" ] && [ ${nombreini4:2:1} -ne "0" ] 
then
nombre=${nombreini4:2:2}
fundos
nom4=$nom2
fi

if [ $nombreini4 == "0000" ]
then
nom4=""
fi


}

#################################################
# Case cinc dígits
#################################################

funcinc() {

nombreini5=$nombre

if [ ${nombreini5:2:3} == "000" ] && [ ${nombreini5:0:2} -ne "00" ] 
then
nombre=${nombreini5:0:2}
fundos
nom5=$nom2" "thousand
fi

if [ ${nombreini5:2:3} -ne "000" ]
then
nombre=${nombreini5:2:3}
funtres
nombre=${nombreini5:0:2}
fundos
nom5=$nom2" "thousand" "$nom3
fi

if [ $nombreini5 == "00000" ]
then
nom5=""
fi

}

#################################################
# Case sis dígits
#################################################

funsis() {

nombreini6=$nombre

if [ ${nombreini6:3:3} == "000" ] && [ ${nombreini6:0:3} -ne "000" ]
then
nombre=${nombreini6:0:3}
funtres
nom6=$nom3" "thousand
fi

if [ ${nombreini6:3:3} -ne "000" ] && [ ${nombreini6:0:3} -ne "001" ] 
then
nombre=${nombreini6:0:3}
funtres
nom3ini1=$nom3
nombre=${nombreini6:3:3}
funtres
nom3fin1=$nom3
nom6=$nom3ini1" "thousand" "$nom3fin1
fi

if [ ${nombreini6:3:3} -ne "000" ] && [ ${nombreini6:0:3} == "001" ]
then
nombre=${nombreini6:3:3}
funtres
nom6="thousand"" "$nom3
fi

if [ $nombreini6 == "000000" ]
then
nom6=""
fi


}

#################################################
# Case set dígits
#################################################

funset() {

nombreini7=$nombre

if [ ${nombreini7:0:1} == "1" ]
then
nombre=${nombreini7:1:6}
funsis
nom7="one million"" "$nom6
fi

if [ ${nombreini7:0:1} -gt "1" ]
then
nombre=${nombreini7:1:6}
funsis
nombre=${nombreini7:0:1}
funun
nom7=$nom1" "million" "$nom6
fi

}

#################################################
# Cases i crida de funcions
#################################################


# Case un dígit
if [ $llarg == 1 ]
then
nombre=$nombretot
funun
nom=$nom1
fi

# Case dos dígits
if [ $llarg == 2 ]
then
nombre=$nombretot
fundos
nom=$nom2
fi

# Case tres dígits
if [ $llarg == 3 ]
then
nombre=$nombretot
funtres
nom=$nom3
fi

# Case quatre dígits
if [ $llarg == 4 ]
then
nombre=$nombretot
funquatre
nom=$nom4
fi

# Case cinc dígits
if [ $llarg == 5 ]
then
nombre=$nombretot
funcinc
nom=$nom5
fi

# Case sis dígits
if [ $llarg == 6 ]
then
nombre=$nombretot
funsis
nom=$nom6
fi

# Case set dígits
if [ $llarg == 7 ]
then
nombre=$nombretot
funset
nom=$nom7
fi

#################################################
# Resultat
#################################################

#echo $nombre
#echo $llarg
echo $nom
echo $nombretot " = " $nom >> out.txt
echo $nom | festival --language english --tts

