#!/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
clear

case "$1" in
	manual)
	manual_input=1
	;;
   	*)
	manual_input=0
	;;
esac

VERSION=0.2.10
curdir=`pwd`
WGET=`which wget`
TAR=`which tar`
MAKE=`which make`
IFCONFIG=`which ifconfig`
ARCH=`uname -m`

if [ -f /etc/redhat-release ]; then
    system=redhat
    RPM=`which rpm`
    YUM=`which yum`
fi
if [ -f /etc/debian_version ]; then
    system=debian
    APT=`which apt-get`
    DPKG=`which dpkg`
fi

#fb_flash_util -i $ethn > fonebridge.info
if [ $manual_input -eq 0 ]; then
  echo -n "Checking for fb_flash_util: "
  which fb_flash_util &> /dev/null
  if [ $? -eq 0 ]; then
     echo "Yes"
     FBFLASH=1
  else
     echo "No"
     FBFLASH=0
  fi

  echo -n "Checking for fonulator: "
  which fonulator &> /dev/null
  if [ $? -eq 0 ]; then
     echo "Yes"
     FONULATOR=1
  else
     echo "No"
     FONULATOR=0
  fi
fi
if [ $FBFLASH -eq 1 ] && [ $manual_input -eq 0 ]; then
	echo -n "Checking for ethtool: "
	if [ $system = redhat ]; then
	  $RPM -qa | grep ethtool > /dev/null
	  if [ $? -eq 0 ]; then
	     echo "Yes"
	  else
	     echo "No"
	     echo "You don't have ethtool installed, please install it and try running the script again"
	     exit
	  fi
	else
	  $DPKG -l | grep ethtool > /dev/null
	  if [ $? -eq 0 ]; then
             echo "Yes"
          else
             echo "No"
             echo "You don't have ethtool installed, please install it and try running the script again"
	     exit
          fi
	fi
	echo -n "Checking for awk: "
	if [ $system = redhat ]; then
          $RPM -qa | grep awk > /dev/null
          if [ $? -eq 0 ]; then
             echo "Yes"
          else
             echo "No"
             echo "awk not installed, please install it and run script again"
             exit
          fi
        else
          $DPKG -l | grep awk > /dev/null
          if [ $? -eq 0 ]; then
             echo "Yes"
          else
             echo "No"
             echo "awk not installed, please install it and run script again"
             exit
          fi
	fi
	
	echo ""
	echo ""	
	echo "*********************************************************"
	echo "*foneBRIDGE2 Install script                             *"
	echo "*                                                       *"
	echo "*The fonebridge utilities are already installed, next   *"
	echo "*the script will help you generate the configuration    *"
	echo "*files.                                                 *"
	echo "*                                                       *" 
	echo "*Please connect the eth port of the fonebridge to the   *"
	echo "*server's network card before proceeding                *"
	echo "*********************************************************"
        
	FB_FLASH=`which fb_flash_util`
	
	numnics=`$IFCONFIG -a | grep Ethernet | awk '{ print $1 }' | awk 'END { print NR }'`
	
	echo  "Which Ethernet card will be used with the fonebridge? "
	 $IFCONFIG -a | grep Ethernet | awk '{ print $1 }' | awk '{ print NR "\t" $0 }'
	echo -n "Select Option [1-$numnics]: "

	read ethn
	echo $ethn>tmp
        cat tmp | grep [a-zA-Z] > /dev/null
	if  [ $? -ne 0 ]
	then
	  isnum=1
	else
	  isnum=0
	fi

	if [ $isnum = 1 ]
	then
	  if [ $ethn -ge 1 ] && [ $ethn -le $numnics ]; then
	     validchoice=1
	  else
	     validchoice=0
	  fi
	fi
	
        while [ $isnum = 0 ] || [ $validchoice = 0 ]; do	  
    	    echo -n "Please enter a valid number from 1 to $numnics: "
	    read ethn
	    echo $ethn>tmp
            cat tmp | grep [a-zA-Z] > /dev/null
            if  [ $? -ne 0 ]
            then
	      isnum=1
          	if [ $ethn -ge 1 ] && [ $ethn -le $numnics ] ; then
		  validchoice=1
		else
		  validchoice=0
           	fi
	    else
	      isnum=0	
            fi
	done
	
	rm -f tmp
	
	tmpvar=`expr $numnics + 1 - $ethn`
	
	ethn=`$IFCONFIG -a | grep Ethernet | awk '{ print $1 }' | awk '{ print NR "\t" $0 }' | awk '{print $2 }' | tail -n $tmpvar | head -n 1`
	echo "You selected $ethn"
	echo -n "Which port of the fonebridge will you be using for TDMoE traffic(1 or 2)? "

	read port

	while [ $port != "1" ] && [ $port != "2" ]; do
          echo -n "Please select 1 or 2: "
          read port
	done
	
	link=`ethtool $ethn | grep Link | cut -d ' ' -f3`
	ethtool $ethn &> /dev/null
	if [ $? -ne 0 ]; then
	   echo "Could not obtain link status, exiting"
	   exit
	fi
	
	if [ $link != yes ]; then
	  echo "**************************************************"
	  echo "* Link is down, please check that the fonebridge *"
	  echo "* is connected to $ethn and powered on, then try  *"
	  echo "* running this script again.                     *"             
	  echo "**************************************************"
	  exit
	else
	 
	   $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"
	
	   echo "Are you using ZAPTEL or DAHDI?"
           echo "  1. Zaptel"
           echo "  2. DAHDI"
	   echo -n "Enter option [1-2]: "	
           read ZORD

           while [ $ZORD != 1 ] && [ $ZORD != 2 ]; do
                echo -n "Invalid option enter 1 or 2: "
                read ZORD
           done

           if [ $ZORD -eq 1 ]; then
             tmpstr=zapata.conf
           else
             tmpstr=chan_dahdi.conf
           fi
           stayinloop=1
	   echo -n "Would you like to configure $tmpstr? (yes/no): "
           while [ $stayinloop -eq 1 ]; do
                read parse
                case "$parse" in
                Y*|y*)
                CONFZORD=1
                stayinloop=0
                ;;
                N*|n*)
                CONFZORD=0
                stayinloop=0
                ;;
                *)
                stayinloop=1
                echo -n "Please enter yes or no: "
                ;;
                esac
           done
	fi
	
