#!/bin/sh
# Babylon installation script
# Copyright (C) 1997-2000 SpellCaster Telecommunications
# $Id: install,v 1.7 2001/06/18 16:43:24 mdj Exp $
# Released under the GNU Public License. See LICENSE file for details.
#
#
if [ "$UID" != "0" -a "$1" != "force" ]
then
	echo "Please run install as root"
	exit
fi
clear
#if [ ! -f /usr/include/linux/compile.h ]
#then
#	echo "*********************************************************"
#	echo "* WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! *"
#	echo "*********************************************************"
#	echo
#	echo "Cannot find the kernel header compile.h. There are three"
#	echo "possible reasons:"
#	echo " 1) Your kernel source is not located in /usr/src/linux "
#	echo " 2) The kernel source has not been installed"
#	echo " 3) The kernel has never been compiled on this machine"
#	echo
#	echo "While install will continue, Babylon may fail to operate"
#	echo "correctly. The kernal source code *MUST* match the version"
#	echo "of the operating kernel. Without the kernel source"
#	echo "install is unable to verify this is the case. "
#	echo
#	echo -n "Press any key to continue"
#	read JUNK
#	clear
#fi

SVER="1."
OS=`uname -s`
VERSION=`uname -r`
PLATFORM=$(uname -m | sed -e 's/i[3-6]86/i386/')

OS_SUPP="Linux"
PLAT_SUPP="i386 alpha"

IFS='@'

#--Early check if we're on a tested platform
case $OS-$VERSION-$PLATFORM in
Linux-2.[0234].*-i386)
	SUPP=Y
	;;
*)
	SUPP=N
	echo "Warning: $OS-$VERSION-$PLATFORM is an unsupported"
	echo "platform.  Expect things to fail."
	echo
	;;
esac

#
# Install the sample config
#
function askyn() {
	ANS=
	DEF_ANS=
	if [ -e .config.old ] ; then
		DEF_ANS=`grep "^$3=" .config.old | cut -d= -f2 | tr a-z A-Z`
	fi
	[ -z "$DEF_ANS" ] && DEF_ANS=$2

	until [ "$ANS" = "Y" -o "$ANS" = "N" ]
	 do
		if [ $DEF_ANS = Y ] ; then
			echo -n "$1 [Y/n]: "
		elif [ $DEF_ANS = N ] ; then
			echo -n "$1 [y/N]: "
		else
			echo "Someone forgot the default($DEF_ANS) answer... 1: $1  2: $2  3: $3"
			exit 1
		fi
		read ANS
		[ -z $ANS ] && ANS=$DEF_ANS
		ANS=$(echo $ANS | tr a-z A-z)
	 done

	echo >>.config "$3=$ANS"
	eval $3=$ANS
	export $3
}


echo "SpellCaster Babylon configuration"
echo "================================="
echo
echo "You must now select the components required to use Babylon on"
echo "your system with your hardware."
echo
echo "When prompted for information, the values shown in [ ] is the"
echo "default and will be selected if you press Enter or Return."
echo "For multiple choice questions, the CAPITAL letter choice is the"
echo "default."
echo

#
# Must be a platform we support
#
SUPPORTED_PLATFORM=$(echo $PLAT_SUPP | sed 's/ /x/g' | grep $PLATFORM)
if [ -z $SUPPORTED_PLATFORM ]
then
	echo "Babylon is currently not supported on your platform."
	echo "Please contact SpellCaster support for more information."
	exit 1
fi

if [ -e .config ] ; then
	mv -f .config .config.old
fi

rm -f .config

askyn "Do you want the sample configuration file installed?" N CONFIG_SAMPLE
askyn "Do you want to create a boot script to load Babylon?" N CONFIG_START_BOOT
if [ "$CONFIG_START_BOOT" = "Y" ] ; then
  askyn "Do you want to start Babylon when finished installing?" N CONFIG_START_IMMED
fi

typeset -i BHSCARDS=`/sbin/lspci -n | grep 1133 | grep e004 | wc --lines` 
typeset -i BHSEUROCARDS=`/sbin/lspci -n | grep 1133 | grep e005 | wc --lines`
typeset -i BHSDEUROCARDS=`/sbin/lspci -n | grep -i 114f | grep 71 | wc --lines`
BHSCARDSTOTAL=$[BHSCARDS+BHSEUROCARDS+BHSDEUROCARDS]
typeset -i PRICARDS=`/sbin/lspci -n | grep 1298 | grep '7[0f]' | wc --lines` 

if [ $BHSCARDSTOTAL -ge 1 ] ; then
   echo
   echo "DataCommute/BRI Pro PCI detected."
   askyn "Do you want to add support for the detected DataCommute/BRI Pro PCI?" Y CONFIG_BHS
   echo
   if [ "$CONFIG_BHS" = "Y" ] ; then
      CONFIG_DRIVERS_BHS="--with-babhisax"
      CONFIG_DRIVERS_SCB="--with-scb"
   fi
