###############################################################################
#!/usr/bin/ksh
#set -x
#
###############################################################################
#
###############################################################################
#
# This file can be distributed and modified freely, as long as this original
# header is not modified.
#
# Author: Prince Uchendu
# Organization: IDSV eSolutions, Inc
# Date: 12-22-07
#
# This script will automatically install:
# Dependencies
# You must already have installed Asterisk-1.4 Asterisk-Addons- Asterisk-Sounds-
# Zaptel-1.4 LibPRI-1.4 Then this script will install A2Billng 1.3.1 yellowjacket for you
###############################################################################
#
###############################################################################
#
DOWNLOAD_DIR13=http://76.240.50.234/pub/a2billing

DOWNLOAD_FILE22=A2Billing_1.3.1.tar.gz


if [ "$1" = "" ]; then
clear
echo ""
echo ""
echo " -------------------------------------------------------------------"
echo " --- Please use asterdeworks new_passwd sys_ip ---"
echo " -------------------------------------------------------------------"
echo " ------------------Example Usage----------------------------- ----"
echo " --- ./asterdeworks new_passwd 76.240.50.234 ---"
echo " -------------------------------------------------------------------"
echo " NOTE:- MySQL 5.0 is installed by default ------------------------"
echo " NOTE:- PHP5 is also installed ------------------------------------"
echo " NOTE:- Apache2 is also installed----------------------------------"
echo " NOTE:- SSL support is installed-----------------------------------"
echo " -------------------------------------------- -----------------------"
echo ""
exit
fi


echo " ==================================================="
echo "| WARNING THIS SCRIPT IS FOR FRESH INSTALLS ONLY |"
echo "| WEIRD THINGS CAN HAPPEN AT ANY TIME |"
echo "| USE AT YOUR OWN DISCRETION |"
echo "| I CANNOT BE HELD RESPONSIBLE FOR DAMAGE CAUSED |"
echo "| BY NOT HEEDING THIS WARNING!!!! |"
echo " ==================================================="
echo "Continue - 'y' or 'n'"
read yesno
if [ $yesno = n ]
then
exit 0
fi
#
new_passwd=$1
sys_ip=$2
clear

echo "+++++++++++++START OF DEPENDENCY INSTALLATION+++++++++++++++"

yum -y install gcc* php-pear php-pear-DB libxml2-devel libtiff-devel php-mysql php-gd php-mbstring mysql-server sox kernel-devel bison ncurses-devel audiofile-devel libogg-devel openssl-devel subversion rpm-build sharutils mod_ssl perl-Net-SSLeay flex mysql-devel


echo "++++++++++++++++++All Depenencies Successfully Installed+++++++++++++++"

#------------------------------------------------------------------
#
# DOWNLOADING A2Billing
#
#------------------------------------------------------------------
# Download The A2Billing+++++++++++++++++++++++++++++++++++++++++++
cd /usr/src
mkdir a2billing
cd /usr/src/a2billing
echo
echo "----------- Fetching and Extracting A2B: ----------"
echo
echo "A2Billing download started"
WGET_OUTPUT=$(2>&1 wget --progress=dot:mega \
"$DOWNLOAD_DIR13/$DOWNLOAD_FILE22")

if [ $? -ne 0 ]
then
# wget had problems downloading A2Billing
echo 1>&2 $0: "$WGET_OUTPUT" Exiting.
exit 1
else

echo "A2Billing was downloaded Successfully"
tar -xzvf $DOWNLOAD_FILE22
rm "$DOWNLOAD_FILE22"
#------------------------------------------------------------------
#
# Install A2Billing
#
#------------------------------------------------------------------

rm /etc/asterisk/a2billing.conf

