#!/bin/bash

LOGFILE=$1

function choice {

        CHOICE=''
        local prompt="$*"
        local answer

        read -p "$prompt" answer
        case "$answer" in
                [yY1] ) CHOICE='y';;
                [nN0] ) CHOICE='n';;
                *     ) CHOICE="$answer";;
        esac
}

function wait_ten {
        echo -n "10 "
        sleep 1
        echo -n "9 "
        sleep 1
        echo -n "8 "
        sleep 1
        echo -n "7 "
        sleep 1
        echo -n "6 "
        sleep 1
        echo -n "5 "
        sleep 1
        echo -n "4 "
        sleep 1
        echo -n "3 "
        sleep 1
        echo -n "2 "
        sleep 1
        echo -n "1 "
        sleep 1
        echo ""
}

echo
echo "================================================================="
echo
echo "PHASE 3 - Installing Vicidial"
echo
echo "PHASE 3.1 - Downloading Latest SVN Code"
echo
echo "PHASE 3.1 will start in 10 seconds."
echo
echo "================================================================="
wait_ten


echo

echo "Downloading the lastest development Vicidial code"
echo "Downloading the lastest development Vicidial code" 2>> $LOGFILE >> $LOGFILE

/tmp/spinner &
SPINPID=`ps -e | grep pinner| sed 's/^ *//g' | cut --delimiter=" " -f1`

mkdir -p /usr/src/astguiclient 2>> $LOGFILE >> $LOGFILE
cd /usr/src/astguiclient 2>> $LOGFILE >> $LOGFILE
svn checkout svn://svn.eflo.net:43690/agc_2-X/trunk 2>> $LOGFILE >> $LOGFILE
#svn checkout svn://svn.eflo.net:43690/agc_2-X/branches/agc_2.0.4

disown $SPINPID
kill $SPINPID 2> /dev/null > /dev/null

cd trunk 2>> $LOGFILE >> $LOGFILE

echo
echo "================================================================="
echo
echo "PHASE 3.2 - Installing the Vicidial Code"
echo
echo "================================================================="

/tmp/spinner &
SPINPID=`ps -e | grep pinner| sed 's/^ *//g' | cut --delimiter=" " -f1`

perl install.pl \
--no-prompt \
--home=/usr/share/astguiclient \
--logs=/var/log/astguiclient \
--agi=/var/lib/asterisk/agi-bin \
--web=/var/www \
--sounds=/var/lib/asterisk/sounds \
--monitor=/var/spool/asterisk/monitor \
--DONEmonitor=/var/spool/asterisk/monitorDONE \
--server_ip=10.10.10.15 \
--DB_server=localhost \
--DB_database=asterisk \
--DB_user=cron \
--DB_pass=1234 \
--DB_port=3306 \
--active_keepalives=1234568 \
--asterisk_version=1.2 \
--copy_sample_conf_files \
--FTP_host=10.0.0.4 \
--FTP_user=cron \
--FTP_pass=test \
--FTP_port=21 \
--FTP_dir=RECORDINGS \
--HTTP_path=http://10.0.0.4 \
--REPORT_host=10.0.0.4 \
--REPORT_user=cron \
--REPORT_pass=test \
--REPORT_port=21 \
--REPORT_dir=REPORTS \
--fastagi_log_min_servers=3 \
--fastagi_log_max_servers=16 \
--fastagi_log_min_spare_servers=2 \
--fastagi_log_max_spare_servers=8 \
--fastagi_log_max_requests=1000 \
--fastagi_log_checkfordead=30 \
--fastagi_log_checkforwait=60 2>> $LOGFILE >> $LOGFILE

disown $SPINPID
kill $SPINPID 2> /dev/null > /dev/null

echo
echo "================================================================="
echo
echo "PHASE 3.2 - Loading Initial Database Settings"
echo
echo "================================================================="

echo

echo "Configuring MySQL for Vicidial"
echo "Configuring MySQL for Vicidial" 2>> $LOGFILE >> $LOGFILE

/etc/init.d/mysql stop

/tmp/spinner &
SPINPID=`ps -e | grep pinner| sed 's/^ *//g' | cut --delimiter=" " -f1`