else
	if [ $manual_input -eq 0 ]; then	
	  echo ""
	  echo ""
	  echo "*********************************************************"
          echo "*foneBRIDGE2 Install script                             *"
          echo "*                                                       *"
          echo "*This script works with debian and redhat based systems,*"
          echo "*if your system has access to the internet, it will     *"
          echo "*download the required utilties and dependencies        *"
          echo "*required for configuring the fonebridge                *"
          echo "*                                                       *"
          echo "*After all utilities are installed, you have the option *"
          echo "*of running the script a second time to help you        *"
          echo "*generate the required configuration files.             *"
          echo "*                                                       *"
          echo "*Otherwise you can select no, and enter all values      *"
          echo "*manually.                                              *"  
          echo "*********************************************************"
          echo " "
	  echo -n "Download fonebridge utilities? (yes|no): "
	  read parse
	else
	  parse=no
	fi
	case "$parse" in
		Y*|y*)
			cd /usr/src
			if [ -d redfone ]; then
			  echo "/usr/src/redfone found"
			else
			  mkdir redfone &> /dev/null
			  if [ $? -ne 0 ]; then
			    echo " Could not create /usr/src/redfone directory"
			    echo " Are you executing this script as root?"
			    exit
			  fi
   			fi
		
			cd redfone
			#downloading utilities
			if [ $FONULATOR -eq 0 ]; then
			
			  find /usr/src -type f -name "fonulator*tar*gz*" > fonulator-src
			  if [ -s fonulator-src ]; then
			     
			     numfonsourcefiles=`cat fonulator-src | awk '{ print $1 }' | awk 'END { print NR }'`
			
			     if [ $numfonsourcefiles -ne 1 ]; then
				echo "$numfonsourcefiles fonulator sources files found:"
			        cat fonulator-src | awk '{ print $1 }' | awk '{ print NR "\t" $0 }'
			        echo -n "Select Option [1-$numfonsourcefiles]: "
			        read parse
			        tmpvar=`expr $numfonsourcefiles + 1 - $parse`
                                fonsource=`cat fonulator-src | awk '{ print $1 }' | awk '{ print NR "\t" $0 }' | awk '{print $2 }' | tail -n $tmpvar | head -n 1`
			     else
				fonsource=`cat fonulator-src`
				echo "fonulator sources found in $fonsource"
			     fi	
			     	
			     echo "unpacking $fonsource "
			     
			     $TAR -xzvf $fonsource &> /dev/null
			  else
			     echo "Downloading fonulator"
			     $WGET http://support.red-fone.com/downloads/fonulator/fonulator-2.0.1.tar.gz &> /dev/null	
			     if [ $? -ne 0 ]; then
			   	echo "Could not download fonulator sources, download them manually, and run this script again"
			      	exit
			     fi
						
			     $TAR -xzvf fonulator-2.0.1.tar.gz &> /dev/null
			  fi
			rm -rf fonulator-src
			fi
			
			if [ $FBFLASH -eq 0 ]; then
			  find /usr/src -type f -name "fb*flash*tar*gz*" > fbflash-src			  
			  if [ -s fbflash-src ]; then
			     numfonsourcefiles=`cat fbflash-src | awk '{ print $1 }' | awk 'END { print NR }'`
			     
			     if [ $numfonsourcefiles -ne 1 ]; then
                                echo "$numfonsourcefiles fb_flash_util sources files found:"
                                cat fbflash-src | awk '{ print $1 }' | awk '{ print NR "\t" $0 }'
                                echo -n "Select Option [1-$numfonsourcefiles]: "
                                read parse
                                tmpvar=`expr $numfonsourcefiles + 1 - $parse`
                                fbflashsource=`cat fbflash-src | awk '{ print $1 }' | awk '{ print NR "\t" $0 }' | awk '{print $2 }' | tail -n $tmpvar | head -n 1`
                             else
                                fbflashsource=`cat fbflash-src`
                                echo "fb_flash_util sources found in $fbflashsource"
                             fi
				
			     echo "unpacking $fbflashsource"
			     $TAR -xzvf $fbflashsource &> /dev/null
			  else
			     echo "Downloading fb_flash_util"
			     $WGET http://support.red-fone.com/fb_flash/fb_flash-2.0.0.tar.gz &> /dev/null
			     if [ $? -ne 0 ]; then
				echo "Could not download fb_flash_util sources, download them manually, and run this script again"
				exit
			     fi
			     $TAR -xzvf fb_flash-2.0.0.tar.gz &> /dev/null
			  fi
			fi
			
			echo -n "Checking for libfb libraries: "
			
			if [ -f /usr/local/lib/libfb.so ]; then
			   echo "Yes"
			   LIBFB=1
			else
			   LIBFB=0
			   echo "No"
			   echo "Downloading libfb libraries"
			   $WGET http://support.red-fone.com/downloads/fonulator/libfb-2.0.0.tar.gz &> /dev/null
			   if [ $? -ne 0 ]; then
                              echo "Could not download libfb sources,
