#!/bin/bash

if [ -e /etc/amportal.conf ]; then

DBNAME=`cat /etc/amportal.conf | sed 's/ //g' | grep ^AMPDBNAME | cut -d= -f2 | tail -n1`
DBUSER=`cat /etc/amportal.conf | sed 's/ //g' | grep ^AMPDBUSER | cut -d= -f2 | tail -n1`
DBPASSLINE=`cat /etc/amportal.conf | grep ^AMPDBPASS | tail -n1`
DBSTRIP=`echo $DBPASSLINE | cut -d= -f1`
DBPASS=`echo $DBPASSLINE | sed "s/$DBSTRIP=//g"`
DBHOST=`cat /etc/amportal.conf | sed 's/ //g' | grep ^AMPDBHOST | cut -d= -f2 | tail -n1`
AMPEXTENSIONS=`cat /etc/amportal.conf | sed 's/ //g' | grep ^AMPEXTENSION | cut -d= -f2 | tail -n1`

hash_insert () {
local name=$1 key=$2 val=$3
eval __hash_${name}_${key}=$val
}
hash_find () {
local name=$1 key=$2
local var=__hash_${name}_${key}
MICLAVE=${!var}
}

eval `cat /etc/asterisk/voicemail.conf | grep -v "^\;" | grep "=>" | cut -d, -f1 | sed 's/ //g' | sed  's/\([^=]*\)=>\(.*\)/hash_insert claves "\1" "\2";/g'`

# Verify if the fop2 plugin table exists
FOP2PLUGIN=0
while read line; do
let FOP2PLUGIN=FOP2PLUGIN+1
done < <( mysql -NB -u $DBUSER -p$DBPASS -h $DBHOST $DBNAME -e "SHOW tables FROM \`$DBNAME\` LIKE 'fop2users'" )

if [ "$FOP2PLUGIN" -gt 0 ]; then

mysql -NB --raw -u $DBUSER -p$DBPASS -h $DBHOST $DBNAME -e "SELECT value FROM fop2settings" | while read LINEA
do
echo $LINEA
done

mysql -NB -u $DBUSER -p$DBPASS -h $DBHOST $DBNAME -e "set @@group_concat_max_len=32768; SELECT CONCAT('perm=',fop2permissions.name,':',permissions,':',IF(isnull(GROUP_CONCAT(device)),'',GROUP_CONCAT(device))) FROM fop2permissions LEFT JOIN fop2PermGroup on fop2permissions.name=fop2PermGroup.name LEFT JOIN fop2GroupButton on fop2GroupButton.group_name=name_group left join fop2buttons on id_button=fop2buttons.id group by fop2permissions.name" | while read LINEA
do
echo $LINEA
done

mysql -NB -u $DBUSER -p$DBPASS -h $DBHOST $DBNAME -e "set @@group_concat_max_len=32768; SELECT CONCAT('group=',group_name,':',GROUP_CONCAT(device)) FROM fop2GroupButton LEFT JOIN fop2buttons on id_button=fop2buttons.id WHERE id_button IS NOT NULL GROUP BY group_name" |  while read LINEA
do
echo $LINEA
done

mysql -NB -u $DBUSER -p$DBPASS -h $DBHOST $DBNAME -e "set @@group_concat_max_len=32768; SELECT CONCAT('user=',fop2users.exten,':',if(secret='','EMPTYSECRET',secret),':',permissions,':'),GROUP_CONCAT(if(name is NULL,id_group,name)) FROM fop2users LEFT OUTER JOIN fop2UserGroup ON fop2users.exten=fop2UserGroup.exten LEFT OUTER JOIN fop2groups ON id_group=fop2groups.id GROUP BY fop2users.exten" | while read LINEA
do
MYEXTEN=`echo $LINEA | cut -d: -f1 | cut -d\= -f2`
hash_find claves "${MYEXTEN}" 
echo -n $LINEA | sed 's/EMPTYSECRET/'${MICLAVE}'/g' | sed 's/: /:/g' | sed 's/:NULL$//g'
echo
done

echo "buttonfile=autobuttons.cfg"
else
#Generacion de usuarios sin freepbx plugin
for A in `cat /etc/asterisk/voicemail.conf | grep "=>" | cut -d, -f1 | sed 's/ => /:/g'`; do echo user=$A:all; done
echo "buttonfile=autobuttons.cfg"

fi
else
#Generacion de usuarios sin freepbx plugin
for A in `cat /etc/asterisk/voicemail.conf | grep "=>" | cut -d, -f1 | sed 's/ => /:/g'`; do echo user=$A:all; done
echo "buttonfile=autobuttons.cfg"

fi

