#!/usr/bin/env bash

VER_ASTERISK="1.8.21.0";
VER_DAHDI="2.6.2+2.6.2";
VER_LIBPRI="1.4.14";
VER_FREEPBX="2.9.0";
VER_BENCHFAX="1.1.0";

if [ "$UID" = "0" ]; then
echo "##################################################################"
echo "#                Auto Updater für SCS VoIPgate 2.x               #"
echo "#                              mit                               #"
echo "         Asterisk ${VER_ASTERISK} - FreePBX ${VER_FREEPBX} - SCS-Firewall   "
echo "#                                                                #"
echo "#                Marcel Binder - mbinder@scscom.ch               #"
echo "#                  http://www.voipgate.ch/asterisk/              #"
echo "#                     http://www.scscom.ch                       #"
echo "### ---------------------------------------------------------- ###"
echo "#                    Drücke <enter> für weiter                   #"
echo "##################################################################"
read

if ( test -f /usr/local/fastmenu-data/voip-check.sh ) ; then
echo "+----------------------------------------------------------------+"
echo "| Deaktivire VoIP-Checker                                        |"
echo "+----------------------------------------------------------------+"
read -t1
sed -i '/Checker.sh/d' /etc/crontab
sed -i '/voip-check.sh/d' /etc/crontab
fi


echo "+----------------------------------------------------------------+"
echo "| Install... alle benötigte Pakette per APT-GET                  |"
echo "+----------------------------------------------------------------+"
read -t1
apt-get update
apt-get -y upgrade
apt-get -y install build-essential autoconf automake libtool flex bison libssl-dev libnewt-dev libncurses5-dev linux-source-2.6 kernel-package make g++
apt-get -y install linux-headers-`uname -r`
apt-get -y install sox curl subversion subversion-tools mysql-client mysql-server libmysqlclient-dev postfix mpg123 libsqlite0-dev sudo
apt-get -y install php5 php5-cli php5-curl php5-gd php5-json php5-mcrypt php5-mhash php5-mysql libtiff-tools libtiff4 libtiff4-dev libssl-ocaml-dev bind9 ghostscript
apt-get -y install apache2 apache2-mpm-prefork libxml2 libapache2-mod-php5 libxml2-dev php-pear php5-suhosin php5-xmlrpc php5-xsl php-pear python-pyopenssl libusb-dev fxload
apt-get -y install libcurl4-openssl-dev
apt-get -y install 

echo "+----------------------------------------------------------------+"
echo "| Download sources auf /tmp/asterisk/ + /tmp/freepbx             |"
echo "+----------------------------------------------------------------+"
read -t1
mkdir /tmp/asterisk/
wget http://www.voipgate.ch/asterisk/archiv/asterisk-${VER_ASTERISK}.tar.gz -O /tmp/asterisk/asterisk-${VER_ASTERISK}.tar.gz
wget http://www.voipgate.ch/asterisk/archiv/dahdi-linux-complete-${VER_DAHDI}.tar.gz -O /tmp/asterisk/dahdi-linux-complete-${VER_DAHDI}.tar.gz
wget http://www.voipgate.ch/asterisk/archiv/libpri-${VER_LIBPRI}.tar.gz -O /tmp/asterisk/libpri-${VER_LIBPRI}.tar.gz
wget http://www.voipgate.ch/asterisk/archiv/debian-multimedia-keyring_2010.12.26_all.deb -O /tmp/asterisk/debian-multimedia-keyring_all.deb
wget http://www.voipgate.ch/asterisk/archiv/menuselect.makedeps -O /tmp/asterisk/menuselect.makedeps
wget http://www.voipgate.ch/asterisk/archiv/menuselect.makeopts -O /tmp/asterisk/menuselect.makeopts
wget http://www.voipgate.ch/asterisk/archiv/scs-design.tgz -O /tmp/scs-design.tgz
wget http://www.voipgate.ch/asterisk/archiv/tel-ch -O /var/www/tel-ch.php
mkdir /tmp/freepbx/
wget http://www.voipgate.ch/asterisk/archiv/freepbx-${VER_FREEPBX}.tar.gz -O /tmp/freepbx/freepbx-${VER_FREEPBX}.tar.gz
#wget http://www.voipgate.ch/asterisk/archiv/freepbx-${VER_FREEPBX}-amportal.conf -O /etc/amportal.conf
#mkdir /root/asterisk-addons
#wget http://www.voipgate.ch/asterisk/archiv/register -O /root/asterisk-addons/register
#wget http://www.voipgate.ch/asterisk/archiv/benchfax-${VER_BENCHFAX}-x86_32 -O /root/asterisk-addons/benchfax
#wget http://www.voipgate.ch/asterisk/archiv/benchg729-${VER_BENCHG729}-x86_32 -O /root/asterisk-addons/benchg729
#chmod +x /root/asterisk-addons/register
#chmod +x /root/asterisk-addons/benchfax
#chmod +x /root/asterisk-addons/benchg729

