#!/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
echo "PHASE 3 - Asterisk"
echo
echo "PHASE 3.1 - Downloading required software"
echo 
echo "PHASE 3.1 will start in 10 seconds."
echo
echo "================================================================="
wait_ten

echo

echo "Downloading the asterisk, libpri, zaptel, and wanpipe source"
echo "Downloading the asterisk, libpri, zaptel, and wanpipe source" 2>> $LOGFILE >> $LOGFILE

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

# Lets download our Asterisk and Wanpipe files
cd /usr/src/tars  2>> $LOGFILE >> $LOGFILE
wget http://downloads.digium.com/pub/asterisk/old-releases/asterisk-1.2.30.4.tar.gz 2>> $LOGFILE >> $LOGFILE
wget http://downloads.digium.com/pub/telephony/libpri/libpri-1.2.8.tar.gz 2>> $LOGFILE >> $LOGFILE
wget http://downloads.digium.com/pub/zaptel/releases/zaptel-1.2.27.tar.gz 2>> $LOGFILE >> $LOGFILE
wget http://downloads.digium.com/pub/telephony/sounds/asterisk-core-sounds-en-ulaw-current.tar.gz  2>> $LOGFILE >> $LOGFILE
wget http://downloads.digium.com/pub/telephony/sounds/asterisk-core-sounds-en-wav-current.tar.gz 2>> $LOGFILE >> $LOGFILE
wget http://downloads.digium.com/pub/telephony/sounds/asterisk-core-sounds-en-gsm-current.tar.gz 2>> $LOGFILE >> $LOGFILE
wget http://downloads.digium.com/pub/telephony/sounds/asterisk-extra-sounds-en-ulaw-current.tar.gz 2>> $LOGFILE >> $LOGFILE
wget http://downloads.digium.com/pub/telephony/sounds/asterisk-extra-sounds-en-wav-current.tar.gz 2>> $LOGFILE >> $LOGFILE
wget http://downloads.digium.com/pub/telephony/sounds/asterisk-extra-sounds-en-gsm-current.tar.gz 2>> $LOGFILE >> $LOGFILE
wget http://downloads.digium.com/pub/telephony/sounds/asterisk-moh-freeplay-gsm.tar.gz 2>> $LOGFILE >> $LOGFILE
wget http://downloads.digium.com/pub/telephony/sounds/asterisk-moh-freeplay-ulaw.tar.gz 2>> $LOGFILE >> $LOGFILE
wget http://downloads.digium.com/pub/telephony/sounds/asterisk-moh-freeplay-wav.tar.gz 2>> $LOGFILE >> $LOGFILE
wget ftp://ftp.sangoma.com/linux/current_wanpipe/wanpipe-3.2.7.1.tgz 2>> $LOGFILE >> $LOGFILE

disown $SPINPID
kill $SPINPID 2> /dev/null > /dev/null # spin no more

echo

echo "Downloading the asterisk patches"
echo "Downloading the asterisk patches" 2>> $LOGFILE >> $LOGFILE

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

# Lets download out Asterisk Patch
mkdir -p /usr/src/astguiclient/patches-1.2  2>> $LOGFILE >> $LOGFILE
cd /usr/src/astguiclient/patches-1.2  2>> $LOGFILE >> $LOGFILE
wget http://public.vicidial.com/conf/patches/app_amd2.c  2>> $LOGFILE >> $LOGFILE
wget http://public.vicidial.com/conf/patches/amd2.conf  2>> $LOGFILE >> $LOGFILE
wget http://public.vicidial.com/conf/patches/meetme_DTMF_passthru-1.2.23.patch  2>> $LOGFILE >> $LOGFILE
wget http://public.vicidial.com/conf/patches/meetme_volume_control_1.2.16.patch  2>> $LOGFILE >> $LOGFILE
wget http://public.vicidial.com/conf/patches/cli_chan_concise_delimiter.patch  2>> $LOGFILE >> $LOGFILE
wget http://public.vicidial.com/conf/patches/app_waitforsilence.c  2>> $LOGFILE >> $LOGFILE
wget http://public.vicidial.com/conf/patches/enter.h  2>> $LOGFILE >> $LOGFILE
wget http://public.vicidial.com/conf/patches/leave.h 2>> $LOGFILE >> $LOGFILE
wget http://public.vicidial.com/conf/patches/asterisk-gsm-1.2.patch 2>> $LOGFILE >> $LOGFILE
wget http://public.vicidial.com/conf/asterisk-gsm-asm.patch 2>> $LOGFILE >> $LOGFILE

