#!/bin/bash 
# Copyright 2006 Redfone Communications, LLC.
# AUTHOR: Brett Carrington <brettcar@gmail.com>
# 
# Instructions: 
#   chmod a+x redfone_gen.sh 
#   ./redfone_gen.sh
#   (Answer the questions.)
#   mv redfone.gen /etc/redfone.conf

### DO NOT EDIT ANYTHING BELOW
### Make custom changes in the generated file

VERSION=0.2.5
curdir=`pwd`
YUM=`which yum`
WGET=`which wget`
TAR=`which tar`
YUM=`which yum`
RPM=`which rpm`
MAKE=`which make`
APT=`which apt-get`

system=redhat
if [ -f /etc/debian_version ]; then
    system=debian
fi
echo $curdir
echo $system
echo "foneBRIDGE 2 fonulator configuration generator"
echo "Please connect the fonebridge before proceeding"
#echo -n "FB ip address: "
#read fb 
echo -n "Which port of the fonebridge will you be using for TDMoE traffic(1 or 2)?"
read port
echo -n "Which Ethernet card will be used with the fonebridge? [ethN] "
read ethn

#fb_flash_util -i $ethn > fonebridge.info
FB_FLASH=`which fb_flash_util`
if [ $? -eq 0 ]; then
	echo "fb_flash_util found in $FB_FLASH"
	$FB_FLASH -i $ethn > fonebridge.info
	echo "Reading the fonebridge configuration"
	if [ $port -eq 2 ]; then
		fb=`tail -n +22 < fonebridge.info | head -n 1 | cut -d " " -f2`
		mac=`tail -n +19 < fonebridge.info | head -n 1 | cut -c 9-25`
	else
		fb=`tail -n +21 < fonebridge.info | head -n 1 | cut -d " " -f2`
		mac=`tail -n +18 < fonebridge.info | head -n 1 | cut -c 9-25`
	fi

	server=`tail -n +4 < fonebridge.info | head -n 1 | cut -d " " -f3`
	numP=`tail -n +11 < fonebridge.info | head -n 1 | cut -d " " -f2`
	echo "You have a $numP port fonebridge" 
	echo "IP address of fonebridge port connected to $ethn:  $fb"
	echo "MAC address of fonebridge port connected to $ethn: $mac"
	echo "MAC address of $ethn:                              $server"

else
	echo "fb_flash_util not found in system"
	echo -n "Would you like to download the utilities and dependencies (yes|no): "
	read parse
	case "$parse" in
		Y*|y*)
			cd /usr/src
			mkdir redfone
			cd redfone
			#downloading utilities
			$WGET http://support.red-fone.com/downloads/fonulator/fonulator-2.0.0.tar.gz
			$TAR -xzvf fonulator-2.0.0.tar.gz
			$WGET http://support.red-fone.com/fb_flash/fb_flash-2.0.0.tar.gz
			$TAR -xzvf fb_flash-2.0.0.tar.gz
			$WGET http://support.red-fone.com/downloads/fonulator/libfb-2.0.0.tar.gz
			$TAR -xzvf libfb-2.0.0.tar.gz
			#installing dependencies
			if [ $system = redhat ]; then
				$YUM install libpcap
				$YUM install libpcap-devel
				$YUM install libnet			
			
				$WGET http://support.red-fone.com/downloads/tools/argtable2/argtable2-8-1.i386.rpm
				$RPM -ivh argtable2-8-1.i386.rpm
				if [ $? -eq 1 ]; then
					rm -f argtable2-8-1.i386.rpm
					$WGET http://support.red-fone.com/downloads/tools/argtable2/argtable2-8.tar.gz
					$TAR -xzvf argtable2-8.tar.gz
				
					cd argtable2-8
					./configure --includedir=/usr/include --libdir=/usr/lib
					$MAKE
					$MAKE install
					cd ..
				fi
			fi

			if [ $system = debian ]; then
				$APT install libpcap-dev
				$APT install libnet1
				$APT install libnet1-dev
				$APT install libargtable2-dev
				$APT install libreadline5-dev
			fi

			cd libfb-2.0.0
			./configure
			$MAKE
			$MAKE install
			cd ..

			cd fonulator-2.0.0
			./configure
			$MAKE
			$MAKE install
			cd ..

			cd fb_flash-2.0.0
		
			if [ $system = debian ]; then
				./configure 
			else
				./configure --without-readline
			fi
			$MAKE
			$MAKE install
			cd ..
			rm -rf fb_flash*
			rm -rf fonulator*
			rm -rf libfb*
			exit 0
		;;
		N*|n*)
			echo "enter configuration manually"
			echo -n "Enter IP address of port $port of the fonebridge: "
			read fb
			echo -n "Enter MAC address of port $port of the fonebridge: "
			read mac
			echo -n "Enter MAC address of server: "
			read server
			echo -n "Enter 2 for dual fonebridge or 4 for quad fonebridge: "
			read numP
		;;
		esac
	