fi

if [ $PRICARDS -ge 1 ] ; then
   echo
   echo "DataCommute/PRI PCI detected. Adding support."
   askyn "Do you want to add support for the detected DataCommute/PRI PCI?" Y CONFIG_SCB
   echo
   if [ "$CONFIG_SCB" = "Y" ] ; then
      CONFIG_DRIVERS_SCB="--with-scb"
   fi
fi

# The test below does an arithmetic operation. BHSCARDS+PRICARDS is the total
# number of PRI cards detected. $# is the number of arguments on the command
# line (will change to a particular option later). So !$# will be 0 if there
# are options on the command line, 1 if none are on the line. The expression
# below multiplies these two. It'll be 0 if no cards are detected or if there
# are options on the command line. BAJ 11/08/00

if [ $[(BHSCARDSTOTAL+PRICARDS)*!$#] -eq 0 ] ; then
   echo
   if [ $[BHSCARDSTOTAL+PRICARDS] -eq 0 ] ; then
      echo "No PCI cards detected. Please select ISA cards to support."
   else
      echo "Please select ISA cards to support."
   fi
   echo
   CONFIG_DRIVERS_SCB=""
   askyn "Do you want DataCommute/BRI, PRI or Telecommute/BRI support?" N CONFIG_SCB
   if [ "$CONFIG_SCB" = "Y" ] ; then
	CONFIG_DRIVERS_SCB="--with-scb"
   fi

   CONFIG_DRIVERS_DCPLUS=""
   askyn "Do you want DataCommute/Plus support?" N CONFIG_DCPLUS
   if [ "$CONFIG_DCPLUS" = "Y" ] ; then
	CONFIG_DRIVERS_DCPLUS="--with-dcplus"
   fi
fi

# Determine distribution here if necessary.
if [ -f /etc/redhat-release ] ; then
	distribution="redhat"
	redhat_release=`cat /etc/redhat-release | cut -f1 -d"(" | cut -f3 -d")"` 
fi

if [ "$redhat_release" ] ; then
	CONFIG_MANDIR="--mandir=/usr/local/man"
	echo "Adding option for man pages $CONFIG_MANDIR"
fi



echo
echo '*****************************************************'
echo "Now configuring drivers... "
echo '*****************************************************'
echo

./configure $CONFIG_MANDIR $CONFIG_DRIVERS_SCB $CONFIG_DRIVERS_DCPLUS $CONFIG_DRIVERS_BHS
if [ $? -ne 0 ]
then
        echo '*****************************************************'
	echo "Configure failed. Please contact Spellcaster Support"
	echo "at support@spellcast.com.				   "
        echo '*****************************************************'
	exit 1
fi

echo '***************************************************'
echo 'Now compiling Babylon... This may take a while.'
echo '***************************************************'

make -s >./make-output 2>&1
if [ $? -ne 0 ]
then
	echo "Compilation (ie. make) failed."
	exit 1
fi
echo '***************************************************'
echo 'Compilation complete, now finishing up the install.'
echo '***************************************************'

#
# Move any existing hisax modules out of the way
#
/usr/bin/find /lib/modules/$VERSION/ -name 'hisax.o' | while read x ; do
	/bin/cp $x $x.bak
	/bin/rm -f $x
done


make -s install
if [ $? -ne 0 ]
then
	echo '***************************************************'
	echo "Installation failed."
	echo '***************************************************'
	exit 1
fi

echo
./mkbdev
if [ $? -ne 0 ]
then
        echo '***************************************************'
	echo "Could not create Babylon devices."
        echo '***************************************************'
	exit 1
fi

echo

if [ ! -d /etc/babylon ] ; then
	mkdir /etc/babylon
fi

echo 
echo 


unset IFS

bhs_known_cards="1133:e004 1133:e005 114f:0071"

/bin/rm -f /tmp/bab_installed_cards
/sbin/lspci -n | cut -f4 -d" " | while read i ; do
	count=`echo $bhs_known_cards | grep -i $i`
	if [ -n "$count" ] ; then
		echo $i >> /tmp/bab_installed_cards
	fi
done

typeset -i bhs_num_installed=0
if [ -f /tmp/bab_installed_cards ] ; then
	bhs_num_installed=`wc --lines /tmp/bab_installed_cards | cut -f7 -d" "`
fi

if [ $bhs_num_installed -ge 1 ] ; then
	echo "You have $bhs_num_installed DataCommute/BRI Pro PCI cards installed."
	bhs_tmp="it"
	if [ $bhs_num_installed -ge 2 ] ; then
		bhs_tmp="them"
	fi
	askyn "Would you like to configure $bhs_tmp now?" Y CONFIG_BHS_CARD
	if [ "$CONFIG_BHS_CARD" = "Y" ] ; then
		rm -fr /etc/babylon/bhs*.conf
		bhs_installed=`cat /tmp/bab_installed_cards`
		i=0
		for k in $bhs_installed ; do
			if [ "$k" = "1133:e004" -o "$k" = "1133:e005" ] ; then
				bhs_card_type="11"
			elif [ "$k" = "114f:0071" ] ; then
				bhs_card_type="35"
			fi

			echo ""
        		echo '***************************************************'
			echo "Configuring Card $i"
        		echo '***************************************************'
			echo ""
			proto_ans=""
			until [ "$proto_ans" = "NI1" -o "$proto_ans" = "DSS1" ] ; do
				echo -n "What protocol support do you want [NI1|DSS1]? "
				read proto_ans
			done
			if [ "$proto_ans" = "NI1" ] ; then
				card_proto=4
			elif [ "$proto_ans" = "DSS1" ] ; then
				card_proto=2
			fi

			echo "CARDTYPE=$bhs_card_type" >> /etc/babylon/bhs$i.conf
			echo "CARDPROTO=$card_proto" >> /etc/babylon/bhs$i.conf
			
			if [ $card_proto -eq 4 ] ; then
				echo -n "Enter the Telephone Number for channel 0 : "  
				read DN;
				echo "DN0=$DN" >> /etc/babylon/bhs$i.conf
				echo -n "Enter the SPID for channel 0 : "
				read SPID;
				echo "SPID0=$SPID" >> /etc/babylon/bhs$i.conf

				j=$[j+1]
				echo -n "Enter the Telephone Number for channel 1 : "  
				read DN;
				echo "DN1=$DN" >> /etc/babylon/bhs$i.conf
				echo -n "Enter the SPID for channel 1 : "  
				read SPID;
				echo "SPID1=$SPID" >> /etc/babylon/bhs$i.conf
			
			else
				echo -n "Enter the EAZ for channel 0 : "  
				read DN;
				echo "DN0=$DN" >> /etc/babylon/bhs$i.conf
				echo -n "Enter the MSN for channel 0 : "  
				read SPID;
				echo "SPID0=$SPID" >> /etc/babylon/bhs$i.conf

				j=$[j+1]
				echo -n "Enter the EAZ for channel 1 : "  
				read DN;
				echo "DN1=$DN" >> /etc/babylon/bhs$i.conf
				echo -n "Enter the MSN for channel 1 : "  
				read SPID;
				echo "SPID1=$SPID" >> /etc/babylon/bhs$i.conf
			fi
			j=$[j+1]
			i=$[i+1]
		done	

	fi
fi

echo

if [ -f /etc/redhat-release ] ; then
	INITFILE="/etc/rc.d/init.d"
elif [ -f /etc/debian_version ] ; then
	INITFILE="/etc/init.d"
elif [ -f /etc/slackware-release ] ; then
	INITFILE="/etc/rc.d"
else
	# default to redhat-style init scripts
	INITFILE="/etc/rc.d/init.d"
fi


if [ "$CONFIG_SCB" = "Y" ] ; then
	sed s/config_scb=N/config_scb=Y/g < $INITFILE/babylon.init > $INITFILE/babylon.out
else
	sed s/config_scb=Y/config_scb=N/g < $INITFILE/babylon.init > $INITFILE/babylon.out
fi
if [ "$CONFIG_DCPLUS" = "Y" ] ; then
	sed s/config_dcplus=N/config_dcplus=Y/g < $INITFILE/babylon.init > $INITFILE/babylon.out
else
	sed s/config_dcplus=Y/config_dcplus=N/g < $INITFILE/babylon.init > $INITFILE/babylon.out
fi
if [ "$CONFIG_BHS" = "Y" ] ; then
	sed s/config_bhs=N/config_bhs=Y/g < $INITFILE/babylon.init > $INITFILE/babylon.out
else
	sed s/config_bhs=Y/config_bhs=N/g < $INITFILE/babylon.init > $INITFILE/babylon.out
fi

if [ -f "$INITFILE/babylon.out" ] ; then
	/bin/mv $INITFILE/babylon.out $INITFILE/babylon.init
fi

echo

echo "*****************************************"
echo "Installation complete."
echo "*****************************************"

if [ "$CONFIG_START_IMMED" = "Y" ] ; then
	echo "*****************************************"
	echo "Starting Babylon..."
	echo "*****************************************"

        . babylon.start
fi

echo -n "Would you like to read the README file now? [Y/n]: "
read JUNK
if [ "$JUNK" != "n" -a "$JUNK" != "N" ]
then
	clear
	more README
fi

