#!/bin/bash
# witchdoc (aet) netzwech (punkt) de
# 2009-05-24 install Sangoma B700 und Asterisk 1.4.21
# tested with Ubuntu 8.04 (hardy) LTS

if ls -lah /bin/sh | grep dash 
then
	echo "dash! rm /bin/sh and ln -s /bin/bash /bin/sh "
	exit 0
fi

function checkdownload() {
	# $1 name
	# $2 suffix
	# $3 baseurl
	if [ ! -f "$1.$2" ]
	then 
            echo "download $1.$2"
	    wget "$3/$1.$2"
	fi

	if [ -d "$1" ]
	then
		echo "cleanup previous $1"
		rm -rf "$1"
	fi	

	tar xzf "$1.$2"
}

function pause() {
	if [ -z "$@" ]; then
	    read -p "continue? " answer
        else
	    read -p "$@ ...y or n? " answer
	fi
	case $answer in
	n|N)
	   exit 0
        ;;
        esac	
}

pause "It's recommended to say y to all questions. Please be carefull and have a backup"

pause "============ prepare system?"
aptitude install build-essential -y

pause "============ install zaptel?"
aptitude install udev libnewt-dev -y
cd /usr/src
checkdownload zaptel-1.4.12.1 tar.gz http://downloads.digium.com/pub/zaptel/releases/
cd zaptel-1.4.12.1/
./configure
make
make install
make config
update-rc.d -f zaptel remove
update-rc.d zaptel defaults 15 30

pause "============ install asterisk?"
aptitude install libssl-dev libncurses5-dev libcurl3-dev -y
cd /usr/src
checkdownload asterisk-1.4.21 tar.gz http://downloads.digium.com/pub/asterisk/releases/
cd asterisk-1.4.21/
./configure 
make
make install
make samples
make config
update-rc.d -f asterisk remove
update-rc.d asterisk defaults 50 15

pause "============= install wanpipe?"
aptitude install udev lksctp-tools libsctp-dev bison flex automake1.9 libtool -y
cd /usr/src
checkdownload wanpipe-3.5.2 tgz ftp://ftp.sangoma.com/linux/current_wanpipe/
cd wanpipe-3.5.2
echo "Choose option 7 for BRI and Zaptel"
./Setup install 

pause "============= install asterisk addons?"
aptitude install mysql-server mysql-client phpmyadmin apache2 php5 php5-cli php5-cgi libmysqlclient15-dev -y
cd /usr/src
checkdownload asterisk-addons-1.4.7 tar.gz http://downloads.digium.com/pub/asterisk/releases/
cd asterisk-addons-1.4.7
./configure
make
make install
make samples


