#!/usr/bin/bash
function check_dialog()
{
    if ! dialog &> /dev/null
    then
        echo "ERROR: dialog binary not found."
        exit 1
    fi
}
function check_database()
{
    if ! mysql -uroot $MYSQLAUTH asterisk -e "show tables" &> /dev/null
    then
        echo "ERROR: Cannot connect to Datbase."
        exit 1
    fi
}
function logo()
{
  dialog --stdout --sleep 1 --backtitle "$BACKTITLE" \
         --infobox " O @ @\n @ @ O\n @ O O\n   O\nIssabel" \
        7 11
}

function set_lang_variables() {
#Set *non standard* GUI languages
case $selLang in
    pt_BR)
        webLang="br"
        fopLang="pt_BR"
        ipbxLang="pt_BR"
    ;;
    pr)
        webLang="fa"
        fopLang="en"
        ipbxLang="en_US"
    ;;
    fr)
        fopLang="fr_FR"
        webLang=$selLang
        ipbxLang="fr_FR"
    ;;
    en)
        webLang="en"
        fopLang="en"
        ipbxLang="en_US"
    ;;
    es)
        webLang="es"
        fopLang="es"
        ipbxLang="es_ES"
    ;;
    *)
        webLang=$selLang
        fopLang=$selLang
        ipbxLang=$selLang
    ;;
esac
}

function set_language()
{
if [ -f /var/www/db/settings.db ]; then
(
  sqlite3 /var/www/db/settings.db "update settings set value='$webLang' where key='language'"

  if [ -f /usr/local/fop2/fop2settings.db ]
  then
      sqlite3 /usr/local/fop2/fop2settings.db "update setup set value=\"'$fopLang'\" WHERE extension='SETTINGS' and context='GENERAL' and parameter='language'";
  fi
  mysql -uroot $MYSQLAUTH asterisk -e "REPLACE INTO sipsettings(keyword,data) VALUES('sip_language','$selLang');" &> /dev/null
  mysql -uroot $MYSQLAUTH asterisk -e "REPLACE INTO iaxsettings(keyword,data) VALUES('iax_language','$selLang');" &> /dev/null
  mysql -uroot $MYSQLAUTH asterisk -e "REPLACE INTO pjsipsettings(keyword,data) VALUES('sip_language','$selLang');" &> /dev/null
  mysql -uroot $MYSQLAUTH asterisk -e "REPLACE INTO issabelpbx_settings(keyword,value,type,defaultval,category,name,description) VALUES('LANGUAGE','$ipbxLang','text','en_EN','GUI Behavior','Language','General Language Setting for IssabelPBX');" &> /dev/null

  #DAHDI
  DAHDI_CFG="/etc/asterisk/chan_dahdi.conf"
  if grep -q "language=" $DAHDI_CFG
  then
      sed -i "s/^language=.*/language=$selLang/g" $DAHDI_CFG
  else
      sed -i "/^\[channels\]/a language=$selLang" $DAHDI_CFG
  fi
  sed -i "s/.*defaultlanguage.*/defaultlanguage=$selLang/" /etc/asterisk/asterisk.conf
) &> /dev/null
else
      write_selection
fi
}

function reload_amportal() {

which amportal &>/dev/null
if [ $? -eq 0 ]; then
logo
if ! amportal a r &> /dev/null
then
    clear
    echo "ERROR: Cannot apply changes."
    exit 3
else
    clear
fi
fi
}

function write_selection() {
    echo "webLang=$webLang" >/tmp/ipbx.lang
    echo "fopLang=$fopLang" >>/tmp/ipbx.lang
    echo "ipbxLang=$ipbxLang" >>/tmp/ipbx.lang
    echo "selLang=$selLang" >>/tmp/ipbx.lang
}

# Loads mysql root password
source /etc/issabel.conf

if [ -z $mysqlrootpwd ]
then
	MYSQLAUTH=""
else
	MYSQLAUTH="-p$mysqlrootpwd"

fi


# Start MAIN section

if [ $# -eq 0 ];
then

# No argument supplied, check for /tmp/ipbx.lang 

if [ ! -f /tmp/ipbx.lang ]; then

check_dialog

if [ -f /var/www/db/settings.db ]; then
    check_database
    currentLang=$(sqlite3 /var/www/db/settings.db "select value from settings where key='language'")
else
    currentLang=""
fi

langselected_en=on
langselected_es=off
langselected_br=off
langselected_fr=off
langselected_fa=off

if [ "x$currentLang" != "x" ]; then
    langselected_en=off
    declare langselected_${currentLang}=on
fi

selLang=$((dialog --backtitle "Issabel" --no-tags \
--radiolist "Select PBX language:" 15 40 10 \
 en "English" $langselected_en \
 es "Spanish" $langselected_es \
 pt_BR "Portuguese(BR)" $langselected_br \
 fr "French" $langselected_fr \
 pr "Persian" $langselected_fa \
 > /dev/tty) 2>&1)
if [ -z $selLang ]
then
    #No language selected
    exit 2
fi

set_lang_variables

else
   # this sets languages as set_lang_variables based on selection, but from saved lang file
   set -o allexport && source /tmp/ipbx.lang && set +o allexport
   rm -f /tmp/ipbx.lang
fi

else
    # language argument passed, if valid just apply changes
    valid_lang="es en fr pt_BR pr"
    selLang=$1
    if [[ $valid_lang =~ (^|[[:space:]])$selLang($|[[:space:]]) ]]; then
	set_lang_variables
    else
	exit 1
    fi

fi

# actually set the language and reloads
set_language
reload_amportal