echo
echo "----------- Installing the new Database ----------"
echo
mysql -vv -e "GRANT ALL PRIVILEGES ON mya2billing.* TO 'a2billinguser'@'%' IDENTIFIED BY '$new_passwd'" -p$new_passwd
mysql -vv -e "GRANT ALL PRIVILEGES ON mya2billing.* TO 'a2billinguser'@'localhost' IDENTIFIED BY '$new_passwd'" -p$new_passwd
mysql -vv -e "GRANT ALL PRIVILEGES ON mya2billing.* TO 'a2billinguser'@'localhost.localdomain' IDENTIFIED BY '$new_passwd'" -p$new_passwd
mysql -vv -e "flush privileges" -p$new_passwd
mysqladmin create mya2billing -u a2billinguser -p$new_passwd
mysql mya2billing -u a2billinguser -p$new_passwd < /usr/src/a2billing/DataBase/mysql/Mysql-5.x/a2billing-mysql-schema-MYSQL.5.X-v1.3.0.sql
mysqladmin -u root password $new_passwd
service httpd restart

echo
echo "----------- Installing the new Web Interface ----------"
echo
cp -rf /usr/src/a2billing/A2Billing_UI /var/www/html/.
chmod 777 /etc/asterisk
cd /var/www/html/A2Billing_UI
chmod 777 templates_c
cp -rf /usr/src/a2billing/A2BCustomer_UI /var/www/html/.
cd /var/www/html/A2BCustomer_UI
chmod 777 templates_c
chmod -R 777 /var/www/html/A2BCustomer_UI
chmod -R 777 /var/www/html/A2Billing_UI

echo
echo "----------- Installing the default configuration file ----------"
echo
cp /usr/src/a2billing/a2billing.conf /etc/asterisk/.
sed -i "s/password = a2billing/password=$new_passwd/" /etc/asterisk/a2billing.conf
sed -i '/postgres/s/dbtype/;dbtype/g' /etc/asterisk/a2billing.conf
sed -i '/mysql/s/;dbtype/dbtype/g' /etc/asterisk/a2billing.conf
sed -i '/manager_user/s/myasterisk/a2billinguser/g' /etc/asterisk/a2billing.conf
sed -i '/manager_secret/s/mycode/$new_passwd/g' /etc/asterisk/a2billing.conf

