Thursday, January 03, 2008

Asterisk Install Script for Debian

#!/bin/bash

# Script to download and untar lastest asterisk build
# Justin Hamade v1 - March 24, 2007
# Justin Hamade v1.1 - May 28, 2007 - Added:
# 1. ability to install 1.2 or 1.4
# 2. dep check for wget, make, and gcc
# 3. install asterisk-gui option for 1.4


# ----------------------------------------------------------------------------
# Pause.
# ----------------------------------------------------------------------------
pause()
{

        if test $NONINTERACTIVE; then
                return 0
        fi

        [ $# -ne 0 ] && echo -e $* >&2
        echo -e "Press [Enter] to continue...\c" >&2
        read tmp
        return 0
}
# ----------------------------------------------------------------------------
# Prompt user for input.
# Usage:
# prompt "Prompt"
# $CMD
# ----------------------------------------------------------------------------
prompt()
{
        if test $NONINTERACTIVE; then
                return 0
        fi

        echo -ne "$*" >&2
        read CMD rest
        return 0
}

# ----------------------------------------------------------------------------
# Get Yes/No
# Usage:
# getyn "\nWould you like to continue?" || return 1
# ----------------------------------------------------------------------------
getyn()
{
        if test $NONINTERACTIVE; then
                return 0
        fi

        while prompt "$* (y/n) "
        do      case $CMD in
                        [yY])   return 0
                                ;;
                        [nN])   return 1
                                ;;
                        *)      echo -e "\nPlease answer y or n" >&2
                                ;;
                esac
        done
}
# ----------------------------------------------------------------------------
# Get Version
# ----------------------------------------------------------------------------
getversion()
{
        if test $NONINTERACTIVE; then
                return 0
        fi

        while prompt "Enter $* minor version number (for 1.$BRANCH.0 enter 0 for 1.$BRANCH.1.1 enter 1.1): "
        do      case $CMD in
                        [0-9]|[1-2][0-9]|[0-9].[0-9]|[1-2][0-9].[0-9])
                                return 0
                                ;;
                        *)      echo -e "Please answer a number from 0 to 29" >&2
                                ;;
                esac
        done
}

getbranch()
{
        if test $NONINTERACTIVE; then
                return 0
        fi

        while prompt "Enter asterisk branch number (for 1.2 enter 2 for 1.4 enter 4): "
        do      case $CMD in
                        [2]|[4])
                                return 0
                                ;;
                        *)      echo -e "Please enter 2 or 4" >&2
                                ;;
                esac
        done
}

install_libpri ()
{
        getyn "Install libpri?" || return 1
        LIBPRIVERSION=0
        getversion "libpri"
        LIBPRIVERSION=$CMD
        cd $SRCDIR
        wget ftp://ftp.digium.com/pub/libpri/libpri-1.$BRANCH.$LIBPRIVERSION.tar.gz
        tar zxvf libpri-1.$BRANCH.$LIBPRIVERSION.tar.gz
        rm libpri-1.$BRANCH.$LIBPRIVERSION.tar.gz
        cd $SRCDIR/libpri-1.$BRANCH.$LIBPRIVERSION
        make && make install
        cd $SRCDIR
}

install_zaptel ()
{
        getyn "Install zaptel?" || return 1
        ZAPTELVERSION=1
        getversion "zaptel"
        ZAPTELVERSION=$CMD
        cd $SRCDIR
        wget ftp://ftp.digium.com/pub/zaptel/zaptel-1.$BRANCH.$ZAPTELVERSION.tar.gz
        tar zxvf zaptel-1.$BRANCH.$ZAPTELVERSION.tar.gz
        rm zaptel-1.$BRANCH.$ZAPTELVERSION.tar.gz
        ln -s $SRCDIR/zaptel-1.$BRANCH.$ZAPTELVERSION /usr/src/zaptel
        cd $SRCDIR/zaptel-1.$BRANCH.$ZAPTELVERSION
        if [ "$BRANCH" = "4" ]; then
                ./configure && make menuselect
        fi
        make && make install
        cd $SRCDIR
}