download them manually, and run this script again"
                              exit
                           fi
			   $TAR -xzvf libfb-2.0.0.tar.gz &> /dev/null
			fi
			#installing dependencies
			echo "Checking for dependencies"
			
			if [ $system = redhat ]; then
				echo -n "Checking for libpcap: "
				$RPM -qa | grep libpcap-devel &> /dev/null 
				if [ $? -eq 0 ]; then
				  echo "Yes"
				else
				  echo "No.. downloading libpcap"	
				  $YUM install libpcap-devel -y &> /dev/null
				  if [ $? -ne 0 ]; then
				     echo "Could not install libpcap-devel package"
				     exit
				  fi
				fi	
				
				$RPM -qa | grep libpcap &> /dev/null
				if [ $? -ne 0 ]; then
				  $YUM install libpcap -y &> /dev/null
				  if [ $? -ne 0 ]; then
                                     echo "Could not install libpcap package"
				     exit
                                  fi
				fi
				
				echo -n "Checking for libnet: "
				$RPM -qa | grep libnet-devel &> /dev/null
				if [ $? -eq 0 ]; then
				  echo "Yes"
				else
				  echo "No.. downloading libnet"
				  $YUM install libnet -y &> /dev/null
				  $YUM install libnet-devel -y &> /dev/null
				  if [ $? -ne 0 ]; then
				     echo "Could not install libnet package"
				     exit
				  fi
				fi
				
			#$RPM -qa | grep ethtool
			#if [ $? -ne 0 ]; then								  $YUM install ethtool -y > /dev/null
		#		fi
				echo -n "Checking for argtable2: "
			 	$RPM -qa | grep argtable2 &> /dev/null
				if [ $? -ne 0 ]; then
				  
				  if [ $ARCH = i686 ]; then
				     echo "No.. downloading argtable2"
				     $WGET http://support.red-fone.com/downloads/tools/argtable2/argtable2-7-1.i386.rpm &> /dev/null
				     $RPM -ivh argtable2-7-1.i386.rpm &> /dev/null
				  fi
				
				
				  if [ $? -ne 0 ] || [ $ARCH != i686 ]; then
					echo "Could not install from rpm, Installing from source"
					
					rm -f argtable2-7-1.i386.rpm
					$WGET http://support.red-fone.com/downloads/tools/argtable2/argtable2-8.tar.gz &> /dev/null
					$TAR -xzvf argtable2-8.tar.gz &> /dev/null
				
					cd argtable2-8
					./configure --includedir=/usr/include --libdir=/usr/lib &> /dev/null
					$MAKE &> /dev/null
					$MAKE install &> /dev/null
					if [ $? -ne 0 ]; then
						echo "could not install argtable2 libraries.. fonulator will not be installed"
						FONULATOR=1
					fi
					
					cd ..
				  fi

				else
				  echo "Yes"
			 
				fi
			fi

			if [ $system = debian ]; then
			   echo -n "Checking for libpcap: "
			   $DPKG -l | grep libpcap-dev &> /dev/null
			   if [ $? -ne 0 ]; then
			      echo "No... downloading libpcap"	
			      $APT install libpcap-dev -y &>/dev/null
			      if [ $? -ne 0 ]; then
			   	echo "Could not install libpcap-dev (apt-get install libpcap-dev failed)"
				
				exit
			      fi
			   else
			      echo "Yes"
			   fi
			   echo -n "Checking for libnet: "
			   $DPKG -l | grep libnet1 &>/dev/null
			   if [ $? -ne 0 ]; then
			      echo "No... downloading libnet"
			      $APT install libnet1 -y &> /dev/null
			      if [ $? -ne 0 ]; then
				echo "Could not install libnet (apt-get install libnet1 failed)"
			        exit
			      fi

			      $DPKG -l | grep libnet1-dev &> /dev/null	
	 		      if [ $? -ne 0 ]; then  
                              	$APT install libnet1-dev -y &> /dev/null
			      fi
			   else
			      echo "Yes"
			   fi
			   echo -n "Checking for argtable2: "
  			   $DPKG -l | grep libargtable2-dev &> /dev/null
			   if [ $? -ne 0 ]; then
			      echo "No... downloading argtable2 "
			      $APT install libargtable2-dev -y &> /dev/null
			   else
			      echo "Yes"
			   fi
		
			   echo -n "Checking for libreadline: "
			   $DPKG -l | grep libreadline5-dev &> /dev/null
			   if [ $? -ne 0 ]; then
				
			      echo "No... downloading libreadline"	
		              $APT install libreadline5-dev -y &> /dev/null
			   else
			      echo "Yes"
			   fi
			  #$APT install ethtool -y
			fi
			
			if [ $LIBFB -eq 0 ];then
			   echo "Installing libfb"
			   cd libfb-2.0.0
			   ./configure &> /dev/null
			   $MAKE &> /dev/null
			   $MAKE install &> /dev/null
			   if [ $? -ne 0 ]; then
			      echo "Could not install libfb static libraries"
			      cd ..
			      rm -rf libfb*
			      exit
			   fi
			   cd ..
			   rm -rf libfb*
			fi

			if [ $FONULATOR -eq 0 ]; then
			  echo "Installing fonulator"
			  cd fonulator-2.0.1
			  ./configure &> /dev/null
			  $MAKE &> /dev/null
			  $MAKE install &> /dev/null
			  if [ $? -ne 0 ]; then
                             echo "Could not install fonulator from source"
                             
                             rm -rf fonulator*
			     if [ $system = redhat ]; then
                                echo "Installing from rpm"
                                wget http://support.red-fone.com/downloads/fonulator/fonulator-2.0.1-1.i386.rpm
                                rpm -ivh fonulator-2.0.1-1.i386.rpm
                                rm fonulator-2.0.1-1.i386.rpm
                             else

                                echo "Error installing fonulator"
                                exit 1
                             fi
			     cd ..
                          
			  else
			     echo "fonulator installed"
							     
                          fi
			  
			  
			fi
			
			if [ $FBFLASH -eq 0 ]; then
			
			   echo "Installing fb_flash_util"
			   cd fb_flash-2.0.0
		
			   if [ $system = debian ]; then
				./configure &> /dev/null
				if [ $? -ne 0 ]; then
				  echo "Installing without readline support"
				  ./configure --without-readline &> /dev/null
				fi	
			   else
				./configure --without-readline &> /dev/null
			   fi
			   $MAKE &> /dev/null
			   $MAKE install &> /dev/null
			   if [ $? -ne 0 ]; then
                              echo "Could not install fb_flash_util"
                              cd ..
                              rm -rf fb_flash*
                              exit
                           fi
			   cd ..
			   rm -rf fb_flash*
			fi
			echo "Please run redfone_gen script again to generate the configuration files"
			exit 0
		;;
		N*|n*)
			echo "******************************"
			echo "*Enter configuration manually*"
			echo "******************************"
			echo ""
			
			echo -n "Enter name of network card you will be using for TDMoE traffic (Ex. eth1): "
			read ethn

			echo -n "Which port of the fonebridge will you be using for TDMoE traffic(1 or 2)?"
		        read port

        		while [ $port != "1" ] && [ $port != "2" ]; do
          		  echo -n "Please select 1 or 2: "
          		  read port
        		done
 
			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 "Enter fonebridge model: "
			echo "1. 750-4000 (single)"
			echo "2. 750-5050 (dual)"
			echo "4. 750-5000 (quad)"
			while [ "$numP" != "1" ] && [ "$numP" != "2" ] && [ "$numP" != "4" ] ; do
			
			  echo -n "Please enter 1,2 or 4: "
			  read numP
			done
			 
			
			echo "Are you using ZAPTEL or DAHDI?"
           		echo "  1. Zaptel"
           		echo "  2. DAHDI"

           		read ZORD

         		while [ $ZORD != 1 ] && [ $ZORD != 2 ]; do
                	  echo -n "Invalid option enter 1 or 2: "
                	  read ZORD
           		done

           		if [ $ZORD -eq 1 ]; then
                          tmpstr=zapata.conf
           		else
             		  tmpstr=chan_dahdi.conf
           		fi
           		stayinloop=1
         		echo -n "Would you like to configure $tmpstr? (yes/no) "
           		while [ $stayinloop -eq 1 ]; do
                	  read parse
                	  case "$parse" in
                	  Y*|y*)
                	  CONFZORD=1
                	  stayinloop=0
                   	  ;;
                	  N*|n*)
                	  CONFZORD=0
                	  stayinloop=0
                	  ;;
                	  *)
                	  stayinloop=1
                	  echo -n "Please enter yes or no: "
			  ;;
			  esac
			done
		        ;;
		esac
		
	
