#! /bin/sh

# dialog(C2)

TDDIN=2

STATE=0
BT="Welcome at the installation of the AVM ISDN-Controller C2"
CALLER=

# Texts

STATE1TEXT="Type of BRI (1)\n\nYou can chose for every S0 interface the type \
of the\nBRI: either point-to-point or point-to-multipoint.\nThe default \
setting is point-to-multipoint for all\nS0 interfaces.\n\nDo you want to \
keep the default setting?\n\nNote: The AVM ISDN-Controller C2 supports \
D-channel\n      protocol DSS1 (Euro ISDN)\n"

STATE2TEXT="Type of BRI (2)\n\nYou can change the type of BRI for all S0 \
interfaces\nat once or individually.\n"

while [ $STATE -ne 13 ]; do

	ok=0
	case $STATE in
	0)
		dialog  --backtitle "$BT ($STATE)" --clear \
			--yesno "$STATE1TEXT" 16 60
		case $? in
		0)
			TDDI1=0
			TDDI2=0
			STATE=8
			;;
		1)
			STATE=1
			;;
		255)
			STATE=13
			;;
		esac
		;;
	1)
		dialog  --backtitle "$BT ($STATE)" --clear \
			--radiolist "$STATE2TEXT" 13 60 2 \
				A "Set type of BRI for all controllers" on \
				I "Set type of BRI individually" off \
					2> res
		if [ $? -eq 0 ]; then
			DUMMY=`cat res`
			clear
			if [ "$DUMMY" == "A" ]; then
				STATE=2
			else
				STATE=3
			fi
		else
			STATE=0
		fi
		;;
	2)	
		FEEDBACK=
		STATE=4
		;;
	3)	
		TDDIX=1
		STATE=5
		;;
	4)	
		if [ "$FEEDBACK" != "" ]; then
			if [ $FEEDBACK -eq 0 ]; then
				if [ "$ANSWER" == "P" ]; then
					TDDI1=1
					TDDI2=1
				else
					TDDI1=0
					TDDI2=0
				fi
				STATE=8
			else
				STATE=1
			fi
		else
			CALLER=4
			STRING="Type of BRI (for all S0 interfaces)"
			STATE=7
		fi
		;;
	5)	
		if [ $TDDIX -le $TDDIN ]; then
			CALLER=6
			STRING="Type of BRI (for S0 interface $TDDIX)"
			STATE=7
		else
			STATE=8
		fi
		;;
	6)	
		if [ $FEEDBACK -eq 0 ]; then
			if [ "$ANSWER" == "P" ]; then
				eval "TDDI$TDDIX=1"
			else
				eval "TDDI$TDDIX=0"
			fi
			TDDIX=`expr $TDDIX + 1`
			STATE=5
		else
			STATE=1
		fi	
		;;
	7)	
		dialog  --backtitle "$BT ($STATE)" --clear \
			--radiolist "$STRING" 9 60 2 \
				M "Point-to-multipoint" on \
				P "Point-to-point" off \
					2> res
		FEEDBACK=$?
		if [ $? -eq 0 ]; then
			ANSWER=`cat res`
		fi
		STATE=$CALLER
		;;
	8)
		STRING="You have chosen the following:\n"
		for TDDIX in 1 2; do
			STRING="$STRING\nS0 interface $TDDIX:"
			eval "DUMMY=\$TDDI$TDDIX"
			if [ $DUMMY -eq 1 ]; then
				STRING="$STRING point-to-point"
			else
				STRING="$STRING point-to-multipoint"
			fi
		done
		echo -e "* Dialog:\n$STRING" >> install.log
		STRING="$STRING\n"
		dialog	--backtitle "$BT ($STATE)" --clear \
			--menu "$STRING" 15 45 3 \
				OK "All settings correct?" \
				Error "Correct the settings" \
				Quit "Quit installation" 2> res
		echo -n "* Dialog " >> install.log
		if [ $? -ne 0 ]; then
			echo "aborted" >> install.log 
			STATE=13
		else
			DUMMY=`cat res`
			case $DUMMY in
			OK)	
				echo "settings accepted" >> install.log 
				ok=1
				STATE=13
				;;
			Error)	
				echo "settings incorrect" >> install.log 
				STATE=0
				;;
			Quit)
				echo "quit" >> install.log 
				STATE=13
				;;
			esac
		fi
		;;
	esac
done

clear
/bin/rm -f res

TPROTO=dss1
TFILE=dss1

if [ $ok -eq 1 ]; then
	echo -e "Last steps...\n"
else
	echo -e "\aInstallation aborted.\n"
	exit 0
fi

