#!/bin/sh
#
# Munch'D by Richard (richard@dynx.net)
# do a VICIDIALNOW install
# ** Please Support these OS Projects ** 
#
# astguiclient.sf.net
# vicidialnow.org
# old: gnudialer.org  new: dynx.net/gnudialer
# 
# wget http://www.dynx.net/ASTERISK/gnudialer/easy_setup/vicidialnow_overlay_gnudialer.sh
# (that is this file)
#
# chmod 755 vicidialnow_overlay_gnudialer.sh
#
# AND read the end of this file
#
# this install file is in no way complete
# 
# nor is it guaranteed in any way.  
# so if your system goes BOOM, thats your fault for running this.
#

echo "Running updatedb...(this takes a few moments)"
updatedb
yum -y install joe
yum -y install cdrecord
yum -y install mkisofs
yum -y install libtermcap-devel
yum -y install newt-devel

echo "Running updatedb...(this takes a few moments)"
updatedb

cd /usr/src

wget http://www.dynx.net/ASTERISK/gnudialer/easy_setup/create_tables.sql

mysql -uroot  < create_tables.sql

read -p "Enter the NEW MySQL root password: " mpass;
mysqladmin -uroot password $mpass
echo "MySQL root password has been set!"; sleep 2;

#wget http://prdownloads.sourceforge.net/webadmin/webmin-1.350-1.noarch.rpm
#rpm -i webmin-1.350-1.noarch.rpm

cd /usr/src
svn checkout http://dynx.net/svn/gnudialer-puff/trunk gnudialer-puff
cd gnudialer-puff
make clean
make
make install
make reload

cd /usr/src/gnudialer-puff/astcrm-1.1.6
wget http://www.dynx.net/ASTERISK/gnudialer/java/j2sdk-1_4_2_08-linux-i586.rpm
rpm -i j2sdk-1_4_2_08-linux-i586.rpm
export PATH=$PATH:/usr/java/j2sdk1.4.2_08/bin
export CLASSPATH=$CLASSPATH:.
make clean
make
make install

echo "Creating and Populating '/etc/gnudialer.conf'..."

cat > /etc/gnudialer.conf << EOF
gnudialer
dialer
1234
localhost
dialer
dialer
1234
localhost
5038
EOF

echo "Creating and Populating '/etc/gdhosts.conf'..."

cat > /etc/gdhosts.conf << EOF
localhost,5038,dialer,1234,1
EOF

echo "Copying run_gnudialer to '/usr/sbin/run_gnudialer'..."
cp /usr/src/gnudialer-puff/scripts/run_gnudialer /usr/sbin
chmod 755 /usr/sbin/run_gnudialer

echo "Copying patch files, and Patching Asterisk..."
cp /usr/src/gnudialer-puff/manager.c.both-patches.txt /usr/local/src/asterisk-1.2.24/manager.c.both-patches.txt
cp /usr/src/gnudialer-puff/channel.c.hangup_callerid_ast12.diff /usr/local/src/asterisk-1.2.24/channel.c.hangup_callerid_ast12.diff

cd /usr/local/src/asterisk-1.2.24

cp manager.c manager.c.org
cp channel.c channel.c.org

patch manager.c <manager.c.both-patches.txt
patch channel.c <channel.c.hangup_callerid_ast12.diff

make clean
make
make install

echo "Populating '/etc/asterisk/manager.conf'..."

cat >> /etc/asterisk/manager.conf << EOF

[dialer]
secret = 1234
read = system,call,log,verbose,command,agent,user
write = system,call,log,verbose,command,agent,user
 
[crm]
secret = 1234
read = call,agent,user
write = call,agent,user

EOF
echo "Adding run_gnudialer to rc.local..."

echo "/usr/sbin/run_gnudialer" >>/etc/rc.d/rc.local

echo "Adding additional firewall rules..."

iptables -I RH-Firewall-1-INPUT 11 -m state --state NEW -m tcp -p tcp --dport 1234 -j ACCEPT
iptables -I RH-Firewall-1-INPUT 11 -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
iptables -I RH-Firewall-1-INPUT 11 -m state --state NEW -m tcp -p tcp --dport 4445 -j ACCEPT
iptables -I RH-Firewall-1-INPUT 11 -m state --state NEW -m udp -p udp --dport 4569 -j ACCEPT
iptables -I RH-Firewall-1-INPUT 11 -m state --state NEW -m tcp -p tcp --dport 5036 -j ACCEPT
iptables -I RH-Firewall-1-INPUT 11 -m state --state NEW -m tcp -p tcp --dport 5038 -j ACCEPT
iptables -I RH-Firewall-1-INPUT 11 -m state --state NEW -m tcp -p tcp --dport 5432 -j ACCEPT
service iptables save
service iptables restart

echo "Backing up, and Adding dialer.conf include to extensions.conf..."
cp -R /etc/asterisk /etc/asterisk-config-bak
sed -i -e "s/\(.*writeprotect.*\)/\1\n#include \"dialer.conf\"/" /etc/asterisk/extensions.conf

cd /var/www/html
wget http://www.dynx.net/ASTERISK/gnudialer/easy_setup/vicidialnow_overlay_index.html
mv index.html index.html.org
mv vicidialnow_overlay_index.html index.html

echo "***************************************************************"
echo "Reminder, change your systems root password from the default!"
echo "***************************************************************"
echo "Sleeping for 30 seconds, then rebooting..."
sleep 30
shutdown -r now

# remember you need to still read all the /usr/src/gnudialer-puff/README's and INSTALL
# remember you need to still read all the /usr/src/gnudialer-puff/astcrm-1.1.6/README's and INSTALL
# you need to do this because you MUST change the user/pass in various areas,
# or use the defaults if you are UNSURE whats needs to change.