disown $SPINPID
kill $SPINPID 2> /dev/null > /dev/null # spin no more

echo

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

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

cd /usr/src
tar -vxzf /usr/src/tars/asterisk-1.2.30.4.tar.gz  2>> $LOGFILE >> $LOGFILE
tar -vxzf /usr/src/tars/libpri-1.2.8.tar.gz 2>> $LOGFILE >> $LOGFILE
tar -vxzf /usr/src/tars/zaptel-1.2.27.tar.gz 2>> $LOGFILE >> $LOGFILE
tar -vxzf /usr/src/tars/wanpipe-3.2.7.1.tgz 2>> $LOGFILE >> $LOGFILE

disown $SPINPID
kill $SPINPID 2> /dev/null > /dev/null # spin no more

echo

echo "Patching Asterisk"
echo "Patching Asterisk" 2>> $LOGFILE >> $LOGFILE

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

cp /usr/src/astguiclient/patches-1.2/app_amd2.c /usr/src/asterisk-1.2.30.4/apps/app_amd.c 2>> $LOGFILE >> $LOGFILE
cp /usr/src/astguiclient/patches-1.2/amd2.conf /usr/src/asterisk-1.2.30.4/configs/amd.conf.sample 2>> $LOGFILE >> $LOGFILE
cp /usr/src/astguiclient/patches-1.2/app_waitforsilence.c /usr/src/asterisk-1.2.30.4/apps/app_waitforsilence.c 2>> $LOGFILE >> $LOGFILE
cp /usr/src/astguiclient/patches-1.2/enter.h /usr/src/asterisk-1.2.30.4/apps/enter.h 2>> $LOGFILE >> $LOGFILE
cp /usr/src/astguiclient/patches-1.2/leave.h /usr/src/asterisk-1.2.30.4/apps/leave.h 2>> $LOGFILE >> $LOGFILE

# Patch us up some files for Asterisk 1.2
cd /usr/src/asterisk-1.2.30.4/apps 2>> $LOGFILE >> $LOGFILE
patch -p1 app_meetme.c /usr/src/astguiclient/patches-1.2/meetme_DTMF_passthru-1.2.23.patch 2>> $LOGFILE >> $LOGFILE
patch -p1 app_meetme.c /usr/src/astguiclient/patches-1.2/meetme_volume_control_1.2.16.patch 2>> $LOGFILE >> $LOGFILE
cd /usr/src/asterisk-1.2.30.4/codecs/gsm 2>> $LOGFILE >> $LOGFILE
patch -p1 Makefile /usr/src/astguiclient/patches-1.2/asterisk-gsm-1.2.patch 2>> $LOGFILE >> $LOGFILE
cd inc 2>> $LOGFILE >> $LOGFILE
patch -p1 private.h /usr/src/astguiclient/patches-1.2/asterisk-gsm-asm.patch 2>> $LOGFILE >> $LOGFILE
cd /usr/src/asterisk-1.2.30.4 2>> $LOGFILE >> $LOGFILE
patch -p1 cli.c /usr/src/astguiclient/patches-1.2/cli_chan_concise_delimiter.patch 2>> $LOGFILE >> $LOGFILE
sed 's/app_mixmonitor.so app_stack.so/app_mixmonitor.so app_stack.so app_amd.so/' /usr/src/asterisk-1.2.30.4/apps/Makefile > /usr/src/asterisk-1.2.30.4/apps/Makefile.new
mv -f /usr/src/asterisk-1.2.30.4/apps/Makefile.new  /usr/src/asterisk-1.2.30.4/apps/Makefile 2>> $LOGFILE >> $LOGFILE

