#!/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 scrip for Asterisk v.1.2 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"


# Lets download our Asterisk and Wanpipe files
cd /usr/src/tars
wget http://downloads.digium.com/pub/asterisk/old-releases/asterisk-1.2.30.4.tar.gz
wget http://downloads.digium.com/pub/telephony/libpri/libpri-1.2.8.tar.gz
wget http://downloads.digium.com/pub/zaptel/releases/zaptel-1.2.27.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

# Lets download out Asterisk Patch
mkdir -p /usr/src/astguiclient/patches-1.2
cd /usr/src/astguiclient/patches-1.2
wget http://public.vicidial.com/conf/patches/app_amd2.c
wget http://public.vicidial.com/conf/patches/amd2.conf
wget http://public.vicidial.com/conf/patches/meetme_DTMF_passthru-1.2.23.patch
wget http://public.vicidial.com/conf/patches/meetme_volume_control_1.2.16.patch
wget http://public.vicidial.com/conf/patches/cli_chan_concise_delimiter.patch
wget http://public.vicidial.com/conf/patches/app_waitforsilence.c
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.2.patch
wget http://public.vicidial.com/conf/asterisk-gsm-asm.patch

# Uncompress files for install
cd /usr/src
tar -xzf /usr/src/tars/asterisk-1.2.30.4.tar.gz
tar -xzf /usr/src/tars/libpri-1.2.8.tar.gz
tar -xzf /usr/src/tars/zaptel-1.2.27.tar.gz
tar -xzf /usr/src/tars/wanpipe-3.2.7.1.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.2/app_amd2.c /usr/src/asterisk-1.2.30.4/apps/app_amd.c
cp /usr/src/astguiclient/patches-1.2/amd2.conf /usr/src/asterisk-1.2.30.4/configs/amd.conf.sample
cp /usr/src/astguiclient/patches-1.2/app_waitforsilence.c /usr/src/asterisk-1.2.30.4/apps/app_waitforsilence.c
cp /usr/src/astguiclient/patches-1.2/enter.h /usr/src/asterisk-1.2.30.4/apps/enter.h
cp /usr/src/astguiclient/patches-1.2/leave.h /usr/src/asterisk-1.2.30.4/apps/leave.h
echo "Done!"

# Patch us up some files for Asterisk 1.2
echo "Patching Asterisk"
cd /usr/src/asterisk-1.2.30.4/apps
patch -p1 app_meetme.c /usr/src/astguiclient/patches-1.2/meetme_DTMF_passthru-1.2.23.patch
patch -p1 app_meetme.c /usr/src/astguiclient/patches-1.2/meetme_volume_control_1.2.16.patch
cd /usr/src/asterisk-1.2.30.4/codecs/gsm
patch -p1 Makefile /usr/src/astguiclient/patches-1.2/asterisk-gsm-1.2.patch
cd inc
patch -p1 private.h /usr/src/astguiclient/patches-1.2/asterisk-gsm-asm.patch
cd /usr/src/asterisk-1.2.30.4
patch -p1 cli.c /usr/src/astguiclient/patches-1.2/cli_chan_concise_delimiter.patch
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
echo "Patching Done!"

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

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

# Compile Zaptel
cd /usr/src/zaptel-1.2.27
make clean
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
/usr/src/wanpipe-3.2.7.1/Setup install --silent --protocol=TDM
fi

# Compile Asterisk finally
cd /usr/src/asterisk-1.2.30.4
make clean
make
make install
make samples

# Do some Asterisk/ViciDial high-load friendly things
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!"

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
cd /var/lib/asterisk/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
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
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
echo "Finished!"
echo
echo "OK! Asterisk should be patched and ready for ViciDial!"
echo

fi
