#!/bin/bash

# (C) 2006 Oden Eriksson <oeriksson@mandriva.com>
# This program is Free Software under the GNU General Public License (>=v2).
# Read the file COPYING that comes with this packages for details.

if [ "x`whoami`" != "xroot" ]; then
    echo 'You must be root to run this.'
    exit
fi

echo ""
echo "[ freepbx_bootstrap ] Mandriva Configuration Script for FreePBX"
echo ""
echo "   * This script will configure FreePBX on a stock Mandriva installation"
echo ""
echo "   * You _must_ read INSTALL completely first.  "
echo "     Some Manual steps are still required."
echo ""
echo "UNSUPPORTED -  Please use at your own risk.   "
echo ""
echo -n "Hit Return to continue or Ctrl-C to exit : "
read foo

DATE=`date +%Y%m%d%N`
echo "Saving old Asterisk configuration..."
pushd /usr/share/freepbx/asterisk >/dev/null 2>&1
for f in *.conf; do
    if [ -f /etc/asterisk/${f} ]; then
	echo "Saving old /etc/asterisk/${f} as /etc/asterisk/${f}-freepbx_backup-${DATE}"
	mv /etc/asterisk/${f} /etc/asterisk/${f}-freepbx_backup-${DATE}
    fi
    cp ${f} /etc/asterisk/
    chmod 640 /etc/asterisk/${f}
    chown asterisk:asterisk /etc/asterisk/${f}
done
cp *.sample *.template *.inc /etc/asterisk/
chmod 640 /etc/asterisk/*.sample /etc/asterisk/*.template /etc/asterisk/*.inc
chown asterisk:asterisk /etc/asterisk/*.sample /etc/asterisk/*.template /etc/asterisk/*.inc
popd >/dev/null 2>&1

# AMPDBUSER: the user to connect to the database named 'asterisk'
AMPDBUSER=`grep '^AMPDBUSER' /etc/freepbx/amportal.conf|cut -d= -f2`
# AMPDBPASS: the password for AMPDBUSER
AMPDBPASS=`perl -e 'for ($i = 0, $bit = "!", $key = ""; $i < 8; $i++) {while ($bit !~ /^[0-9A-Za-z]$/) { $bit = chr(rand(90) + 32); } $key .= $bit; $bit = "!"; } print "$key";'`
echo -n "Please enter the FreePBX Database password for the AMPDBUSER ($AMPDBUSER) user, press enter for the generated one (${AMPDBPASS}): "
read AMPDBPASS_db_passwd
if [ -z ${AMPDBPASS_db_passwd} ]; then AMPDBPASS_db_passwd=${AMPDBPASS}; fi
sed -r -i "s/^AMPDBPASS=[a-zA-Z0-9]*/AMPDBPASS=${AMPDBPASS_db_passwd}/" /etc/freepbx/amportal.conf
sed -r -i "s/password=[a-zA-Z0-9]*/password=${AMPDBPASS_db_passwd}/" /etc/asterisk/cdr_mysql.conf
sed -r -i "s/user=[a-zA-Z0-9]*/user=$AMPDBUSER/" /etc/asterisk/cdr_mysql.conf
chmod 640 /etc/freepbx/amportal.conf; chown apache:root /etc/freepbx/amportal.conf
chmod 640 /etc/asterisk/cdr_mysql.conf; chown asterisk:asterisk /etc/asterisk/cdr_mysql.conf

# AMPDBHOST: the host to connect to the database named 'asterisk'
echo -n "Please enter the FreePBX Database host, press enter for the default hostname (localhost): "
read AMPDBHOST_hostname
if [ -z ${AMPDBHOST_hostname} ]; then AMPDBHOST_hostname=localhost; fi
sed -r -i "s/hostname=[a-zA-Z0-9]*/hostname=${AMPDBHOST_db_passwd}/" /etc/asterisk/cdr_mysql.conf

# AMPMGRUSER: the user to access the Asterisk manager interface
AMPMGRUSER=`grep '^AMPMGRUSER' /etc/freepbx/amportal.conf|cut -d= -f2`
# AMPMGRPASS: the password for AMPMGRUSER
AMPMGRPASS=`perl -e 'for ($i = 0, $bit = "!", $key = ""; $i < 8; $i++) {while ($bit !~ /^[0-9A-Za-z]$/) { $bit = chr(rand(90) + 32); } $key .= $bit; $bit = "!"; } print "$key";'`
echo -n "Please enter the FreePBX Database password for the AMPMGRPASS ($AMPMGRUSER) user, press enter for the generated one (${AMPMGRPASS}): "
read AMPMGRPASS_db_passwd
if [ -z ${AMPMGRPASS_db_passwd} ]; then AMPMGRPASS_db_passwd=${AMPMGRPASS}; fi
sed -r -i "s/^AMPMGRPASS=[a-zA-Z0-9]*/AMPDBPASS=${AMPMGRPASS_db_passwd}/" /etc/freepbx/amportal.conf
sed -r -i "s/secret = [a-zA-Z0-9]*/secret = ${AMPMGRPASS_db_passwd}/" /etc/asterisk/manager.conf
sed -r -i "/\[general\]/!s/\[[a-zA-Z0-9]+\]/[$AMPMGRUSER]/" /etc/asterisk/manager.conf
chmod 640 /etc/freepbx/amportal.conf; chown apache:root /etc/freepbx/amportal.conf
chmod 640 /etc/asterisk/manager.conf; chown asterisk:asterisk /etc/asterisk/manager.conf

