#!/bin/sh
#scriptname : openvox_analog_card_setup.sh
	
echo "This is a shellscript for openvox Analog card setup..."
sleep 2 

cardnum=`lspci | sed -n '/Tiger Jet Network Inc/p' | wc -l`
if [ $cardnum -ne 0 ]
then
			if [[ $cardnum == 1 ]]
			then
					echo "The system has found an openvox Analog card! "
			else
					echo "The system has found $cardnum openvox Analog cards! "
			fi
else
			echo "Are you sure you've installed the Analog card? Please check your system to make sure the Analog card is installed properly or you can switch the A400P to another PCI slot. Please try it. "
			exit 65
fi
sleep 2

essential_package="bison bison-devel ncurses ncurses-devel zlib zlib-devel openssl openssl-devel gnutls-devel gcc gcc-c++"
for espack in $essential_package
do
			if rpm -q $espack
			then
						echo "$espack is already in your system"
						echo
			else
						echo "Now download and install $espack"
						yum install $espack
			fi
done

zaptel="zaptel-1.4.11"                   #http://downloads.digium.com/pub/zaptel/releases/zaptel-1.4.11.tar.gz
asterisk="asterisk-1.4.21.1"             #http://downloads.digium.com/pub/asterisk/releases/asterisk-1.4.21.1.tar.gz

echo "Now downloading zaptel and asterisk"
echo
cd /usr/src
wget http://downloads.digium.com/pub/zaptel/releases/$zaptel.tar.gz
wget http://downloads.digium.com/pub/asterisk/releases/$asterisk.tar.gz
tar -xvzf $zaptel.tar.gz
tar -xvzf $asterisk.tar.gz

echo "tar is already done"
sleep 1

if cat /etc/redhat-release | grep "CentOS release 5.2"
then
			cd $zaptel/kernel/xpp/
			sed -e '/#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)/a\
			#if LINUX_VERSION_CODE != KERNEL_VERSION(2,6,18) || ! defined(hex_asc)' -e '118 a\#endif' xdefs.h > xdefs.h.bak
			mv xdefs.h.bak xdefs.h
			echo "patch is done too"
			sleep 1
fi
echo
echo
echo
echo
echo "Please Select the number: "
echo "1)Install_OpenVox_A400P"
echo "2)Install_OpenVox_A800P/A1200P"
echo "3)Exit"
read num
if [ $num -eq 1 ]
then
        :
elif [ $num -eq 2 ]
then
	cd /usr/src
        wget http://www.openvox.com.cn/downloadsFile/opvxa1200.c
        mv opvxa1200.c $zaptel/kernel/
        sed -i -e 's/ztdynamic zttranscode/ztdynamic zttranscode opvxa1200/' -e '/ztmonitor.o: kernel\/zaptel.h/a\opvxa1200.o: kernel/zaptel.h kernel/wctdm.h' zaptel-1.4.11/Makefile
        echo 'MODULES="$MODULES opvxa1200" # OPENVOX A1200P' >> $zaptel/zaptel.sysconfig
else
        exit 10
fi


echo "download is done, next we will compile zaptel and asterisk"
cd /usr/src/$zaptel
make clean
./configure
make
make install
make config
cd ../$asterisk
make clean
./configure
make
make install
make samples

if dmesg | grep "DID YOU REMEMBER TO PLUG IN THE HD POWER CABLE"
then
		echo "Sorry, I think you forget to plup the power cable, shutdown the computer and plug it up,then run me again!"
		exit 65
fi

cd ../$zaptel/kernel/xpp/utils
echo "Please check the number of channels: "
sleep 2 

./genzaptelconf -sdvM 
grep "#include zapata-channels.conf" /etc/asterisk/zapata.conf | echo "#include zapata-channels.conf" >> /etc/asterisk/zapata.conf

isright=1
while [ $isright -eq 1 ]
do
	ztcfg -vvvv
	echo "Now you can see the card's LED is on!"
	echo "Do the channels from output show correctly?"
	echo "1) Yes"
	echo "2) No"
	read answer
	case $answer in
	1) echo "Congratulations, your card is working properly now"
		 isright=0
				;;
	2) echo "Please check the problem from the output file"
	   exit 65
	      			;;
	*) echo "The answer you are entering is not incorrect, please try again!"
				;;
	esac
done 

modprobe zaptel
modprobe wctdm