# load more appropreiate settings
cd /etc/mysql/ 2>> $LOGFILE >> $LOGFILE
wget http://download.vicidial.com/ubuntu/ubuntu-my-vici.cnf 2>> $LOGFILE >> $LOGFILE
mv -f ubuntu-my-vici.cnf my.cnf 2>> $LOGFILE >> $LOGFILE

/tmp/spinner &
SPINPID=`ps -e | grep pinner| sed 's/^ *//g' | cut --delimiter=" " -f1`

# start mysql without permissions
mysqld_safe --skip-grant-tables 2>> $LOGFILE >> $LOGFILE &

# make sure it has time to come up
sleep 10 2>> $LOGFILE >> $LOGFILE

# get the root users password
mysql --execute="select Password from mysql.user where user='root' limit 1" > /tmp/userpwd
PASSWORD=`cat /tmp/userpwd | grep -v "Password"` 2>> $LOGFILE >> $LOGFILE
rm /tmp/userpwd 2>> $LOGFILE >> $LOGFILE

# wipe the root users password so we do not need to prompt for one
mysql mysql --user=root --execute="update user set Password=PASSWORD('') where user='root';"  2>> $LOGFILE >> $LOGFILE

# shut down mysql so we can start it properly
mysqladmin shutdown  2>> $LOGFILE >> $LOGFILE

disown $SPINPID
kill $SPINPID 2> /dev/null > /dev/null

# start mysql properly
/etc/init.d/mysql start

echo

echo "Creating and populating the initial Vicidial database"
echo "Creating and populating the initial Vicidial database" 2>> $LOGFILE >> $LOGFILE

/tmp/spinner &
SPINPID=`ps -e | grep pinner| sed 's/^ *//g' | cut --delimiter=" " -f1`

# create the asterisk db and populate it properly
cd /usr/src/astguiclient/trunk  2>> $LOGFILE >> $LOGFILE
mysqladmin create asterisk  2>> $LOGFILE >> $LOGFILE
mysql asterisk --execute="GRANT SELECT,INSERT,UPDATE,DELETE,LOCK TABLES on asterisk.* TO cron@'%' IDENTIFIED BY '1234';"  2>> $LOGFILE >> $LOGFILE
mysql asterisk --execute="GRANT SELECT,INSERT,UPDATE,DELETE,LOCK TABLES on asterisk.* TO cron@localhost IDENTIFIED BY '1234';"  2>> $LOGFILE >> $LOGFILE
mysql asterisk --execute="\. /usr/src/astguiclient/trunk/extras/MySQL_AST_CREATE_tables.sql"  2>> $LOGFILE >> $LOGFILE
mysql asterisk --execute="\. /usr/src/astguiclient/trunk/extras/sip-iax_phones.sql"  2>> $LOGFILE >> $LOGFILE
mysql asterisk --execute="\. /usr/src/astguiclient/trunk/extras/first_server_install.sql"  2>> $LOGFILE >> $LOGFILE

mysql mysql --execute="UPDATE user set password='$PASSWORD' where user='root';"  2>> $LOGFILE >> $LOGFILE


# update the IP for the server
/usr/share/astguiclient/ADMIN_update_server_ip.pl  --auto --old-server_ip=10.10.10.15  2>> $LOGFILE >> $LOGFILE

# populate the area code table
/usr/share/astguiclient/ADMIN_area_code_populate.pl  2>> $LOGFILE >> $LOGFILE

disown $SPINPID
kill $SPINPID 2> /dev/null > /dev/null

/etc/init.d/mysql restart 2>> $LOGFILE >> $LOGFILE

echo
echo "================================================================="
echo
echo "PHASE 3.3 - Loading Performance Test Leads into list 999"
echo
echo "================================================================="

/tmp/spinner &
SPINPID=`ps -e | grep pinner| sed 's/^ *//g' | cut --delimiter=" " -f1`

# Load the leads
cp /usr/src/astguiclient/trunk/extras/performance_test_leads.txt /usr/share/astguiclient/LEADS_IN/  2>> $LOGFILE >> $LOGFILE
/usr/share/astguiclient/VICIDIAL_IN_new_leads_file.pl --forcelistid=999 --forcephonecode=1  2>> $LOGFILE >> $LOGFILE