disown $SPINPID
kill $SPINPID 2> /dev/null > /dev/null # spin no more

echo
echo "================================================================="
echo
echo "PHASE 3.2 - Installing Asterisk Prereqs"
echo
echo "================================================================="

echo

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

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

cd /usr/src/libpri-1.2.8 2>> $LOGFILE >> $LOGFILE
make clean 2>> $LOGFILE >> $LOGFILE
make 2>> $LOGFILE >> $LOGFILE
make install 2>> $LOGFILE >> $LOGFILE

disown $SPINPID
kill $SPINPID 2> /dev/null > /dev/null # spin no more
echo

echo

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

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

cd /usr/src/zaptel-1.2.27 2>> $LOGFILE >> $LOGFILE
make clean 2>> $LOGFILE >> $LOGFILE
./configure 2>> $LOGFILE >> $LOGFILE
make 2>> $LOGFILE >> $LOGFILE
make install 2>> $LOGFILE >> $LOGFILE
make zttool 2>> $LOGFILE >> $LOGFILE
rm /etc/init.d/zaptel 2>> $LOGFILE >> $LOGFILE
rm /etc/rc.d/init.d/zaptel 2>> $LOGFILE >> $LOGFILE

disown $SPINPID
kill $SPINPID 2> /dev/null > /dev/null # spin no more

echo

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

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

cd /usr/src 2>> $LOGFILE >> $LOGFILE
ln -s zaptel-1.2.27 zaptel 2>> $LOGFILE >> $LOGFILE
cd /usr/src/wanpipe-3.2.7.1 2>> $LOGFILE >> $LOGFILE
wget http://download.vicidial.com/ubuntu/wanpipe-kern-ver.patch 2>> $LOGFILE >> $LOGFILE
patch -p1 Setup wanpipe-kern-ver.patch 2>> $LOGFILE >> $LOGFILE
./Setup install --silent --protocol=TDM --with-zaptel=/usr/src/zaptel 2>> $LOGFILE >> $LOGFILE

disown $SPINPID
kill $SPINPID 2> /dev/null > /dev/null # spin no more

echo
echo "================================================================="
echo
echo "PHASE 3.3 - Compiling Installing Asterisk"
echo
echo "================================================================="

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

cd /usr/src/asterisk-1.2.30.4 2>> $LOGFILE >> $LOGFILE
make clean 2>> $LOGFILE >> $LOGFILE
make 2>> $LOGFILE >> $LOGFILE
make install 2>> $LOGFILE >> $LOGFILE
make samples 2>> $LOGFILE >> $LOGFILE

disown $SPINPID
kill $SPINPID 2> /dev/null > /dev/null # spin no more

echo
echo "================================================================="
echo
echo "PHASE 3.4 - Configuring Asterisk for high-load"
echo
echo "================================================================="

echo

echo "Setting up in RAM recording"
echo "Setting up in RAM recording" 2>> $LOGFILE >> $LOGFILE

# Do some Asterisk/ViciDial high-load friendly things
echo "tmpfs   /var/spool/asterisk/monitor       tmpfs      rw                    0 0" >> /etc/fstab

echo

echo "Decompressing native format sound files"
echo "Decompressing native format sound files" 2>> $LOGFILE >> $LOGFILE


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

