#!/bin/bash

function choice {

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

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

echo "ViciDial SVN Trunk Installer Script for OpenSuSE v.11.0"
echo
choice "Do you want to continue? (N/y) : "


if [ "$CHOICE" == "y" ]; then # All code to run before here

echo
choice "Did you use Asterisk v.1.4? (N/y) : "
AST14=$CHOICE

mkdir -p /usr/src/astguiclient
cd /usr/src/astguiclient
svn checkout svn://svn.eflo.net:43690/agc_2-X/trunk
svn checkout svn://svn.eflo.net:43690/agc_2-X/branches/agc_2.0.4
cd trunk
echo
echo "Installing ViciDial"
echo
perl install.pl --web=/srv/www/htdocs --copy_sample_conf_files
echo
echo "Installing DataBase with workaround for buggy Yast updates"
echo
cd /etc
wget http://public.vicidial.com/conf/my-vici.cnf
mv -f my-vici.cnf my.cnf
/etc/init.d/mysql start
echo
echo "Loading Initial Database Settings"
echo
cd /usr/src/astguiclient/trunk
mysqladmin create asterisk
mysql asterisk --execute="GRANT SELECT,INSERT,UPDATE,DELETE,LOCK TABLES on asterisk.* TO cron@'%' IDENTIFIED BY '1234';"
mysql asterisk --execute="GRANT SELECT,INSERT,UPDATE,DELETE,LOCK TABLES on asterisk.* TO cron@localhost IDENTIFIED BY '1234';"
mysql asterisk --execute="\. /usr/src/astguiclient/trunk/extras/MySQL_AST_CREATE_tables.sql"
mysql asterisk --execute="\. /usr/src/astguiclient/trunk/extras/sip-iax_phones.sql"
mysql asterisk --execute="\. /usr/src/astguiclient/trunk/extras/first_server_install.sql"
/usr/share/astguiclient/ADMIN_update_server_ip.pl --old-server_ip=10.10.10.15
/usr/share/astguiclient/ADMIN_area_code_populate.pl
cp /usr/src/astguiclient/trunk/extras/performance_test_leads.txt /usr/share/astguiclient/LEADS_IN/
/usr/share/astguiclient/VICIDIAL_IN_new_leads_file.pl --forcelistid=999 --forcephonecode=1
echo "Performance Leads loaded into list 999 for testing"
/etc/init.d/apache2 restart
echo "Installing CRON and Start-Up Scripts"
cd /usr/src/astguiclient
wget http://public.vicidial.com/conf/vicicron
crontab -l > rootcron
cat vicicron >> rootcron
crontab rootcron
cd /etc/init.d
wget http://public.vicidial.com/conf/vicidial
chmod 755 /etc/init.d/vicidial
if [ "$AST14" == "y" ]; then
        cp /usr/src/astguiclient/trunk/docs/conf_examples/extensions.conf.sample-1.4 /etc/asterisk/extensions.conf
fi

# allow apache to own the monitorDONE directory.
chown -R wwwrun /var/spool/asterisk/monitorDONE

chkconfig apache2 35
chkconfig mysql 235
chkconfig vicidial 35
/etc/init.d/apache2 restart
/etc/init.d/mysql restart
/etc/init.d/vicidial start
echo
echo
echo "Hooray! We are done with the Base Install! Reboot to make sure everything cleanly loads!"
fi