install_asterisk ()
{
        getyn "Install asterisk?" || return 1
        ASTERISKVERSION=2
        getversion "asterisk"
        ASTERISKVERSION=$CMD
        cd $SRCDIR
        wget ftp://ftp.digium.com/pub/asterisk/asterisk-1.$BRANCH.$ASTERISKVERSION.tar.gz
        tar zxvf asterisk-1.$BRANCH.$ASTERISKVERSION.tar.gz
        rm asterisk-1.$BRANCH.$ASTERISKVERSION.tar.gz
        cd $SRCDIR/asterisk-1.$BRANCH.$ASTERISKVERSION
        if [ "$BRANCH" = "4" ]; then
                ./configure && make menuselect
        fi
        make && make install
        cd $SRCDIR
}

install_addons ()
{
        getyn "Install asterisk-addons?" || return 1
        ADDONSVERSION=0
        getversion "addons"
        ADDONSVERSION=$CMD
        cd $SRCDIR
        wget ftp://ftp.digium.com/pub/asterisk/asterisk-addons-1.$BRANCH.$ADDONSVERSION.tar.gz
        tar zxvf asterisk-addons-1.$BRANCH.$ADDONSVERSION.tar.gz
        rm asterisk-addons-1.$BRANCH.$ADDONSVERSION.tar.gz
        cd $SRCDIR/asterisk-addons-1.$BRANCH.$ADDONSVERSION
        if [ "$BRANCH" = "4" ]; then
                ./configure && make menuselect
        fi
        make && make install
        cd $SRCDIR
}
install_gui ()
{
        getyn "Install asterisk-gui?" || return 1
        cd $SRCDIR
        svn checkout http://svn.digium.com/svn/asterisk-gui/trunk asterisk-gui
        cd $SRCDIR/asterisk-gui
        ./configure && make && make install && make samples
        cd $SRCDIR
}
install_sounds ()
{
        getyn "Install asterisk-sounds?" || return 1
        SOUNDSVERSION=0
        getversion "sounds"
        SOUNDSVERSION=$CMD
        cd $SRCDIR
        wget ftp://ftp.digium.com/pub/asterisk/asterisk-sounds-1.$BRANCH.$ADDONSVERSION.tar.gz
        tar zxvf asterisk-sounds-1.$BRANCH.$SOUNDSVERSION.tar.gz
        rm asterisk-sounds-1.$BRANCH.$SOUNDSVERSION.tar.gz
        cd $SRCDIR/asterisk-sounds-1.$BRANCH.$SOUNDSVERSION
        make install
        cd $SRCDIR
}
install_wanpipe ()
{
        getyn "Install sangoma wanpipe?" || return 1
        prompt "Enter wanpipe version (ie 2.3.4-9 or 3.1.0)"
        WANPIPEVERSION=$CMD
        cd $SRCDIR
        wget ftp://ftp.sangoma.com/linux/current_wanpipe/wanpipe-$WANPIPEVERSION.tgz
        tar zxvf wanpipe-$WANPIPEVERSION.tgz
        rm wanpipe-$WANPIPEVERSION.tgz
        cd $SRCDIR/wanpipe-$WANPIPEVERSION
        ./Setup install
        cd $SRCDIR
}

banner ()
{
        cat << ENDOFTEXT
This script will download and install Asterisk 1.2 or 1.4 and its required packages including Sangoma drivers.
ENDOFTEXT
pause
}

checkwget ()
{
        echo "Checking wget ...";
        if [ ! -x $WGET ]; then
                getyn "wget not installed run, apt-get install wget?" || return 1
                apt-get install wget
        fi
        echo "success"
}
checkmake ()
{
        echo "Checking make ...";
        if [ ! -x $MAKE ]; then
                getyn "make not installed, run apt-get install make?" || return 1
                apt-get install make
        fi
        echo "success"
}
checkgcc ()
{
        echo "Checking gcc ...";
        if [ ! -x $GCC ]; then
                getyn "gcc not installed, run apt-get install gcc?" || return 1
                apt-get install gcc
        fi
        echo "success"
}


banner
WGET=/usr/bin/wget
checkwget
MAKE=/usr/bin/make
checkmake
GCC=/usr/bin/gcc
checkgcc
getbranch
BRANCH=$CMD

SRCDIR="/usr/src/asterisk-1.$BRANCH"
if [ -d $SRCDIR ]; then
        echo "directory exsists"
else
        mkdir $SRCDIR
fi
cd $SRCDIR

install_libpri
install_zaptel
install_asterisk
install_addons
if [ "$BRANCH" = "2" ]; then
        install_sounds
fi
if [ "$BRANCH" = "4" ]; then
        install_gui
fi
install_wanpipe