#!/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 installer script for Asterisk v.1.4 on OpenSuSE v.11.0"
echo
choice "Do you want to continue? (N/y) : "

if [ "$CHOICE" == "y" ]; then # All code to run before here

echo
choice "Do you have a Sangoma T1 card installed? (N/y) : "
SANGOMA="$CHOICE"
echo
choice "Do you have a Sangoma VoiceTime USB timer installed? (N/y) : "
SANGOMAVT="$CHOICE"

# Lets download our Asterisk and Wanpipe files
cd /usr/src/tars
#wget http://downloads.digium.com/pub/asterisk/old-releases/asterisk-1.4.21.2.tar.gz
#wget http://downloads.digium.com/pub/libpri/releases/libpri-1.4.9.tar.gz
#wget http://downloads.digium.com/pub/zaptel/releases/zaptel-1.4.12.1.tar.gz
#wget http://downloads.digium.com/pub/telephony/sounds/asterisk-core-sounds-en-ulaw-current.tar.gz
#wget http://downloads.digium.com/pub/telephony/sounds/asterisk-core-sounds-en-wav-current.tar.gz
#wget http://downloads.digium.com/pub/telephony/sounds/asterisk-core-sounds-en-gsm-current.tar.gz
#wget http://downloads.digium.com/pub/telephony/sounds/asterisk-extra-sounds-en-ulaw-current.tar.gz
#wget http://downloads.digium.com/pub/telephony/sounds/asterisk-extra-sounds-en-wav-current.tar.gz
#wget http://downloads.digium.com/pub/telephony/sounds/asterisk-extra-sounds-en-gsm-current.tar.gz
#wget http://downloads.digium.com/pub/telephony/sounds/asterisk-moh-freeplay-gsm.tar.gz
#wget http://downloads.digium.com/pub/telephony/sounds/asterisk-moh-freeplay-ulaw.tar.gz
#wget http://downloads.digium.com/pub/telephony/sounds/asterisk-moh-freeplay-wav.tar.gz
#wget ftp://ftp.sangoma.com/linux/current_wanpipe/wanpipe-3.2.7.1.tgz
#wget ftp://ftp.sangoma.com/linux/wanpipe_voicetime/wanpipe-voicetime-1.0.7.tgz

# Lets download out Asterisk Patch
mkdir -p /usr/src/astguiclient/patches-1.4
cd /usr/src/astguiclient/patches-1.4
wget http://public.vicidial.com/conf/patches/enter.h
wget http://public.vicidial.com/conf/patches/leave.h
wget http://public.vicidial.com/conf/patches/asterisk-gsm-1.4.patch
wget http://public.vicidial.com/conf/patches/asterisk-gsm-asm.patch

# Uncompress files for install
cd /usr/src
tar -xzf /usr/src/tars/asterisk-1.4.21.2.tar.gz
tar -xzf /usr/src/tars/libpri-1.4.9.tar.gz
tar -xzf /usr/src/tars/zaptel-1.4.12.1.tar.gz
tar -xzf /usr/src/tars/wanpipe-3.2.7.1.tgz
tar -xzf /usr/src/tars/wanpipe-voicetime-1.0.7.tgz
echo "Finished!"

# Place files into source tree where applicable
echo -n "Moving add-on files to Asterisk Source Tree... "
cp /usr/src/astguiclient/patches-1.4/enter.h /usr/src/asterisk-1.4.21.2/apps/enter.h
cp /usr/src/astguiclient/patches-1.4/leave.h /usr/src/asterisk-1.4.21.2/apps/leave.h
echo "Done!"

# Patch us up some files for Asterisk 1.4
echo "Patching Asterisk"
cd /usr/src/asterisk-1.4.21.2/codecs/gsm
patch -p1 Makefile /usr/src/astguiclient/patches-1.4/asterisk-gsm-1.4.patch
cd inc
patch -p1 private.h /usr/src/astguiclient/patches-1.4/asterisk-gsm-asm.patch
echo "Patching Done!"

