#!/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.4d

echo "foneBRIDGE 2 fonulator configuration generator"
echo "All MAC addresses must be entered in the form:"
echo "   XX:XX:XX:XX:XX:XX "
echo -n "FB1 MAC Address: "
read fb1
echo -n "FB2 MAC Address: " 
read fb2
echo "I will now ask you for the MAC addresses of up to four (4) Asterisk servers."
echo -n "Server 1 MAC Address: "
read server1
echo -n "Server 2 MAC Address: "
read server2
echo -n "Server 3 MAC Address: "
read server3
echo -n "Server 4 MAC Address: "
read server4
numP=-1
while [ $numP -lt 0 ]; do 
 echo "The foneBRIDGE 2 can support up to four (4) T1 or E1 links."
 echo -n "Do you have a 2-port or a 4-port foneBRIDGE? [24] "
 read port
 case "$port" in
  2)
   echo "Okay, I understand you have a 2 port foneBRIDGE."
   numP=2
  ;;
  4)
   echo "Okay, I understand you have a 4 port foneBRIDGE."
   numP=4
 ;;
 *)
   echo "I didn't understand that. Try again."
   numP=-1
 ;;
 esac
done

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 "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."
 		;;
		*)
		 t1[$i]=-1
		 echo "I didn't understand that. I won't configure span $i."
		 continue
		;;
   	 esac
	 echo -n "And is span $i a PRI link? [yn] "
	 read parse
	 case "$parse" in
	 	Y*|y*)
		 pri[$i]=1
		 echo "Okay, configuring span $i as PRI."
		;;
		*)
		 pri[$i]=0
		 echo "Okay, confiuring span $i as non-PRI."
		;;
	 esac
	 echo -n "Of the four servers you specified before, which server should span $i's data be sent to? [1234] "
	 read parse
	 destination[$i]="$parse"
	 echo -n "And should the data for span $i be sent over the foneBRIDGE's 1st MAC or 2nd? [12] "
	 read parse
	 source[$i]="$parse"
done
echo -n "Finally, which Ethernet card will be used to configure the foneBRIDGE? [ethN] "
read ethn
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 "fb1=$fb1" >> redfone.gen
echo "fb2=$fb2" >> redfone.gen
echo "server1=$server1" >> redfone.gen
echo "server2=$server2" >> redfone.gen
echo "server3=$server3" >> redfone.gen
echo "server4=$server4" >> redfone.gen
echo "card=$ethn,fb1" >> redfone.gen
echo "" >> redfone.gen
for i in `seq 1 $numP`; do
let parse=${t1[$i]} 1
 if [ $parse -gt -1 ]; then 
     echo "[span$i]" >> redfone.gen
     echo -n "span=$i,${pri[$i]},0," >> redfone.gen
     if [ ${t1[$i]} -eq 1 ]; then
	 echo "esf,b8zs" >> redfone.gen;
     else
	 echo -n "hdb3,b8zs" >> redfone.gen
     fi
     if [ ${crc4[$i]} -eq 1 ]; then echo ",crc4" >> redfone.gen; else echo >> 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

