#!/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 Pre-Requisites 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

# Lets go to a safe-place to start with
cd /tmp

# Install packages available from YaST
zypper ar http://ftp.skynet.be/pub/packman/suse/11.1/Packman.repo
zypper ar http://download.opensuse.org/repositories/server:/database/openSUSE_11.1/server:database.repo
zypper ar http://download.opensuse.org/repositories/server:/php:/extensions/openSUSE_11.1/server:php:extensions.repo
echo
echo "Please type y and hit Enter to the accept Key and Import prompts"
echo
zypper refresh
zypper --non-interactive up --auto-agree-with-licenses
zypper --non-interactive up --auto-agree-with-licenses # Since we need to restart to load new libzypp
zypper --non-interactive in -t pattern lamp_server # Making life simple for installers
zypper --non-interactive in --repo server_database mysql libmysqlclient-devel # We want latest and stablest MySQL!
zypper --non-interactive in pico nano findutils-locate mc lame sox mytop iftop screen php5-devel mpg123 phpMyAdmin perl-DBD-mysql newt newt-devel irqbalance curl zip unzip php5-eaccelerator http://public.vicidial.com/conf/package/sipsak-0.9.6-2.1.i586.rpm http://public.vicidial.com/conf/package/ploticus-2.4.0-6.2.i586.rpm

# Give CPAN some silent settings; Make it as painless as possible
cd /tmp
wget http://public.vicidial.com/conf/Config.pm
mv -f /tmp/Config.pm /usr/lib/perl5/5.10.0/CPAN/Config.pm

# 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

# Download packages not available through CPAN
mkdir /usr/src/tars
cd /usr/src/tars
wget http://asterisk.gnuinter.net/files/asterisk-perl-0.08.tar.gz
wget http://www.daveltd.com/src/util/ttyload/ttyload-0.4.4.tar.gz
wget http://internap.dl.sourceforge.net/sourceforge/mtop/mtop-0.6.6.tar.gz

# Extract downloaded sources
cd /usr/src
tar -xzf /usr/src/tars/ttyload-0.4.4.tar.gz
tar -xzf /usr/src/tars/mtop-0.6.6.tar.gz
tar -xzf /usr/src/tars/asterisk-perl-0.08.tar.gz

## Install Packages
# Asterisk Perl
cd /usr/src/asterisk-perl-0.08
perl Makefile.PL
make all
make install
cd ../

# TTY Load
cd /usr/src/ttyload-0.4.4
make
make install
cd ../

# E Accelerator
cd /etc/php5/conf.d
rm eaccelerator.ini
wget http://public.vicidial.com/conf/eaccelerator.ini
cd /usr/src

# MTop
cd /usr/src/mtop-0.6.6
perl Makefile.PL
make
make install
cd ..

## Time to install supporting configuration files
# Install ViciDial Recordings .conf file for apache
cd /etc/apache2/conf.d
wget http://public.vicidial.com/conf/vicirecord.conf

# Install ViciDial php.ini for apache
cd /etc/php5/apache2
rm php.ini
wget http://public.vicidial.com/conf/php.ini

# Restart apache incase it's running
/etc/init.d/apache2 restart

# Install modified ntp.conf file and restart
cd /etc
wget http://public.vicidial.com/conf/ntp-server.conf
mv -f ntp-server.conf ntp.conf
/etc/init.d/ntp restart
chkconfig ntp 35

echo
echo "All Done! Pre Requisites are Installed!"
echo

fi