fi

for i in `seq 1 $numP`; do
	echo "Span $i..."
	echo "Is this a T1 or an E1?"
	t1[$i]=-1
	if [ $i -gt 1 ]; then
		echo ""
	  	echo  "Hit ENTER key to configure Span $i the same as Span `expr $i - 1`: "
		if [ ${t1[$i-1]} -eq 0 ]; then
		  echo  "E1 "
		  echo  "  framing=${framing[$i-1]}"
		  echo  "  encoding=${encoding[$i-1]}"
		fi
		if [ ${t1[$i-1]} -eq 1 ]; then
		  echo  "T1 "
		  echo  "  framing=${framing[$i-1]}"
                  echo  "  encoding=${encoding[$i-1]}"
		fi
	
	fi
#	echo -n "Is this a T1 or an E1? [TE] "
	while [ ${t1[$i]} -eq -1 ]
	do
	echo -n "Enter T for T1, E for E1: "
	read parse
	case "$parse" in
		E*|e*)
		 t1[$i]=0
		 rbs[$i]=0
		 echo "Okay, E1."
		 echo -n "Enter framing (cas ccs): "
		 if [ $i -gt 1 ]; then
			if [ ${t1[$i-1]} -eq ${t1[$i]} ]; then
			  echo "Hit ENTER to configure framing as ${framing[$i-1]} "
			  echo "otherwise enter new value: "
			fi
		 fi
		 read parse
		 case "$parse" in
		     cas*)
		     echo "framing=cas"
		     framing[$i]=cas
		     rbs[$i]=1
		     ;;
		     ccs*)
		     echo "framing=ccs"
		     framing[$i]=ccs
		     ;;
		     *)
		     if [ $i -eq 1 ]; then
		     	framing[$i]=ccs
			echo "Setting framing to default value: ccs"
		     else
			if [ ${t1[$i-1]} -eq ${t1[$i]} ]; then
			  framing[$i]=${framing[$i-1]}
		          echo "framing=${framing[$i-1]}"
			else
			  framing[$i]=ccs
			  echo "Setting framing to default value: ccs"
			fi
		     fi
		     ;;
			
		 esac
		 echo -n "Enter encoding (ami hdb3): "
		 if [ $i -gt 1 ]; then
                        if [ ${t1[$i-1]} -eq ${t1[$i]} ]; then
                          echo "Hit ENTER to configure encoding as ${encoding[$i-1]}"
			  echo "otherwise enter new value: "
                        fi
                 fi
		 read parse
		 case "$parse" in
		     a*)
		     echo "encoding=ami"
		     encoding[$i]=ami
		     ;;
		     h*)
		     echo "encoding=hdb3"
		     encoding[$i]=hdb3
		     ;;
		     *)
		     if [ $i -eq 1 ]; then
                        encoding[$i]=hdb3
                        echo "Setting encoding to default value: hdb3"
                     else
                        if [ ${t1[$i-1]} -eq ${t1[$i]} ]; then
                          encoding[$i]=${encoding[$i-1]}
                          echo "encoding=${encoding[$i-1]}"
                        else
			  encoding[$i]=hdb3
                          echo "Setting encoding to default value: hdb3"
			fi
                     fi
		     ;;
		 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): "
		 if [ $i -gt 1 ]; then
                        if [ ${t1[$i-1]} -eq ${t1[$i]} ]; then
                          echo "Hit ENTER to configure framing as ${framing[$i-1]} "
                          echo "otherwise enter new value: "
                        fi
                 fi
		 read parse
		 case "$parse" in
		     s*|S*)
		     echo "framing=sf"
		     framing[$i]=sf
		     ;;
		     e*|E*)
		     echo "framing=esf"
		     framing[$i]=esf
		     ;;
		     *)
		     if [ $i -eq 1 ]; then
                        framing[$i]=esf
                        echo "Setting to framing to default value: esf"
                     else
                        if [ ${t1[$i-1]} -eq ${t1[$i]} ]; then
                          framing[$i]=${framing[$i-1]}
                          echo "framing=${framing[$i-1]}"
                        else
                          framing[$i]=esf
                          echo "Setting to framing to default value: esf"
                        fi
                     fi
		     ;;
		 esac
		 echo -n "Enter encoding (ami b8zs): "
		 if [ $i -gt 1 ]; then
                        if [ ${t1[$i-1]} -eq ${t1[$i]} ]; then
                          echo "Hit ENTER to configure encoding as ${encoding[$i-1]} "
                          echo "otherwise enter new value: "
                        fi
                 fi
                 read parse
		 case "$parse" in
	             a*)
                     echo "encoding=ami"
                     encoding[$i]=ami
                     ;;
		     b*)		
		     echo "encoding=b8zs"		
		     encoding[$i]=b8zs
		     ;;
		     *)
		     if [ $i -eq 1 ]; then
                        encoding[$i]=esf
                        echo "Setting encoding to default value: b8zs"
                     else
                        if [ ${t1[$i-1]} -eq ${t1[$i]} ]; then
                          encoding[$i]=${encoding[$i-1]}
                          echo "encoding=${encoding[$i-1]}"
                        else
                          encoding[$i]=b8zs
                          echo "Setting encoding to default value: b8zs"
                        fi
                     fi
		     ;;
		 esac
		
		 if [ $CONFZORD -eq 0 ]; then
		   echo -n "Is this an RBS line: "
		   if [ $i -gt 1 ]; then
                        if [ ${t1[$i-1]} -eq ${t1[$i]} ]; then
			  if [ ${rbs[$i-1]} -eq 1 ]; then
                            echo "Hit ENTER to configure Span $i with rbs "
                            echo -n "otherwise enter no: "
			  else
			    echo "Hit Enter to configure Span $i without rbs "
			    echo -n "otherwise enter yes: "
			  fi
                        fi
                   fi
		   read parse
		   case "$parse" in
		     Y*|y*)
			echo "Setting Span $i with rbs"
			rbs[$i]=1
		     ;;
		     N*|n*)
			echo "Setting Span $i without rbs"
			rbs[$i]=0
		     ;;
		     *)
			rbs[$i]=${rbs[$i-1]}	
		     ;;
		   esac
		 fi
		 ;;
		*)
		 if [ $i -gt 1 ]; then
		     echo -n "Span $i configured as  "
		     if [ ${t1[$i-1]} -eq 1 ]; then
			echo "T1"
		     else
			echo "E1"
		     fi
		     t1[$i]=${t1[$i-1]}
		     framing[$i]=${framing[$i-1]}
		     encoding[$i]=${encoding[$i-1]}
		     rbs[$i]=${rbs[$i-1]}
		     crc4[$i]=${crc4[$i-1]}

		 else
		 	
		     echo "Invalid Choice"
		     echo -n "  Please enter T1 or E1: "
		     t1[$i]=-1
		fi
		;;
   	 esac
	 done
	 echo -n "Slave or Master (s or m): "
	 read parse
	 case "$parse" in
	 	S*|s*|1*)
		 master[$i]=0
		 echo "Okay, configuring span $i as slave"
		;;
		M*|m*|2*)
		 master[$i]=1
		 echo "Okay, configuring span $i as master"
		;;
		*)
		 echo "Setting span $i to default (slave)"
		 master[$i]=0
		;;
	 esac
	 
	 if [ ${t1[$i]} -eq 0 ]; then	    
	   if [ ${framing[$i]} = cas ]; then
	      echo  "Select signaling type"
              echo  " 1. FXS Loop Start"
              echo  " 2. FXS Ground Start"
              echo  " 3. FXS Kewl Start"
              echo  " 4. FXO Loop Start"
              echo  " 5. FXO Ground Start"
              echo  " 6. FXO Kewl Start"
              echo  " 7. E & M"
              echo  " 8. E & M Wink"
	      echo  " 9. MFC/R2"
	      echo  -n " Select Option [1-9]: "
	   else
	      echo  "Select signaling type"
              echo  " 1. PRI CPE"
              echo  " 2. PRI NET"
              echo  " 3. FXS Loop Start"
              echo  " 4. FXS Ground Start"
              echo  " 5. FXS Kewl Start"
              echo  " 6. FXO Loop Start"
              echo  " 7. FXO Ground Start"
              echo  " 8. FXO Kewl Start"
              echo  " 9. E & M"
              echo  "10. E & M Wink"
	      echo  -n " Select Option [1-10]: "
	   fi
	 else
 
	   echo  "Select signaling type"
	   echo  " 1. PRI CPE"
	   echo  " 2. PRI NET"
	   echo  " 3. FXS Loop Start"
	   echo  " 4. FXS Ground Start"
	   echo  " 5. FXS Kewl Start"
	   echo  " 6. FXO Loop Start"
	   echo  " 7. FXO Ground Start"
	   echo  " 8. FXO Kewl Start"
	   echo  " 9. E & M"
	   echo  "10. E & M Wink"
	   echo  -n " Select Option [1-10]: "
	 fi
	 read parse
	 echo ""
	 echo ""
	 if [ ${t1[$i]} -eq 0 ]; then
           if [ ${framing[$i]} = cas ]; then
	     let parse=parse+2
	   fi
	 fi
	 case "$parse" in

		1|P*|p*|C*|c*)		
		rbs[$i]=0
		ast_sign[$i]=pri_cpe

		;;

		2*|N*|n*)
		rbs[$i]=0
		ast_sign[$i]=pri_net

		;;
	
	 	3*|L*|l*)	
		rbs[$i]=1
		rbsign[$i]=fxsls
		ast_sign[$i]=fxs_ls
		
		;;

		4*|G*|g*)
		rbs[$i]=1
                rbsign[$i]=fxsgs
		ast_sign[$i]=fxs_gs		

		;;

		5*|K*|k*)
                rbs[$i]=1
                rbsign[$i]=fxsks
		ast_sign[$i]=fxs_ks

		;;

		6*)
                rbs[$i]=1
                rbsign[$i]=fxols
		ast_sign[$i]=fxo_ls
		;;
		
		7*)
                rbs[$i]=1
                rbsign[$i]=fxogs
		ast_sign[$i]=fxo_gs
	
		;;

		8*)
                rbs[$i]=1
                rbsign[$i]=fxoks
		ast_sign[$i]=fxo_ks
	
		;;
		
		9*)
                rbs[$i]=1
                rbsign[$i]='e&m'
		ast_sign[$i]=em

		;;

		10*)
                rbs[$i]=1
                rbsign[$i]='e&m'
		ast_sign[$i]=em_w
		
		;;
		
		11*)
		rbs[$i]=1
		rbsign[$i]=cas
		ast_sign[$i]=mfcr2
		
		;;

		*)
		echo "No option selected, setting signaling of Span $i to pri cpe"
		rbs[$i]=0
	  	ast_sign[$i]=pri_cpe	
		;;	
	   esac

	if [ $CONFZORD -eq 1 ]; then
	
	  if [ ${rbs[$i]} -eq 0 ]; then

	   echo  "Select switchtype"
           echo  " 1. National ISDN 2"
           echo  " 2. Nortel DMS100"
           echo  " 3. AT&T 4ESS"
           echo  " 4. Lucent 5ESS"
           echo  " 5. EuroISDN"
           echo  " 6. Old National ISDN 1"
           echo  " 7. Q.SIG"
	   echo  -n "Select option [1-7]: "
	   read parse
	   echo ""
	   echo ""

           case "$parse" in

                1*|N*|n*)
                switchtype[$i]=national

                ;;

		2*|D*|d*)
                switchtype[$i]=dms100
		;;

		3*)
                switchtype[$i]=4ess
		
		;;
		
		4*)
                switchtype[$i]=5ess

		;;

		5*)
                switchtype[$i]=euroisdn

		;;
		
		6*)
                switchtype[$i]=ni1
		
		;;
		
		7*)
                switchtype[$i]=qsig

		;;
	  esac

	fi
	  echo  "Select context"
          echo  " 1. from-pstn"
          echo  " 2. from-internal"
          echo  " 3. custom"
	  echo  -n "Select option: [1-3]: "
	
	  read parse
	  echo ""
	  echo ""
          case "$parse" in

                1*)
                context[$i]=from-pstn

                ;;

                2*)
                context[$i]=from-internal
                ;;

                3*)
		echo "Enter name of context"
		read context[$i]
		;;
	  esac
	fi	
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 zapata.conf
if [ $CONFZORD -eq 1 ]; then
	echo ";Automatically Generated $tmpstr configuration file" > zapata.conf
	echo ";Generator Version $VERSION" >> zapata.conf
	echo "" >> zapata.conf

	echo "[trunkgroups]" >> zapata.conf
	echo "" >> zapata.conf

	echo "[channels]" >> zapata.conf
	echo "usecallerid=yes" >> zapata.conf
	echo "hidecallerid=no" >> zapata.conf
	echo "callwaiting=yes" >> zapata.conf
	echo "usecallingpres=yes" >> zapata.conf
	echo "callwaitingcallerid=yes" >> zapata.conf
	echo "threewaycalling=yes" >> zapata.conf
	echo "transfer=yes" >> zapata.conf
	echo "canpark=yes" >> zapata.conf
	echo "cancallforward=yes" >> zapata.conf
	echo "callreturn=yes" >> zapata.conf
	echo "echocancel=no" >> zapata.conf
	echo "echocancelwhenbridged=no" >> zapata.conf
	echo "relaxdtmf=yes" >> zapata.conf
	echo "rxgain=0.0" >> zapata.conf
	echo "txgain=0.0" >> zapata.conf
	echo "group=1" >> zapata.conf
	echo "callgroup=1" >> zapata.conf
	echo "pickupgroup=1" >> zapata.conf
	echo "" >> zapata.conf