# Lets compile some source now, starting with libPRI
echo "Now starting source compile"

cd /usr/src/libpri-1.4.9
make clean
make
make install

# Compile Zaptel
cd /usr/src/zaptel-1.4.12.1
make clean
./configure
make
make install
make zttool
rm /etc/init.d/zaptel
rm /etc/rc.d/init.d/zaptel

if [ "$SANGOMA" == "y" ]; then
# Compile Wanpipe on top of Zaptel/libPRI
cd /usr/src/wanpipe-3.2.7.1
./Setup install --silent --protocol=TDM
fi

if [ "$SANGOMAVT" == "y" ]; then
# Install some VoiceTime!
cd /usr/src/wanpipe-voicetime-1.0.7
make ZAPDIR=/usr/src/zaptel-1.4.12.1/kernel
make install
fi

# Compile Asterisk finally
cd /usr/src/asterisk-1.4.21.2
make clean
./configure
make
make install
make samples

echo "Finished Compiling"

# Do some Asterisk/ViciDial high-load friendly things
echo -n "Doing Sound installation for WAV ULAW GSM formats... "
echo "tmpfs   /var/spool/asterisk/monitor       tmpfs      rw                    0 0" >> /etc/fstab
cd /var/lib/asterisk/sounds
tar -xzf /usr/src/tars/asterisk-core-sounds-en-gsm-current.tar.gz
tar -xzf /usr/src/tars/asterisk-core-sounds-en-ulaw-current.tar.gz
tar -xzf /usr/src/tars/asterisk-core-sounds-en-wav-current.tar.gz
tar -xzf /usr/src/tars/asterisk-extra-sounds-en-gsm-current.tar.gz
tar -xzf /usr/src/tars/asterisk-extra-sounds-en-ulaw-current.tar.gz
tar -xzf /usr/src/tars/asterisk-extra-sounds-en-wav-current.tar.gz
wget http://public.vicidial.com/conf/conf.gsm
echo "Done!"

echo -n "Converting sounds to support ULAW, WAV, and GSM formats... "
sox conf.gsm conf.wav
sox conf.gsm -t ul conf.ulaw
cp conf.gsm park.gsm
cp conf.ulaw park.ulaw
cp conf.wav park.wav
echo -n "25% "
cd /var/lib/asterisk
ln -s moh mohmp3
mkdir mohmp3
cd mohmp3
tar -xzf /usr/src/tars/asterisk-moh-freeplay-gsm.tar.gz
tar -xzf /usr/src/tars/asterisk-moh-freeplay-ulaw.tar.gz
tar -xzf /usr/src/tars/asterisk-moh-freeplay-wav.tar.gz
mkdir /var/lib/asterisk/quiet-mp3
echo -n "50% "
cd /var/lib/asterisk/quiet-mp3
sox ../mohmp3/fpm-calm-river.wav fpm-calm-river.wav vol 0.25
sox ../mohmp3/fpm-calm-river.gsm fpm-calm-river.gsm vol 0.25
sox -t ul -r 8000 -c 1 ../mohmp3/fpm-calm-river.ulaw -t ul fpm-calm-river.ulaw vol 0.25
sox ../mohmp3/fpm-sunshine.wav fpm-sunshine.wav vol 0.25
sox ../mohmp3/fpm-sunshine.gsm fpm-sunshine.gsm vol 0.25
echo -n "75% "
sox -t ul -r 8000 -c 1 ../mohmp3/fpm-sunshine.ulaw -t ul fpm-sunshine.ulaw vol 0.25
sox ../mohmp3/fpm-world-mix.wav fpm-world-mix.wav vol 0.25
sox ../mohmp3/fpm-world-mix.gsm fpm-world-mix.gsm vol 0.25
sox -t ul -r 8000 -c 1 ../mohmp3/fpm-world-mix.ulaw -t ul fpm-world-mix.ulaw vol 0.25
echo "Finished!"
echo
echo "OK! Asterisk should be patched and installed for ViciDial!"

fi