disown $SPINPID
kill $SPINPID 2> /dev/null > /dev/null

echo
echo "================================================================="
echo
echo "PHASE 3.4 - Configuration for Ubuntu"
echo
echo "================================================================="

echo

echo "Configuring Apache for Vicidial recordings"
echo "Configuring Apache for Vicidial recordings" 2>> $LOGFILE >> $LOGFILE

cd /var/www/  2>> $LOGFILE >> $LOGFILE
chown -R www-data *  2>> $LOGFILE >> $LOGFILE

cd /var/spool/asterisk  2>> $LOGFILE >> $LOGFILE
chown -R www-data monitorDONE  2>> $LOGFILE >> $LOGFILE

echo

echo "Configuring Vicidial cron jobs"
echo "Configuring Vicidial cron jobs" 2>> $LOGFILE >> $LOGFILE

/tmp/spinner &
SPINPID=`ps -e | grep pinner| sed 's/^ *//g' | cut --delimiter=" " -f1`

cd /usr/src/astguiclient  2>> $LOGFILE >> $LOGFILE
wget http://download.vicidial.com/ubuntu/vicicron  2>> $LOGFILE >> $LOGFILE
crontab -l > rootcron
cat vicicron >> rootcron 
crontab rootcron 2>> $LOGFILE >> $LOGFILE

disown $SPINPID
kill $SPINPID 2> /dev/null > /dev/null

echo

echo "Installing Vicidial init script"
echo "Installing Vicidial init script" 2>> $LOGFILE >> $LOGFILE

/tmp/spinner &
SPINPID=`ps -e | grep pinner| sed 's/^ *//g' | cut --delimiter=" " -f1`

cd /etc/init.d 2>> $LOGFILE >> $LOGFILE
wget http://download.vicidial.com/ubuntu/vicidial 2>> $LOGFILE >> $LOGFILE
chmod 755 /etc/init.d/vicidial 2>> $LOGFILE >> $LOGFILE

cd /etc/rc2.d/ 2>> $LOGFILE >> $LOGFILE
ln -s ../init.d/vicidial S92vicidial  2>> $LOGFILE >> $LOGFILE
cd /etc/rc3.d/ 2>> $LOGFILE >> $LOGFILE
ln -s ../init.d/vicidial S92vicidial 2>> $LOGFILE >> $LOGFILE
cd /etc/rc4.d/ 2>> $LOGFILE >> $LOGFILE
ln -s ../init.d/vicidial S92vicidial 2>> $LOGFILE >> $LOGFILE
cd /etc/rc5.d/ 2>> $LOGFILE >> $LOGFILE
ln -s ../init.d/vicidial S92vicidial 2>> $LOGFILE >> $LOGFILE

cd /etc/rc6.d/ 2>> $LOGFILE >> $LOGFILE
ln -s ../init.d/vicidial K08vicidial 2>> $LOGFILE >> $LOGFILE
cd /etc/rc1.d/ 2>> $LOGFILE >> $LOGFILE
ln -s ../init.d/vicidial K08vicidial 2>> $LOGFILE >> $LOGFILE

disown $SPINPID
kill $SPINPID 2> /dev/null > /dev/null

echo

echo "Restarting MySQL and Apache"
echo "Restarting MySQL and Apache" 2>> $LOGFILE >> $LOGFILE

/etc/init.d/apache2 restart
/etc/init.d/mysql restart

echo

echo "Starting Vicidial"
echo "Starting Vicidial" 2>> $LOGFILE >> $LOGFILE

/etc/init.d/vicidial start

echo
echo
screen -ls
echo

SERVERIP=`cat /etc/astguiclient.conf | grep VARserver_ip | cut --delimiter=" " -f3`  2>> $LOGFILE >> $LOGFILE

echo "================================================================="
echo
echo "PHASE 3 - Complete"
echo
echo "Congradulations. Vicidial is installed and ready for configuring."
echo
echo "You should see eight screen sessions listed above."
echo "To access the web interface, from another computer, open:"
echo "http://$SERVERIP/vicidial/admin.php"
echo "The username is 6666 and the password is 1234"
echo
echo "================================================================="