fi

for i in `seq 1 $numP`; do
	echo "Tell me about span $i..."
	echo -n "Is this a T1 or an E1? [TE] "
	read parse
	case "$parse" in
		E*|e*)
		 t1[$i]=0
		 echo "Okay, E1."
		 echo -n "Enter framing (cas ccs): "
		 read parse
		 case "$parse" in
		     cas*)
		     echo "framing=cas"
		     framing[$i]=cas
		     ;;
		     ccs*)
		     echo "framing=ccs"
		     framing[$i]=ccs
		     ;;
		     *)
		     framing[$i]=ccs
		     echo "Setting to default ccs"
		     ;;
		 esac
		 echo -n "Enter encoding (ami hdb3): "
		 read parse
		 case "$parse" in
		     a*)
		     echo "encoding=ami"
		     encoding[$i]=ami
		     ;;
		     h*)
		     echo "encoding=hdb3"
		     encoding[$i]=hdb3
		     ;;
		     *)
		     echo "Setting to default hdb3"
		     encoding[$i]=hdb3
		     ;;
		 esac		 
		 echo -n "Do you need CRC4 support on span $i? [yn] "
		 read parse
		 case "$parse" in
		     Y*|y*)
		      crc4[$i]=1
		     ;;
		     *)
		      crc4[$i]=0
		     ;;
		 esac
		;;
      		T*|t*)
		 t1[$i]=1
		 crc4[$i]=0
		 echo "Okay, T1."
		 echo -n "Enter framing (sf esf): "
		 read parse
		 case "$parse" in
		     s*)
		     echo "framing=sf"
		     framing[$i]=sf
		     ;;
		     e*)
		     echo "framing=esf"
		     framing[$i]=esf
		     ;;
		     *)
		     echo "Setting to default esf"
		     framing[$i]=esf
		     ;;
		 esac
		 echo -n "Enter encoding (ami b8zs): "
                 read parse
		 case "$parse" in
	             a*)
                     echo "encoding=ami"
                     encoding[$i]=ami
                     ;;
		     b*)		
		     echo "encoding=b8zs"		
		     encoding[$i]=b8zs
		     ;;
		     *)
		     echo "Setting to default b8zs"		
		     encoding[$i]=b8zs
		     ;;
		 esac
		 ;;
		*)
		 t1[$i]=-1
		 echo "Invalid entry, span $i not configured."
		 continue
		;;
   	 esac
	 echo -n "Slave or master? (s or m): "
	 read parse
	 case "$parse" in
	 	M*|m*)
		 master[$i]=1
		 echo "Okay, configuring span $i as master"
		;;
		*)
		 master[$i]=0
		 echo "Okay, configuring span $i as slave"
		;;
	 esac
	 echo -n "RBS? (yes or no): "
	 read parse
	 case "$parse" in
	 	Y*|y*)
		rbs[$i]=1
		echo "span[$i] configured as non pri"
		echo -n "Enter signaling type: "
		read rbsign[$i]
		echo "Enter the channels that will have this signalling type (ex: 1-8): "
		read rbschans[$i]
		configured_chans=`echo ${rbschans[$i]} |  cut -d "-" -f2`
		
		if [ ${t1[$i]} -eq 1 ]; then 
			let unconfigured=24-configured_chans
		else
			let unconfigured=31-configured_chans
		fi

		if [ $unconfigured -gt 0 ]; then
			echo "There are still $unconfigured channels unconfigured for span $i"
			#echo "Enter signalling for remaining channels, or enter - to leave unconfigured: "
			#read parse


		fi

		;;
		*)
		rbs[$i]=0
		;;
	esac