cd /var/lib/asterisk/sounds 2>> $LOGFILE >> $LOGFILE
tar -vxzf /usr/src/tars/asterisk-core-sounds-en-gsm-current.tar.gz 2>> $LOGFILE >> $LOGFILE
tar -vxzf /usr/src/tars/asterisk-core-sounds-en-ulaw-current.tar.gz 2>> $LOGFILE >> $LOGFILE
tar -vxzf /usr/src/tars/asterisk-core-sounds-en-wav-current.tar.gz 2>> $LOGFILE >> $LOGFILE
tar -vxzf /usr/src/tars/asterisk-extra-sounds-en-gsm-current.tar.gz 2>> $LOGFILE >> $LOGFILE
tar -vxzf /usr/src/tars/asterisk-extra-sounds-en-ulaw-current.tar.gz 2>> $LOGFILE >> $LOGFILE
tar -vxzf /usr/src/tars/asterisk-extra-sounds-en-wav-current.tar.gz 2>> $LOGFILE >> $LOGFILE
wget http://download.vicidial.com/ubuntu/conf.gsm 2>> $LOGFILE >> $LOGFILE

disown $SPINPID
kill $SPINPID 2> /dev/null > /dev/null # spin no more

echo

echo "Installing native format hold music"
echo "Installing native format hold music" 2>> $LOGFILE >> $LOGFILE

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

sox conf.gsm conf.wav 2>> $LOGFILE >> $LOGFILE
sox conf.gsm -t ul conf.ulaw 2>> $LOGFILE >> $LOGFILE
cp conf.gsm park.gsm 2>> $LOGFILE >> $LOGFILE
cp conf.ulaw park.ulaw 2>> $LOGFILE >> $LOGFILE
cp conf.wav park.wav 2>> $LOGFILE >> $LOGFILE
cd /var/lib/asterisk/mohmp3 2>> $LOGFILE >> $LOGFILE
tar -vxzf /usr/src/tars/asterisk-moh-freeplay-gsm.tar.gz 2>> $LOGFILE >> $LOGFILE
tar -vxzf /usr/src/tars/asterisk-moh-freeplay-ulaw.tar.gz 2>> $LOGFILE >> $LOGFILE
tar -vxzf /usr/src/tars/asterisk-moh-freeplay-wav.tar.gz 2>> $LOGFILE >> $LOGFILE
mkdir /var/lib/asterisk/quiet-mp3 2>> $LOGFILE >> $LOGFILE
cd /var/lib/asterisk/quiet-mp3 2>> $LOGFILE >> $LOGFILE
sox ../mohmp3/fpm-calm-river.wav fpm-calm-river.wav vol 0.25 2>> $LOGFILE >> $LOGFILE
sox ../mohmp3/fpm-calm-river.gsm fpm-calm-river.gsm vol 0.25 2>> $LOGFILE >> $LOGFILE
sox -t ul -r 8000 -c 1 ../mohmp3/fpm-calm-river.ulaw -t ul fpm-calm-river.ulaw vol 0.25 2>> $LOGFILE >> $LOGFILE
sox ../mohmp3/fpm-sunshine.wav fpm-sunshine.wav vol 0.25 2>> $LOGFILE >> $LOGFILE
sox ../mohmp3/fpm-sunshine.gsm fpm-sunshine.gsm vol 0.25 2>> $LOGFILE >> $LOGFILE
sox -t ul -r 8000 -c 1 ../mohmp3/fpm-sunshine.ulaw -t ul fpm-sunshine.ulaw vol 0.25 2>> $LOGFILE >> $LOGFILE
sox ../mohmp3/fpm-world-mix.wav fpm-world-mix.wav vol 0.25 2>> $LOGFILE >> $LOGFILE
sox ../mohmp3/fpm-world-mix.gsm fpm-world-mix.gsm vol 0.25 2>> $LOGFILE >> $LOGFILE
sox -t ul -r 8000 -c 1 ../mohmp3/fpm-world-mix.ulaw -t ul fpm-world-mix.ulaw vol 0.25 2>> $LOGFILE >> $LOGFILE

disown $SPINPID
kill $SPINPID 2> /dev/null > /dev/null # spin no more

echo
echo "================================================================="
echo
echo "PHASE 3 Is complete. Asterisk is installed."
echo
echo "================================================================="

