#!/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 "ViciDial Installer Script for Ubuntu Server 8.04.2"
echo 
echo "PHASE 2 - The Prerequisites"
echo
echo "PHASE 2.1 - Installing packages from Ubuntu"
echo
echo "You will be prompted for a MySQL password."
echo
echo "You will also be asked which http server you want to set"
echo "PhpMyAdmin up for. Choose 'apache2'"
echo
echo "For a full log of the installation process please look in"
echo "/var/log/astguiclient/ubuntu-vici-install.log"
echo "Some output is not surpressed due to issues with their prompts"
echo
echo "Phase 2.1 will start in 10 seconds."
echo
echo "================================================================="
wait_ten

echo

echo "Installing MySQL prereqs from Ubuntu"
echo "Installing MySQL prereqs from Ubuntu" 2>> $LOGFILE >> $LOGFILE
# Install packages available from Ubuntu

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

apt-get -y --force-yes install mysql-common libnet-daemon-perl libplrpc-perl libdbi-perl libmysqlclient15off libdbd-mysql-perl mysql-client-5.0  2>> $LOGFILE >> $LOGFILE

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

echo

echo "Installing MySQL from Ubuntu"
echo "Installing MySQL from Ubuntu" 2>> $LOGFILE >> $LOGFILE

apt-get -y --force-yes install mysql-server-5.0

echo

echo "Installing software from Ubuntu"
echo "Installing software from Ubuntu" 2>> $LOGFILE >> $LOGFILE

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

apt-get -y --force-yes install apache2 apache2-mpm-prefork build-essential iftop lame libmysqlclient15-dev libncurses5-dev libploticus0-dev libsox-fmt-all linux-source linux-headers-server mpg123 mysql-doc-5.0 mytop ntp openssh-server php5 php5-cli php5-dev php5-mysql ploticus screen sipsak sox subversion subversion-tools unzip bison flex libnewt-dev openssh-server libmcrypt4 php5-mcrypt libwww-curl-perl libcurl3 php5-curl 2>> $LOGFILE >> $LOGFILE

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

echo

echo "Installing phpmyadmin from Ubuntu"
echo "Installing phpmyadmin from Ubuntu" 2>> $LOGFILE >> $LOGFILE

apt-get -y --force-yes install phpmyadmin

echo

echo "Installing perl modules from Ubuntu"
echo "Installing perl modules from Ubuntu" 2>> $LOGFILE >> $LOGFILE
# install CPAN packages that are available through Ubuntu

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

apt-get -y --force-yes install libmd5-perl libdigest-sha1-perl libterm-readline-perl-perl libdbi-perl libnet-telnet-perl libnet-server-perl libunicode-map8-perl libjcode-perl libspreadsheet-writeexcel-perl libole-storage-lite-perl libspreadsheet-parseexcel-perl libcurses-perl libgetopt-mixed-perl libnet-domain-tld-perl libmail-sendmail-perl 2>> $LOGFILE >> $LOGFILE

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

echo

echo "Setting up Linux source code for driver compilation"
echo "Setting up Linux source code for driver compilation" 2>> $LOGFILE >> $LOGFILE

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

cd /usr/src/ 2>> $LOGFILE >> $LOGFILE
LNXSOURCE=`ls -1 linux-source-*.bz2 | tail -n 1` 2>> $LOGFILE >> $LOGFILE
tar -vxjf $LNXSOURCE 2>> $LOGFILE >> $LOGFILE
LNXSOURCEDIR=`echo * | tr " " "\n" | grep "linux-source" | grep -v "bz2"| tail -n 1` 2>> $LOGFILE >> $LOGFILE
mv $LNXSOURCEDIR linux-source-`uname -r` 2>> $LOGFILE >> $LOGFILE
ln -s linux-source-`uname -r` linux 2>> $LOGFILE >> $LOGFILE

cd /usr/src/linux 2>> $LOGFILE >> $LOGFILE
wget http://download.vicidial.com/ubuntu/wanpipe-linux-source.patch 2>> $LOGFILE >> $LOGFILE
patch -p1 Makefile wanpipe-linux-source.patch 2>> $LOGFILE >> $LOGFILE

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

echo
echo "================================================================="
echo
echo "Phase 2.2 - CPAN modules"
echo
echo "================================================================="

echo

echo "Installing CPAN modules"
echo "Installing CPAN modules" 2>> $LOGFILE >> $LOGFILE

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

# Give CPAN some silent settings; Make it as painless as possible
cd /tmp 2>> $LOGFILE >> $LOGFILE
wget http://download.vicidial.com/ubuntu/Config.pm 2>> $LOGFILE >> $LOGFILE
mv -f /tmp/Config.pm /etc/perl/CPAN/Config.pm 2>> $LOGFILE >> $LOGFILE

# Install CPAN modules
#cpan -i MD5 Digest::MD5 Digest::SHA1 readline
#cpan -i Bundle::CPAN
#cpan -fi Scalar::Util
#cpan -i DBI
#cpan -fi DBD::mysql
#cpan -fi Net::Telnet Time::HiRes Net::Server Switch Unicode::Map Jcode Spreadsheet::WriteExcel OLE::Storage_Lite Proc::ProcessTable IO::Scalar Spreadsheet::ParseExcel Curses Getopt::Long Net::Domain Mail::Sendmail


cpan -i Time::HiRes Switch Proc::ProcessTable IO::Scalar 2>> $LOGFILE >> $LOGFILE
#cpan -i Net::Domain
#echo "Just installed Net::Domain"
#sleep 4

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