fi
#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 [ $CONFZORD -eq 1 ]; then
	  echo -n "context=" >> zapata.conf
	  echo "${context[$i]}" >> zapata.conf	
	fi

	if [ ${rbs[$i]} -eq 1 ]; then
		echo -n "${rbsign[$i]}=$currchan-" >> zaptel.conf
		echo -n "signalling=" >> zapata.conf
		echo "${ast_sign[$i]}" >> zapata.conf
		echo -n "channel => $currchan-" >> zapata.conf
		if [ $parse -eq 1 ]; then
                        let currchan=currchan+23
                        echo "$currchan" >> zaptel.conf
                        echo "" >> zaptel.conf
                        echo "$currchan" >> zapata.conf
                        echo "" >> zapata.conf
			let currchan=currchan+1

		else	
			firstchan=$currchan
			if [ ${framing[$i]} = cas ]; then
			
			   let currchan=currchan+14
                           echo "$currchan:1101" >> zaptel.conf
                           echo -n "$currchan, " >> zapata.conf
                           let currchan=currchan+1
                           echo "dchan=$currchan" >> zaptel.conf
                           let currchan=currchan+1
                           echo -n "cas=$currchan-" >> zaptel.conf
                           echo -n "$currchan-" >> zapata.conf
                           let currchan=currchan+14
                           echo "$currchan:1101" >> zaptel.conf
                           echo "$currchan" >> zapata.conf
                           echo "" >> zapata.conf
			   echo "alaw=$firstchan-$currchan" >> zaptel.conf
			   let currchan=currchan+1
			   echo "" >> zaptel.conf
			
			  
			else 
			   let currchan=currchan+30
                           echo "$currchan" >> zaptel.conf
			   echo "$currchan" >> zapata.conf
                           echo "" >> zapata.conf
			   echo "alaw=$firstchan-$currchan" >> zaptel.conf
			   echo "" >> zaptel.conf
                           let currchan=currchan+1
			fi
		fi

	else
		echo -n "bchan=$currchan-" >> zaptel.conf
		echo "switchtype=${switchtype[$i]}" >> zapata.conf
		echo -n "signalling=" >> zapata.conf
		echo "${ast_sign[$i]}" >> zapata.conf
                echo -n "channel => $currchan-" >> zapata.conf
		if [ $parse -eq 1 ]; then
			let currchan=currchan+22
			echo "$currchan" >> zaptel.conf
			echo "$currchan" >> zapata.conf
			echo "" >> zapata.conf
			let currchan=currchan+1
			echo "dchan=$currchan" >> zaptel.conf
			echo "" >> zaptel.conf
			let currchan=currchan+1
		else
			firstchan=$currchan
			let currchan=currchan+14
			echo "$currchan" >> zaptel.conf
			echo -n "$currchan, " >> zapata.conf
			let currchan=currchan+1
			echo "dchan=$currchan" >> zaptel.conf
			let currchan=currchan+1
			echo -n "bchan=$currchan-" >> zaptel.conf
			echo -n "$currchan-" >> zapata.conf
			let currchan=currchan+14
			echo "$currchan" >> zaptel.conf
                        echo "alaw=$firstchan-$currchan" >> zaptel.conf
			echo "" >>zaptel.conf
			echo "$currchan" >> zapata.conf
			echo "" >> zapata.conf
			let currchan=currchan+1
			
		fi
	fi