clear
echo "##################################################################"
echo "# Asterisk installation startet jetzt                            #"
echo "##################################################################"
read -t1

echo "+----------------------------------------------------------------+"
echo "| Stop Asterisk                                                  |"
echo "+----------------------------------------------------------------+"
read -t1
/etc/init.d/freepbx stop

echo "+----------------------------------------------------------------+"
echo "| Dekomrimiere sources in /usr/src/                              |"
echo "+----------------------------------------------------------------+"
read -t1
cd /usr/src/
tar zxvf /tmp/asterisk/asterisk-${VER_ASTERISK}.tar.gz
tar zxvf /tmp/asterisk/dahdi-linux-complete-${VER_DAHDI}.tar.gz
tar zxvf /tmp/asterisk/libpri-${VER_LIBPRI}.tar.gz
mv /tmp/asterisk/menuselect.makedeps /usr/src/asterisk-${VER_ASTERISK}/menuselect.makedeps
mv /tmp/asterisk/menuselect.makeopts /usr/src/asterisk-${VER_ASTERISK}/menuselect.makeopts
echo "+----------------------------------------------------------------+"
echo "| Kompiliere DAHDI                                               |"
echo "+----------------------------------------------------------------+"
read -t1
cd /usr/src/dahdi-linux-complete-${VER_DAHDI}/linux/
make
make install
cd /usr/src/dahdi-linux-complete-${VER_DAHDI}/tools/
./configure
make
make install
make config
echo "+----------------------------------------------------------------+"
echo "| Kompiliere libPRI                                              |"
echo "+----------------------------------------------------------------+"
read -t1
cd cd /usr/src/libpri-${VER_LIBPRI}/
make
make install
echo "+----------------------------------------------------------------+"
echo "| Installiere MP3 Modul                                          |"
echo "+----------------------------------------------------------------+"
read -t1
echo "deb http://www.deb-multimedia.org squeeze main non-free" >> /etc/apt/sources.list
dpkg -i /tmp/asterisk/debian-multimedia-keyring_all.deb
apt-get update
apt-get -y install lame libmp3lame-dev
cd /usr/src/asterisk-${VER_ASTERISK}
contrib/scripts/get_mp3_source.sh

echo "+----------------------------------------------------------------+"
echo "| Kompiliere Asterisk                                            |"
echo "+----------------------------------------------------------------+"
read -t1
cd /usr/src/asterisk-${VER_ASTERISK}
./configure
# menu config von MenuSelect
#make menuconfig
#echo "COPY menuselect"
#read
make
make install

echo "+----------------------------------------------------------------+"
echo "| Depmod (Modules)                                               |"
echo "+----------------------------------------------------------------+"
read -t1
depmod

echo "+----------------------------------------------------------------+"
echo "| Starte Asterisk und FreePBX                                    |"
echo "+----------------------------------------------------------------+"
read -t1
/etc/init.d/freepbx start

echo "##################################################################"
echo "# Asterisk installation : Fertig                                  #"
echo "##################################################################"
read -t2


FREE_CHECK=`cat /var/www/admin/modules/core/module.xml | grep -om1 2.9.`;
if ! [ "$FREE_CHECK" = "2.9." ]; then
key=""
while [ "$key" != "y" ] && [ "$key" != "n" ];do
read -n 1 -p " FreePBX Updaten auf 2.9.x ? [y/n]" key
done
if [ $key == "y" ]; then
clear
echo "##################################################################"
echo "# FreePBX Update                                                 #"
echo "##################################################################"
read -t1

echo "+----------------------------------------------------------------+"
echo "| Dekomprimiere FreePBX sources in /usr/src/                     |"
echo "+----------------------------------------------------------------+"
read -t1
cd /usr/src
tar zxvf /tmp/freepbx/freepbx-${VER_FREEPBX}.tar.gz