echo
echo "================================================================="
echo
echo "Phase 2.3 - Compiled Tools"
echo
echo "We will now be downloading various software packages."
echo "They will then be compiled and installed."
echo "These packages are currently not available from Ubuntu or CPAN"
echo
echo "================================================================="


echo

echo "Downloading source code"
echo "Downloading source code" 2>> $LOGFILE >> $LOGFILE

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

mkdir /usr/src/tars 2>> $LOGFILE >> $LOGFILE
cd /usr/src/tars 2>> $LOGFILE >> $LOGFILE
wget http://download.vicidial.com/required-apps/asterisk-perl-0.08.tar.gz 2>> $LOGFILE >> $LOGFILE
wget http://download.vicidial.com/required-apps/ttyload-0.5.tar.gz 2>> $LOGFILE >> $LOGFILE
wget http://download.vicidial.com/required-apps/mtop-0.6.6.tar.gz 2>> $LOGFILE >> $LOGFILE
wget http://download.vicidial.com/required-apps/eaccelerator-0.9.5.3.tar.bz2 2>> $LOGFILE >> $LOGFILE

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

echo

echo "Extracting downloaded source code"
echo "Extracting downloaded source code" 2>> $LOGFILE >> $LOGFILE

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

cd /usr/src
tar -vxzf /usr/src/tars/ttyload-0.5.tar.gz 2>> $LOGFILE >> $LOGFILE
tar -vxzf /usr/src/tars/mtop-0.6.6.tar.gz 2>> $LOGFILE >> $LOGFILE
tar -vxzf /usr/src/tars/asterisk-perl-0.08.tar.gz 2>> $LOGFILE >> $LOGFILE
tar -vxjf /usr/src/tars/eaccelerator-0.9.5.3.tar.bz2 2>> $LOGFILE >> $LOGFILE

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

echo

echo "Compiling and installing asterisk-perl"
echo "Compiling and installing asterisk-perl" 2>> $LOGFILE >> $LOGFILE

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

cd /usr/src/asterisk-perl-0.08 2>> $LOGFILE >> $LOGFILE
perl Makefile.PL 2>> $LOGFILE >> $LOGFILE
make all 2>> $LOGFILE >> $LOGFILE
make install 2>> $LOGFILE >> $LOGFILE
cd ../

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

echo

echo "Compiling and installing ttyload"
echo "Compiling and installing ttyload" 2>> $LOGFILE >> $LOGFILE

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

cd /usr/src/ttyload-0.5 2>> $LOGFILE >> $LOGFILE
make 2>> $LOGFILE >> $LOGFILE
make install 2>> $LOGFILE >> $LOGFILE
cd ../

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

echo

echo "Compiling and installing eaccelerator"
echo "Compiling and installing eaccelerator" 2>> $LOGFILE >> $LOGFILE

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

cd /usr/src/eaccelerator-0.9.5.3
phpize 2>> $LOGFILE >> $LOGFILE
./configure 2>> $LOGFILE >> $LOGFILE
make 2>> $LOGFILE >> $LOGFILE
make install 2>> $LOGFILE >> $LOGFILE
mkdir /var/lib/eaccelerator 2>> $LOGFILE >> $LOGFILE
chmod 0755 /var/lib/eaccelerator 2>> $LOGFILE >> $LOGFILE
cd /etc/php5/conf.d 2>> $LOGFILE >> $LOGFILE
wget http://download.vicidial.com/ubuntu/eaccelerator.ini 2>> $LOGFILE >> $LOGFILE
cd /usr/src 2>> $LOGFILE >> $LOGFILE

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

echo

echo "Compiling and installing mtop"
echo "Compiling and installing mtop" 2>> $LOGFILE >> $LOGFILE

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

cd /usr/src/mtop-0.6.6 2>> $LOGFILE >> $LOGFILE
perl Makefile.PL 2>> $LOGFILE >> $LOGFILE
make 2>> $LOGFILE >> $LOGFILE
make install 2>> $LOGFILE >> $LOGFILE
cd ..

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

echo
echo "================================================================="
echo
echo "Phase 2.4 - Configuration"
echo
echo "We will now be configuring the prerequisite packages."
echo
echo "================================================================="

echo

echo "Configuring and restarting Apache2"
echo "Configuring and restarting Apache2" 2>> $LOGFILE >> $LOGFILE

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

cd /etc/apache2/conf.d 2>> $LOGFILE >> $LOGFILE
wget http://download.vicidial.com/ubuntu/vicirecord.conf 2>> $LOGFILE >> $LOGFILE

cd /etc/php5/apache2 2>> $LOGFILE >> $LOGFILE
rm php.ini 2>> $LOGFILE >> $LOGFILE
wget http://download.vicidial.com/ubuntu/php.ini 2>> $LOGFILE >> $LOGFILE

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

/etc/init.d/apache2 restart

echo

echo "Configuring and restarting NTP"
echo "Configuring and restarting NTP" 2>> $LOGFILE >> $LOGFILE

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

cd /etc
wget http://download.vicidial.com/ubuntu/ntp-server.conf 2>> $LOGFILE >> $LOGFILE
mv -f ntp-server.conf ntp.conf 2>> $LOGFILE >> $LOGFILE

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

/etc/init.d/ntp restart

echo "================================================================="
echo
echo "Phase 2 is complete. All of the prerequisites are installed!"
echo
echo "================================================================="