echo
echo "----------- Installing the Sounds and Music on Hold ----------"
echo
mkdir /var/lib/asterisk/mohmp3/acc_1
mkdir /var/lib/asterisk/mohmp3/acc_2
mkdir /var/lib/asterisk/sounds/a2billing
chmod 777 /var/lib/asterisk/sounds/a2billing
cp /usr/src/a2billing/addons/sounds/* /var/lib/asterisk/sounds/.
cp /usr/src/a2billing/addons/sounds/en/* /var/lib/asterisk/sounds/.
chmod 777 /var/lib/asterisk/mohmp3/acc_*
mkdir /var/lib/asterisk/sounds/a2billing
chmod 777 /var/lib/asterisk/sounds/a2billing


echo
echo "----------- Installing the AGI ----------"
echo
cp /usr/src/a2billing/A2Billing_AGI/a2billing.php /var/lib/asterisk/agi-bin/.
cp -rf /usr/src/a2billing/A2Billing_AGI/libs_a2billing /var/lib/asterisk/agi-bin/.
chmod +x /var/lib/asterisk/agi-bin/a2billing.php
chmod 777 /var/lib/asterisk/agi-bin/a2billing.php


echo
echo "----------- Installing IAX and SIP conf Files ----------"
echo
cd /etc/asterisk/
touch additional_a2billing_iax.conf
touch additional_a2billing_sip.conf
touch extensions_a2billing.conf

chmod 666 /etc/asterisk/additional_a2billing_iax.conf
chmod 666 /etc/asterisk/additional_a2billing_sip.conf
chmod 666 /etc/asterisk/extensions_a2billing.conf

chown -R asterisk:asterisk /etc/asterisk/
chown -R asterisk:asterisk /var/www/html/
chown -R asterisk:asterisk /var/lib/asterisk/

echo
echo "----------- Registering IAX and SIP conf Files ----------"
echo

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 "
[a2billing]

exten => _X.,1,Answer
exten => _X.,n,Wait(1)
exten => _X.,n,DeadAGI(a2billing.php|1)
exten => _X.,n,Hangup
" >> /etc/asterisk/extensions_a2billing.conf

echo
echo "----------- Restarting some services: ----------"
echo

service httpd restart
service mysqld restart
amportal restart

fi

clear
echo "Add Asterisk Group"
groupadd asterisk

echo "Add Asterisk User"
useradd -c "Asterisk PBX" -d /var/lib/asterisk -g asterisk asterisk

chmod 755 -R /var/www/html

chown asterisk /etc/amportal.conf

chown asterisk /var/lib/php/session/

chown asterisk:asterisk -R /var/lib/asterisk/
chown asterisk:asterisk -R /etc/asterisk/
chown asterisk:asterisk -R /etc/amportal.conf
chmod 777 -R /var/lib/asterisk/agi-bin/
chmod 777 -R /var/lib/asterisk/
chmod 777 -R /etc/asterisk/
chmod 777 -R /etc/amportal.conf

sed -i "s/User apache/User asterisk/" /etc/httpd/conf/httpd.conf

sed -i "s/Group apache/Group asterisk/" /etc/httpd/conf/httpd.conf

mv -vf zaptel zaptel.old

touch zaptel

echo "TELEPHONY=yes" >> /etc/sysconfig/zaptel

echo "#DEBUG=yes" >> /etc/sysconfig/zaptel

echo 'MODULES="$MODULES ztdummy"' >> /etc/sysconfig/zaptel

mysql -vv -e "GRANT ALL PRIVILEGES ON asteriskcdrdb.* TO asteriskuser@localhost IDENTIFIED BY '$new_passwd'" -p$new_passwd

mysql -vv -e "GRANT ALL PRIVILEGES ON asterisk.* TO asteriskuser@localhost IDENTIFIED BY '$new_passwd'" -p$new_passwd

mysql -vv -e "flush privileges" -p$new_passwd

echo "[channels] " >> /etc/asterisk/zapata.conf

echo "language=en " >> /etc/asterisk/zapata.conf

echo "#include zapata_additional.conf " >> /etc/asterisk/zapata.conf

sed -i "s/AMPWEBADDRESS=xx.xx.xx.xx/AMPWEBADDRESS=$sys_ip/" /etc/amportal.conf
sed -i "s/FOPRUN=false/FOPRUN=true/" /etc/amportal.conf
sed -i "s/AMPMGRPASS=amp111/AMPMGRPASS=$new_passwd/" /etc/amportal.conf
sed -i "s/secret = amp111/secret=$new_passwd/" /etc/asterisk/manager.conf



echo "**************************************************************************************************"
echo "* INSTALLATION SUCCESSFUL *"
echo "**************************************************************************************************"
echo "* *"
echo "* *"
echo "* Direct your web browser to : http://192.168.0.15/A2Billing_UI to enter admin interface. *"
echo "* Username = admin ; Password = mypassword *"
echo "* Username = root ; Password = myroot *"
echo "* *"
echo "* *"
echo "* Direct your web browser to : http://yourserverIP/A2Bcustomer_UI to enter user interface. *"
echo "* Username = 'the card alias' which is set in A2Billing admin interface *"
echo "* *"
echo "* *"
echo "* Secure your server by entering admin interface => Administrator => and changing the password *"
echo "* for admin and root. *"
echo "* *"
echo "* *"
echo "* Congratulations !!! *"
echo "**************************************************************************************************"
echo "--------------------------------------------------------------------------------"
echo " Done!"
echo " You can access the FreePBX administration Console via: https://youripaddress/admin"
echo " You can access the Webmin via: https://youripaddress:10000 using username=root and password=rootpassword"
echo " You can access the phpsysinfo via: https://youripaddress/phpsysinfo"
echo " You can access the phpMyAdmion via: https://youripaddress/phpMyAdmin"
echo " You can access the phpPgAdmion via: https://youripaddress/phpPgAdmin"
echo " Enjoy and have fun! Prince Uchendu"
echo "--------------------------------------------------------------------------------"
exit 0