done

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

if [ -f /etc/redfone.conf ]; then
	mv /etc/redfone.conf /etc/redfone.conf.bak
fi

cp redfone.gen /etc/redfone.conf

parse=$ZORD

         case "$parse" in

                1*|Z*|z*)
                if [ -f /etc/zaptel.conf ]; then
			echo "Backing up zaptel.conf to zaptel.conf.bak"
			mv /etc/zaptel.conf /etc/zaptel.conf.bak				
                fi
		
		echo "Zaptel configuration saved to /etc/zaptel.conf"
		cp zaptel.conf /etc/zaptel.conf
		
		if [ $CONFZORD == 1 ]; then
		  if [ -d /etc/asterisk ]; then
		    if [ -f /etc/asterisk/zapata.conf ]; then
                        echo "Backing up zapata.conf to zapata.conf.bak"
                        mv /etc/asterisk/zapata.conf /etc/asterisk/zapata.conf.bak
                    fi
		

                    echo "Zapata configuration saved to /etc/asterisk/zapata.conf"
	     	    cp zapata.conf /etc/asterisk/zapata.conf
		  else
		    echo "/etc/asterisk directory not found, probably because asterisk is not installed, the new generated zapata.conf file will be saved in the current directory"
		    	  
		  fi
		fi

                ;;
		
		2*|D*|d*)
		
		if [ -d /etc/dahdi ]; then
		  if [ -f /etc/dahdi/system.conf ]; then
                        echo "Backing up system.conf to system.conf.bak"
                        mv /etc/dahdi/system.conf /etc/dahdi/system.conf.bak
                  fi

                  echo "DAHDI configuration saved to /etc/dahdi/system.conf"
                  mv zaptel.conf /etc/dahdi/system.conf
		else
		  echo "/etc/dahdi directory not found, probably because dahdi has not been installed, the new generated system.conf file will be saved in the current directory"
		  mv zaptel.conf system.conf
		fi
		
		if [ $CONFZORD == 1 ]; then
		  if [ -d /etc/asterisk ]; then
	            if [ -f /etc/asterisk/chan_dahdi.conf ]; then
                        echo "Backing up chan_dahdi.conf to chan_dahdi.conf.bak"
                        mv /etc/asterisk/chan_dahdi.conf /etc/asterisk/chan_dahdi.conf.bak
                    fi

                    echo "Chan_dahdi configuration saved to /etc/asterisk/chan_dahdi.conf"
		    mv zapata.conf /etc/asterisk/chan_dahdi.conf
		  else
		    echo "/etc/asterisk directory not found, probably because asterisk has not been installed, the new generated chan_dahdi.conf will be save in the current directory"
		    mv zapata.conf chan_dahdi.conf
		  fi
		fi
		;;
	 esac
if [ -f /etc/redfone.conf ]; then
	mv /etc/redfone.conf /etc/redfone.conf.bak
fi

cp redfone.gen /etc/redfone.conf