done
#echo "Enter MAC address of the fonebridge (XX:XX:XX:XX:XX:XX format): "
#read mac
#echo "Okay! You're all done. I've stored your new config in the current directory with the name redfone.gen"
#echo "You'll probably want to move this to /etc/redfone.conf"
echo "## Automatically Generated REDFONE Config" > redfone.gen
echo "## Generator Version $VERSION" >> redfone.gen
echo "" >> redfone.gen
echo "[globals]" >> redfone.gen
echo "fb=$fb" >> redfone.gen
echo "port=$port" >> redfone.gen
echo "server=$server" >> redfone.gen
echo "" >> redfone.gen

#Generation of zaptel.conf

echo "##Automatically Generated zaptel configuration file" > zaptel.conf
echo "##Generator Version $VERSION" >> zaptel.conf
echo "" >> zaptel.conf

for i in `seq 1 $numP`; do
let parse=${t1[$i]} 1
 
 echo -n "dynamic=ethmf,$ethn/$mac/`expr $i - 1`," >> zaptel.conf
 if [ $parse -eq 1 ]; then
     echo -n "24," >> zaptel.conf
 else
     echo -n "31," >> zaptel.conf    
 fi

 if [ $i -eq $numP ]; then
     echo "1" >> zaptel.conf
     echo "" >> zaptel.conf
 else
     echo "0" >> zaptel.conf
 fi

 if [ $parse -gt -1 ]; then 
     echo "[span$i]" >> redfone.gen
     echo "framing=${framing[$i]}" >> redfone.gen
     echo "encoding=${encoding[$i]}" >> redfone.gen
     if [ ${master[$i]} -eq 1 ]; then
     	echo "master" >> redfone.gen
     else
     	echo "slave" >> redfone.gen
     fi
	
     if [ ${rbs[$i]} -eq 1 ]; then
     	echo "rbs" >> redfone.gen
     fi
     
     if [ ${crc4[$i]} -eq 1 ]; then 
     	echo "crc4" >> redfone.gen
     fi



#     echo -n "fb" >> redfone.gen
#     echo ${source[$i]} >> redfone.gen
#     echo -n "server" >> redfone.gen
#     echo ${destination[$i]} >> redfone.gen
#     if [ ${pri[$i]} -eq 1 ]; then echo "pri" >> redfone.gen; fi
 fi
 echo "" >> redfone.gen
done
currchan=1
for i in `seq 1 $numP`; do
	let parse=${t1[$i]} 1
	
	if [ ${rbs[$i]} -eq 1 ]; then
		echo -n "${rbsign[$i]}=" >> zaptel.conf
		echo "${rbschans[$i]}" >> zaptel.conf
		echo "" >> zaptel.conf
		if [ $parse -eq 1 ]; then
			let currchan=currchan+24
		else
			let currchan=currchan+31
		fi
	else
		echo -n "bchan=$currchan-" >> zaptel.conf
		if [ $parse -eq 1 ]; then
			let currchan=currchan+22
			echo "$currchan" >> zaptel.conf
			let currchan=currchan+1
			echo "dchan=$currchan" >> zaptel.conf
			echo "" >> zaptel.conf
			let currchan=currchan+1
		else
			let currchan=currchan+14
			echo "$currchan" >> zaptel.conf
			let currchan=currchan+1
			echo "dchan=$currchan" >> zaptel.conf
			let currchan=currchan+1
			echo -n "bchan=$currchan-" >> zaptel.conf
			let currchan=currchan+14
			echo "$currchan" >> zaptel.conf
			echo "" >>zaptel.conf
			let currchan=currchan+1
		fi
	fi
done

echo "loadzone=us" >> zaptel.conf
echo "defaultzone=us" >> zaptel.conf
rm -f fonebridge.info

cp redfone.gen /etc/redfone.conf
mv /etc/zaptel.conf /etc/zaptel.conf.bak
cp zaptel.conf /etc/zaptel.conf