# FOPPASSWORD: the secret code for performing transfers and hangups in the Flash Operator Panel
FOPPASSWORD=`perl -e 'for ($i = 0, $bit = "!", $key = ""; $i < 8; $i++) {while ($bit !~ /^[0-9A-Za-z]$/) { $bit = chr(rand(90) + 32); } $key .= $bit; $bit = "!"; } print "$key";'`
echo -n "Please enter the FreePBX FOPPASSWORD, press enter for the generated one (${FOPPASSWORD}): "
read FOPPASSWORD_passwd
if [ -z ${FOPPASSWORD_passwd} ]; then FOPPASSWORD_passwd=${FOPPASSWORD}; fi
sed -r -i "s/^FOPPASSWORD=[a-zA-Z0-9]*/FOPPASSWORD=${FOPPASSWORD_passwd}/" /etc/freepbx/amportal.conf
chmod 640 /etc/freepbx/amportal.conf; chown apache:root /etc/freepbx/amportal.conf

# AMPWEBADDRESS: the IP address or host name used to access the AMP web admin
AMPWEBADDRESS=`hostname --fqdn`
echo -n "Please enter the FreePBX web host, press enter for the detected hostname (${AMPWEBADDRESS}): "
read AMPWEBADDRESS_hostname
if [ -z ${AMPWEBADDRESS_hostname} ]; then AMPWEBADDRESS_hostname=${AMPWEBADDRESS}; fi
sed -r -i "s/^AMPWEBADDRESS=[a-zA-Z0-9]*/AMPWEBADDRESS=${AMPWEBADDRESS_hostname}/" /etc/freepbx/amportal.conf

if [ -e /etc/op_panel/op_server.cfg ]; then
    FOPWEBROOT=`grep '^FOPWEBROOT' /etc/freepbx/amportal.conf|cut -d= -f2`
    sed -r -i "s/manager_user=[a-zA-Z0-9]*/manager_user=$AMPMGRUSER/" /etc/op_panel/op_server.cfg
    sed -r -i "s/manager_secret=[a-zA-Z0-9]*/manager_secret=${AMPMGRPASS_db_passwd}/" /etc/op_panel/op_server.cfg
    sed -r -i "s/web_hostname=[a-zA-Z0-9_.-]*/web_hostname=${AMPWEBADDRESS_hostname}/" /etc/op_panel/op_server.cfg
    sed -r -i "s/security_code=[a-zA-Z0-9]*/security_code=${FOPPASSWORD_passwd}/" /etc/op_panel/op_server.cfg
    sed -r -i "s!flash_dir=[a-zA-Z0-9_./\-]*!flash_dir=$FOPWEBROOT!" /etc/op_panel/op_server.cfg
    sed -r -i "s!web_hostname=[a-zA-Z0-9.-]*!web_hostname=${AMPWEBADDRESS_hostname}!" /etc/op_panel/op_server.cfg
    sed -r -i "s!web_hostname=[a-zA-Z0-9.-]*!web_hostname=${AMPWEBADDRESS_hostname}!" /etc/op_panel/op_server.cfg
fi

if [ "xx${AMPWEBADDRESS_hostname}" = "xx" ]; then
	echo "You might need to modify /etc/asterisk/vm_email.inc manually"
else
	sed -r -i "s!http://.*/recordings!http://${AMPWEBADDRESS_hostname}/recordings!" /etc/asterisk/vm_email.inc
fi

# FOPPASSWORD: the secret code for performing transfers and hangups in the Flash Operator Panel
AMPWEBPASSWORD=`perl -e 'for ($i = 0, $bit = "!", $key = ""; $i < 8; $i++) {while ($bit !~ /^[0-9A-Za-z]$/) { $bit = chr(rand(90) + 32); } $key .= $bit; $bit = "!"; } print "$key";'`
echo -n "Please enter the password for the web admin user, press enter for the generated one (${AMPWEBPASSWORD}): "
read AMPWEBPASSWORD_passwd
if [ -z ${AMPWEBPASSWORD_passwd} ]; then AMPWEBPASSWORD_passwd=${AMPWEBPASSWORD}; fi
echo "# clear text password for the admin user is: ${AMPWEBPASSWORD_passwd}" >> /etc/freepbx/freepbx_web_users
htpasswd -m -b /etc/freepbx/freepbx_web_users admin ${AMPWEBPASSWORD_passwd}
chmod 640 /etc/freepbx/freepbx_web_users; chown apache:root /etc/freepbx/freepbx_web_users

echo "Setting up MySQL for CDR and freePBX web interface

	- mysqladmin -u root password 'db_root_pwd'
	- mysqladmin create asteriskcdrdb -p
	- mysql --user=root --password=db_root_pwd asteriskcdrdb < /usr/share/doc/freepbx-2.0.2/cdr_mysql_table.sql
	
	- mysqladmin create asterisk -p
	- mysql --user root -p asterisk < /usr/share/doc/freepbx-2.0.2/newinstall.sql

	
###
Grant access to these two databases you just created

**Note the default mysql username/password is asteriskuser/amp109.  
**If you change either of these, you will be prompted for them while running ./install below

	- mysql --user root -p

		mysql> GRANT ALL PRIVILEGES ON asteriskcdrdb.* TO asteriskuser@localhost IDENTIFIED BY '${AMPDBPASS_db_passwd}';
		Query OK, 0 rows affected (0.00 sec)

		mysql> GRANT ALL PRIVILEGES ON asterisk.* TO asteriskuser@localhost IDENTIFIED BY '${AMPDBPASS_db_passwd}';
		Query OK, 0 rows affected (0.00 sec)
		
		mysql> quit"



echo "Installation completed."
echo "login via: http://${AMPWEBADDRESS_hostname}/freepbx/ as admin and with the ${AMPWEBPASSWORD_passwd} password"

