#!/bin/bash
count=12
array_packages=(bison bison-devel ncurses ncurses-devel zlib zlib-devel openssl openssl-devel gnutls-devel gcc gcc-c++ kernel-devel)
echo ""
echo "This script is for those who are not familar with Centos, zaptel-1.4.7 and asterisk-1.4.15."
echo "It will install OpenVox A400P, A800P and A1200P in an easy way! It performs two main tasks:"
echo "1) Check the necessary packages."
echo "2) Install zaptel, patch files for A1200P if necessary and asterisk."
echo ""
echo "***********check the all packages for Asterisk and Zaptel******************"
for ((i=0;i<$count;i++))
do
echo "$[i+1]---${array_packages[$i]}"
done
echo "Press <Enter> to continue, or <CTRL> + <C> to abort."
echo "****************************************************************************"
read
for ((i=0;i<$count;i++))
do
if (rpm -q ${array_packages[$i]}); then
echo "${array_packages[$i]} has been installed in your system";
else
echo ""
echo "${array_packages[$i]} is needed! Please install it by runing: yum install  ${array_packages[$i]}.";
echo "Press <Enter> to continue install others packages, or <CTRL> + <C> to abort."
read
fi
done
echo ""
echo "You can start installing libpri, zaptel, asterisk"
echo "*********Please select one card to install ******"
select  var in "Install_OpenVox_A400P"  "Install_OpenVox_A800P/A1200P" "Exit"; do
case "$var" in   
    Install_OpenVox_A400P) 
    cd /usr/src
    wget http://downloads.digium.com/pub/zaptel/releases/zaptel-1.4.7.1.tar.gz
    wget http://downloads.digium.com/pub/asterisk/releases/asterisk-1.4.15.tar.gz
    gzip -d ./*.gz
    tar -xf zaptel-1.4.7.1.tar
    tar -xf asterisk-1.4.15.tar
    ln -s zaptel-1.4.7.1 zaptel
    ln -s asterisk-1.4.15 asterisk
    echo "****Finishing patching*************"
    echo "****Starting installing zaptel*****"
    cd zaptel
    make clean all
    ./configure
    make
    make install
    make config
    cd ..
    echo "****Starting installing asterisk***"
    cd asterisk
    make clean all
    make
    make install
    cd ..
    echo "****Finished installation**********"
    echo "****Please reboot the system*******"
    ;; 
    Install_OpenVox_A800P/A1200P)
    cd /usr/src
    wget http://downloads.digium.com/pub/zaptel/releases/zaptel-1.4.7.1.tar.gz
    wget http://downloads.digium.com/pub/asterisk/releases/asterisk-1.4.15.tar.gz
    gzip -d ./*.gz
    tar -xf zaptel-1.4.7.1.tar
    tar -xf asterisk-1.4.15.tar
    ln -s zaptel-1.4.7.1 zaptel
    ln -s asterisk-1.4.15 asterisk
    wget http://www.openvox.com.cn/downloadsFile/zaptel_opvxa1200_patch
    cp zaptel_opvxa1200_patch zaptel
    cd zaptel
    patch -p1 < zaptel_opvxa1200_patch
    echo "****Finishing patching*************"
    echo "****Starting installing zaptel*****"
    make clean all
    ./configure
    make
    make install
    make config
    cd ..
    echo "****Starting installing asterisk***"
    cd asterisk
    make clean all
    ./configure
    make
    make install
    cd ..
    echo "****Finished installation**********"
    echo "****Please change to asterisk dir to make sampels and reboot the system*******"
    ;;
    Exit) 
    echo "Exit from installation!" 
    ;; 
esac 
break
done
 


