#!/bin/bash

#
# Cnet-O-Matic: Automatically download and install an asterisk-cnet system.
#
# By: Russ Price http://cnet.fubegra.net/
#
# NOTE: Configuration of the installed system is entirely left to the user.
#

# 2010-07-16: make sure to build DAHDI against the latest installed kernel.

# rpmforge.org parameters (defaults for CentOS 5 i386)
# Note that if you're using el6, you should set RF_ARCH to i686
RF_ARCH=i386
RF_DISTRO=el5
RF_RELEASE=0.5.2-2

# parameters for asterisk-cnet and dahdi-cnet
AST_RELEASE=1.4.42
DRIVER_RELEASE=2.4.1.2
TOOLS_RELEASE=2.4.1

NUMCPUS=`grep '^processor' /proc/cpuinfo | wc -l`

echo "This script will add the RPMforge repository to your system, and"
echo "then download and install dahdi-cnet and asterisk-cnet."
echo
echo "THIS IS FOR NEW INSTALLATIONS ONLY.  DO NOT USE IT TO UPGRADE AN"
echo "EXISTING INSTALLATION."
echo
echo "The dahdi-cnet installation will inclode the OSLEC echo canceller,"
echo "and the mouldes be installed via DKMS to automatically recompile them"
echo "(if needed) after a kernel update."
echo
echo "Please note that this script does NOT configure your asterisk-cnet"
echo "or dahdi-cnet installations.  You must do that yourself."
echo
echo "Press ENTER when ready, or Ctrl-C to abort."
read foo
mkdir -p ~/downloads
cd ~/downloads
wget "http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-$RF_RELEASE.$RF_DISTRO.rf.$RF_ARCH.rpm"
wget "http://cnet.fubegra.net/downloads/asterisk-cnet-$AST_RELEASE.tar.gz"
wget "http://cnet.fubegra.net/downloads/dahdi-cnet-linux-$DRIVER_RELEASE.tar.gz"
wget "http://cnet.fubegra.net/downloads/dahdi-cnet-tools-$TOOLS_RELEASE.tar.gz"
rpm -ivh ~/downloads/"rpmforge-release-$RF_RELEASE.$RF_DISTRO.rf.$RF_ARCH.rpm"
yum -y update
yum -y install dkms newt-devel gcc-c++ libtermcap-devel sox sox-devel speex-devel alsa-lib-devel alsa-utils mpg123
KERNEL=`rpm -q kernel | tail -1 | sed -e 's/kernel-//'`
useradd -r asterisk
cd /usr/src
tar xvfz ~/downloads/"dahdi-cnet-linux-$DRIVER_RELEASE.tar.gz"
tar xvfz ~/downloads/"dahdi-cnet-tools-$TOOLS_RELEASE.tar.gz"
tar xvfz ~/downloads/"asterisk-cnet-$AST_RELEASE.tar.gz"
dkms add -m dahdi-cnet-linux -v $DRIVER_RELEASE -k $KERNEL
dkms build -m dahdi-cnet-linux -v $DRIVER_RELEASE -k $KERNEL
dkms install -m dahdi-cnet-linux -v $DRIVER_RELEASE -k $KERNEL
# 2010-11-02: make sure to install headers so the tools will install
cd /usr/src/dahdi-cnet-linux-$DRIVER_RELEASE
make install-include
cd /usr/src/dahdi-cnet-tools-$TOOLS_RELEASE
./configure
make -j$NUMCPUS && make install
make config
dahdi_genconf modules
cd /usr/src/asterisk-cnet-$AST_RELEASE
./configure
make -j$NUMCPUS && make install
make samples
make config

