#!/bin/bash

#################################################
# Nombre a lletra
# Versió nombre en català
# 2020-08-28
#################################################

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="un"
fi

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

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

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

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

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

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

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

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

}

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

fundos() {

nombreini2=$nombre

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

if [ $nombreini2 == "11" ]
then
nom2="onze"
fi
if [ $nombreini2 == "12" ]
then
nom2="dotze"
fi
if [ $nombreini2 == "13" ]
then
nom2="tretze"
fi
if [ $nombreini2 == "14" ]
then
nom2="catorze"
fi
if [ $nombreini2 == "15" ]
then
nom2="quinze"
fi
if [ $nombreini2 == "16" ]
then
nom2="setze"
fi
if [ $nombreini2 == "17" ]
then
nom2="disset"
fi
if [ $nombreini2 == "18" ]
then
nom2="divuit"
fi
if [ $nombreini2 == "19" ]
then
nom2="dinou"
fi
if [ $nombreini2 -ge "20" ]
then
nom2="vint"
fi
if [ $nombreini2 -ge "30" ]
then
nom2="trenta"
fi
if [ $nombreini2 -ge "40" ]
then
nom2="quaranta"
fi
if [ $nombreini2 -ge "50" ]
then
nom2="cinquanta"
fi
if [ $nombreini2 -ge "60" ]
then
nom2="seixanta"
fi
if [ $nombreini2 -ge "70" ]
then
nom2="setanta"
fi
if [ $nombreini2 -ge "80" ]
then
nom2="vuitanta"
fi
if [ $nombreini2 -ge "90" ]
then
nom2="noranta"
fi

if [ ${nombreini2:1:1} -ne "0" ] 
then
if [ $nombreini2 -gt 19 ] && [ $nombreini2 -lt 30 ]
then
nombre=${nombreini2:1:1}
funun
nom2=$nom2"-i-"$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

}

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

funtres() {

nombreini3=$nombre

if [ $nombreini3 -ge "100" ]
then
nom3="cent"
fi
if [ $nombreini3 -ge "200" ]
then
nom3="dos-cents"
fi
if [ $nombreini3 -ge "300" ]
then
nom3="tres-cents"
fi
if [ $nombreini3 -ge "400" ]
then
nom3="quatre-cents"
fi
if [ $nombreini3 -ge "500" ]
then
nom3="cinc-cents"
fi
if [ $nombreini3 -ge "600" ]
then
nom3="sis-cents"
fi
if [ $nombreini3 -ge "700" ]
then
nom3="set-cents"
fi
if [ $nombreini3 -ge "800" ]
then
nom3="vuit-cents"
fi
if [ $nombreini3 -ge "900" ]
then
nom3="nou-cents"
fi

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

if [ ${nombreini3:0:1} -ne "0" ] && [ ${nombreini3:1:1} -ne "0" ] && [ ${nombreini3:1:2} -ne "00" ]
then
nombre=${nombreini3:1:2}
fundos
nom3=$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:1} == "0" ] && [ ${nombreini3:1:1} -ne "0" ]
then
nombre=${nombreini3:1:2}
fundos
nom3=$nom2
fi

}

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

funquatre() {

nombreini4=$nombre

if [ ${nombreini4:1:3} == "000" ] && [ ${nombreini4:0:1} == "1" ] 
then
nom4="mil"
fi

if [ ${nombreini4:1:3} == "000" ] && [ ${nombreini4:0:1} -gt "1" ] 
then
nombre=${nombreini4:0:1}
funun
nom4=$nom1" "mil
fi

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

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

}

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

funcinc() {

nombreini5=$nombre

if [ ${nombreini5:2:3} == "000" ]
then
nombre=${nombreini5:0:2}
fundos
nom5=$nom2" "mil
fi

if [ ${nombreini5:2:3} -ne "000" ]
then
nombre=${nombreini5:2:3}
funtres
nombre=${nombreini5:0:2}
fundos
nom5=$nom2" "mil" "$nom3
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" "mil
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" "mil" "$nom3fin1
fi

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

}

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

funset() {

nombreini7=$nombre

if [ ${nombreini7:0:1} == "1" ]
then
nombre=${nombreini7:1:6}
funsis
nom7="un milió"" "$nom6
fi

if [ ${nombreini7:0:1} -gt "1" ]
then
nombre=${nombreini7:1:6}
funsis
nombre=${nombreini7:0:1}
funun
nom7=$nom1" "milions" "$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 catalan --tts


