#!/bin/bash
yum -y install perl-DBD-Pg
cd /usr/src
wget --no-check-certificate https://github.com/Star2Billing/a2billing/archive/master.tar.gz
tar zxvf master
mv a2billing-master a2billing
cd /usr/src/a2billing
#Installl DataBase
cd /usr/src/a2billing/DataBase/mysql-5.x
mysql -u root -ppassw0rd < ./a2billing-createdb-user.sql
cat a2billing-schema-v1.4.0.sql mysql --user=a2billinguser --password=a2billing --host=localhost mya2billing
cp /usr/src/a2billing/a2billing.conf /etc/
sed -i '/port/ s/$/ 3306/' /etc/a2billing.conf
sed -i 's/a2billing_dbuser/a2billinguser/g' /etc/a2billing.conf
sed -i 's/a2billing_dbname/mya2billing/g' /etc/a2billing.conf
sed -i 's/a2billing_dbpassword/a2billing/g' /etc/a2billing.conf
#Create files and set permissions
chmod 777 /etc/asterisk
touch /etc/asterisk/additional_a2billing_iax.conf
touch /etc/asterisk/additional_a2billing_sip.conf
echo "#include additional_a2billing_sip.conf" >> /etc/asterisk/sip_custom.conf
echo "#include additional_a2billing_iax.conf" >> /etc/asterisk/iax_custom.conf
chown -Rf asterisk:asterisk /etc/asterisk/additional_a2billing_iax.conf
chown -Rf asterisk:asterisk /etc/asterisk/additional_a2billing_sip.conf
#Run sound installation script
cd /usr/src/a2billing/addons/sounds
./install_a2b_sounds.sh
chown -R asterisk:asterisk /var/lib/asterisk/sounds/
#Add the following to /etc/asterisk/manager_custom.conf
echo "
[general]
enabled = yes
port = 5038
bindaddr = 0.0.0.0
[myasterisk]
secret = mycode
deny=0.0.0.0/0.0.0.0
permit=127.0.0.1/255.255.255.0
read = system,call,log,verbose,command,agent,user
write = system,call,log,verbose,command,agent,user
" >> /etc/asterisk/manager_custom.conf
#Set up a2billing web folder
mkdir /var/www/html/a2billing
cp -Rf /usr/src/a2billing/admin /var/www/html/a2billing/admin
cp -Rf /usr/src/a2billing/agent /var/www/html/a2billing/agent
cp -Rf /usr/src/a2billing/customer /var/www/html/a2billing/customer
cp -Rf /usr/src/a2billing/common /var/www/html/a2billing/common
chown -R asterisk:asterisk /var/www/html/a2billing
chmod 755 /var/www/html/a2billing/admin/templates_c
chmod 755 /var/www/html/a2billing/customer/templates_c
chmod 755 /var/www/html/a2billing/agent/templates_c
#Set up AGI
mkdir /var/lib/asterisk/agi-bin
cd /usr/src/a2billing/AGI
cp a2billing.php /var/lib/asterisk/agi-bin/
chown -R asterisk:asterisk /var/lib/asterisk/agi-bin
chmod 755 /var/lib/asterisk/agi-bin/a2billing.php
ln -s /var/www/html/a2billing/common/lib /var/lib/asterisk/agi-bin/lib
#Add the following extensions to /etc/asterisk/extensions_custom.conf
echo "
#include additional_a2billing_sip.conf
" >> /etc/asterisk/sip_custom.conf
echo "
#include additional_a2billing_iax.conf
" >> /etc/asterisk/iax_custom.conf
echo "
#include extensions_a2billing.conf
" >> /etc/asterisk/extensions_custom.conf
echo '
[macro-dialout-trunk-predial-hook]
exten => s,1,GotoIf($["${OUT_${DIAL_TRUNK}:4:4}" = "A2B/"]?custom-freepbx-a2billing,${OUTNUM},1:2)
exten => s,2,MacroExit
[custom-freepbx-a2billing]
exten => _X.,1,AGI(a2billing.php,${OUT_${DIAL_TRUNK}:8})
exten => _X.,n,Hangup()
' >> /etc/asterisk/extensions_custom.conf
#Add the A2Billing context
echo "
[a2billing]
exten => _X.,1,Answer
exten => _X.,n,Wait(1)
exten => _X.,n,AGI(a2billing.php,1)
exten => _X.,n,Hangup
[a2billing-callback]
exten => _X.,1,AGI(a2billing.php,1,callback)
exten => _X.,n,Hangup
[a2billing-cid-callback]
exten => _X.,1,AGI(a2billing.php,1,cid-callback,34) ;last parameter is the callback area code
exten => _X.,n,Hangup
[a2billing-all-callback]
exten => _X.,1,AGI(a2billing.php,1,all-callback,34) ;last parameter is the callback area code
exten => _X.,n,Hangup
[a2billing-did]
exten => _X.,1,AGI(a2billing.php,1,did)
exten => _X.,2,Hangup
[a2billing-voucher]
exten => _X.,1,AGI(a2billing.php,1,voucher)
exten => _X.,n,Hangup
[custom-a2billing-did]
exten => _X.,1,AGI(a2billing.php,1,did)
exten => _X.,2,Hangup
[custom-a2billing]
exten => _X.,1,AGI(a2billing.php,1)
exten => _X.,n,Hangup
" >> /etc/asterisk/extensions_a2billing.conf
#Recurring Services
#Recurring services are handled via the /etc/crontab.
#Make directory for A2Billing cron PID
mkdir -p /var/run/a2billing
chown asterisk:asterisk /var/run/a2billing
Copy cron files to some permanent location such as /usr/local
mkdir -p /usr/local/a2billing
cp -R /usr/src/a2billing/Cronjobs /usr/local/a2billing/
ln -sf /var/www/html/a2billing/common/lib /usr/local/a2billing/Cronjobs/lib
chown -R asterisk:asterisk /usr/local/a2billing
#Add the cron jobs to /var/spool/cron/asterisk.
echo "
# update the currency table
0 6 * * * php /usr/local/a2billing/Cronjobs/currencies_update_yahoo.php
# manage the monthly services subscription
0 6 1 * * php /usr/local/a2billing/Cronjobs/a2billing_subscription_fee.php
# To check account of each Users and send an email if the balance is less than the user have choice.
0 * * * * php /usr/local/a2billing/Cronjobs/a2billing_notify_account.php
# this script will browse all the DID that are reserve and check if the customer need to pay for it.
# bill them or warn them per email to know if they want to pay in order to keep their DIDs.
0 2 * * * php /usr/local/a2billing/Cronjobs/a2billing_bill_diduse.php
# This script will take care of the recurring service.
0 12 * * * php /usr/local/a2billing/Cronjobs/a2billing_batch_process.php
# To generate invoices and for each user.
0 6 * * * php /usr/local/a2billing/Cronjobs/a2billing_batch_billing.php
# to proceed the autodialer
*/5 * * * * php /usr/local/a2billing/Cronjobs/a2billing_batch_autodialer.php
# manage alarms
0 * * * * php /usr/local/a2billing/Cronjobs/a2billing_alarm.php
# manage archive
0 12 * * * php /usr/local/a2billing/Cronjobs/a2billing_archive_data_cront.php
#autorefill
0 10 21 * * php /usr/local/a2billing/Cronjobs/a2billing_autorefill.php
15 * * * * php /usr/local/a2billing/Cronjobs/a2billing_batch_cache.php
" >> /var/spool/cron/asterisk
#Add log files:
mkdir -p /var/log/a2billing
touch /var/log/a2billing/cront_a2b_alarm.log
touch /var/log/a2billing/cront_a2b_autorefill.log
touch /var/log/a2billing/cront_a2b_batch_process.log
touch /var/log/a2billing/cront_a2b_archive_data.log
touch /var/log/a2billing/cront_a2b_bill_diduse.log
touch /var/log/a2billing/cront_a2b_subscription_fee.log
touch /var/log/a2billing/cront_a2b_currency_update.log
touch /var/log/a2billing/cront_a2b_invoice.log
touch /var/log/a2billing/cront_a2b_check_account.log
touch /var/log/a2billing/a2billing_paypal.log
touch /var/log/a2billing/a2billing_epayment.log
touch /var/log/a2billing/a2billing_api_ecommerce_request.log
touch /var/log/a2billing/a2billing_api_callback_request.log
touch /var/log/a2billing/a2billing_api_card.log
touch /var/log/a2billing/a2billing_agi.log
chown -R asterisk:asterisk /var/log/a2billing
#Asterisk Realtime
echo "iaxusers => mysql,general,cc_iax_buddies
iaxpeers => mysql,general,cc_iax_buddies
sipusers => mysql,general,cc_sip_buddies
sippeers => mysql,general,cc_sip_buddies
extensions => mysql,general,cc_sip_buddies
" >> /etc/asterisk/extconfig.conf
echo "dbhost = 127.0.0.1
dbname = mya2billing
dbuser = a2billinguser
dbpass = a2billing
dbport = 3306
" >> /etc/asterisk/res_config_mysql.conf
#Callback
yum -y install python-psycopg2 python-sqlalchemy
cd /usr/src/a2billing/CallBack/callback-daemon-py/dist
tar -xvf callback_daemon-1.0.prod-r1528.tar.gz
cd callback_daemon-1.0.prod-r1528
python setup.py build
python setup.py bdist_egg
easy_install dist/callback_daemon-1.0.prod_r1528-py2.6.egg
cd /usr/src/a2billing-master/CallBack/callback-daemon-py/dist/callback_daemon-1.0.prod-r1528/build/lib/
cp callback_daemon/a2b-callback-daemon.rc /etc/init.d/a2b-callback-daemon
chmod +x /etc/init.d/a2b-callback-daemon
echo "service a2b-callback-daemon start" >> /etc/rc.d/rc.local
amportal restart
echo
echo
echo "*************************************************"
echo "* Goto http://<serverip>>/a2billing/admin *"
echo '* Login as "root" and password "changepassword" *'
echo "*************************************************"
echo
echo