#!/bin/bash
config="/etc/astercc.conf"

if [ ! -e ${config} ];then
	echo "reloadconf.sh: Can not find config file "${config}
else    
	dbname=`sed -n '/\[database]/,/\[.*\]/p' ${config} |grep dbname[\ ]*= |grep -v "^\ *#" |cut -d= -f2 |tr -d " "`
	dbhost=`sed -n '/\[database]/,/\[.*\]/p' ${config} |grep dbhost[\ ]*= |grep -v "^\ *#" |cut -d= -f2 |tr -d " "`
	dbuser=`sed -n '/\[database]/,/\[.*\]/p' ${config} |grep username[\ ]*= |grep -v "^\ *#" |cut -d= -f2 |tr -d " "`
	dbpasswd=`sed -n '/\[database]/,/\[.*\]/p' ${config} |grep password[\ ]*= |grep -v "^\ *#" |cut -d= -f2 |tr -d " "`
	prefix=`sed -n '/\[database]/,/\[.*\]/p' ${config} |grep prefix[\ ]*= |grep -v "^\ *#" |cut -d= -f2 |tr -d " "`

	if [ "${dbpasswd}X" != "X" ];then
		`mysql -h${dbhost} -u${dbuser} -p${dbpasswd} -e "use ${dbname}; INSERT INTO ${prefix}reload_tables (reload_model, team_id, bartype) VALUES ('Device', '0', 'conf'), ('Trunk', '0', 'conf'), ('Queue', '0', 'conf'), ('Moh', '0', 'conf'), ('Featruecode', '0', 'conf');"`
	else
		`mysql -h${dbhost} -u${dbuser}  -e "use ${dbname}; INSERT INTO ${prefix}reload_tables (reload_model, team_id, bartype) VALUES ('Device', '0', 'conf'), ('Trunk', '0', 'conf'), ('Queue', '0', 'conf'), ('Moh', '0', 'conf'), ('Featruecode', '0', 'conf');"`	
	fi
    
    `/var/www/html/asterCC/cake/console/cake -app /var/www/html/asterCC/app device >/dev/null 2>&1`
    `/var/www/html/asterCC/cake/console/cake -app /var/www/html/asterCC/app trunk >/dev/null 2>&1`
    `/var/www/html/asterCC/cake/console/cake -app /var/www/html/asterCC/app queue >/dev/null 2>&1`
    `/var/www/html/asterCC/cake/console/cake -app /var/www/html/asterCC/app moh >/dev/null 2>&1`
    `/var/www/html/asterCC/cake/console/cake -app /var/www/html/asterCC/app featruecode >/dev/null 2>&1`

    echo "reloadconf.sh: Done"
fi

exit