echo "+----------------------------------------------------------------+"
echo "| Lade Asterisk Post Installation                                |"
echo "+----------------------------------------------------------------+"
read -t1
/usr/src/freepbx-${VER_FREEPBX}/start_asterisk start

echo "+----------------------------------------------------------------+"
echo "| Installation FreePBX                                           |"
echo "+----------------------------------------------------------------+"
read -t1
mv /etc/asterisk/sip_notify.conf /etc/asterisk/sip_notify.conf.backup
cd /usr/src/freepbx-${VER_FREEPBX}/
./install_amp --force-overwrite
/usr/src/freepbx-${VER_FREEPBX}/apply_conf.sh

echo "+----------------------------------------------------------------+"
echo "| Installire script (Asterisk+FrePBX)                            |"
echo "| /etc/init.d/freepbx [start|stop|restart]                       |"
echo "+----------------------------------------------------------------+"
read -t1

update-rc.d -f freepbx remove
rm /etc/init.d/freepbx
cp /usr/local/sbin/amportal /etc/init.d/freepbx
chmod +x /etc/init.d/freepbx

sed -i "/env bash/r"<(echo "### END INIT INFO") /etc/init.d/freepbx
sed -i "/env bash/r"<(echo "# Description:       VoIP Dienst") /etc/init.d/freepbx
sed -i "/env bash/r"<(echo "# Short-Description: start und stop SCS-VoIPgate") /etc/init.d/freepbx
sed -i "/env bash/r"<(echo "# Default-Stop:      0 1 6") /etc/init.d/freepbx
sed -i "/env bash/r"<(echo "# Default-Start:     2 3 4 5") /etc/init.d/freepbx
sed -i "/env bash/r"<(echo "# Should-Stop:       postfix mysql dahdi") /etc/init.d/freepbx
sed -i "/env bash/r"<(echo "# Should-Start:      postfix mysql dahdi") /etc/init.d/freepbx
sed -i "/env bash/r"<(echo '# Required-Stop:     $local_fs $remote_fs $syslog $named $network') /etc/init.d/freepbx
sed -i "/env bash/r"<(echo '# Required-Start:    $local_fs $remote_fs $syslog $named $network $time') /etc/init.d/freepbx
sed -i "/env bash/r"<(echo "# Provides:          voipgate") /etc/init.d/freepbx
sed -i "/env bash/r"<(echo "### BEGIN INIT INFO") /etc/init.d/freepbx

echo "Update services loading at boot time..."
update-rc.d freepbx defaults

echo "Starting FreePBX"
/etc/init.d/freepbx restart

echo "Reload Config"
/var/lib/asterisk/bin/module_admin reload

echo "Restart FreePBX"
/etc/init.d/freepbx restart

cd /
tar zxvf /tmp/scs-design.tgz

fi
fi


if ( test -f /usr/local/fastmenu-data/voip-check.sh ) ; then
echo "+----------------------------------------------------------------+"
echo "| Aktivire VoIP-Checker wieder                                   |"
echo "+----------------------------------------------------------------+"
read -t1
echo "# Asterisk RUN/Carrier Checker.sh. Der alle Minute überprüft" >> /etc/crontab
echo "*/1 *  * * *  root  /usr/local/fastmenu-data/voip-check.sh >/dev/null 2>&1" >> /etc/crontab
echo " Install Asterisk Checker OK"
fi

echo "+----------------------------------------------------------------+"
echo "| Ändere Welcome Login Fenster (motd.tail)                       |"
echo "+----------------------------------------------------------------+"
read -t1
echo "" > /etc/motd.tail
echo "========================================================" >> /etc/motd.tail
echo "-           Welcome to the Asterisk Server             -" >> /etc/motd.tail
echo "-                                                      -" >> /etc/motd.tail
echo "-        Asterisk ${VER_ASTERISK}   /   FreePBX ${VER_FREEPBX}         -" >> /etc/motd.tail
echo "-                                                      -" >> /etc/motd.tail
echo "-      Support: 044 / 744 60 60   www.scscom.ch        -" >> /etc/motd.tail
echo "========================================================" >> /etc/motd.tail
echo "" >> /etc/motd.tail
echo "For Fastmenu type: mm" >> /etc/motd.tail
clear
echo ""
echo " !Update abgeschlossen! Für 100% Funktion bitte System Rebooten."
else
echo "DU must als root eingeloggt sein."
fi
