#! /bin/sh

# dialog(T1)

TTPROTO=dss1
TTFILE=dss1
TDDIN=1
TDDI1=0
TOK=n
TIOADDR=150
TIRQ=5

prompt () {
	eval $3=\"$2\"
	echo -n "$1 [$2]: "
	read tmp
	if [ -n "$tmp" ]; then 
		eval $3=\"$tmp\"
	fi
}

ask_bool () {
	default=`eval echo '$'$2`
	if [ ! "$default" ]; then 
		default=n
	fi
	answer=""
	while [ "$answer" != "n" -a "$answer" != "y" ]; do
		prompt "$1" "$default" answer
	done
	eval "$2=$answer"
}

ask_number () {
	default=`eval echo '$'$2`
	if [ ! "$default" ]; then
		default=0
	fi
	answer=""
	while [ "$answer" == "" ]; do
		prompt "$1" "$default" answer
		numlen=`expr "$answer" : "[0-9]\+"`
		strlen=`expr length "$answer"`
		if [ $numlen -ne $strlen ]; then
			answer=""
		else
			if [ $answer -ge $3 ]; then
				answer=""
			fi
		fi
	done
	eval "$2=$answer"
}

protochk () {
	case $1 in
	dss1|1tr6)
		true
		;;
	*)
		false
		;;
	esac	
}

iochk () {
        case $1 in
        150|250|300|340)
                true
                ;;
        *)
                false
                ;;
        esac
}

irqchk () {
        case $1 in
        3|5|7|10|11|12|15)
                true
                ;;
        *)
                false
                ;;
        esac
}

ask_str () {
	default=`eval echo '$'$2`
	answer=""
	while [ ! "$answer" ]; do
		prompt "$1" "$default" answer
		eval "$3 '$answer'"
		if [ $? -ne 0 ]; then
			answer=""
		fi
	done
	eval "$2=$answer"
}

while [ "$TOK" == "n" ]; do
	clear
	echo -e "\nWelcome at the installation of the AVM ISDN-Controller T1\n"

	ask_str "D-channel protocol     (dss1/1tr6)" TTPROTO protochk
	TTFILE=$TTPROTO
	if [ "$CARD" == "t1isa" -o "$CARD" == "t1bisa" ]; then
		ask_number "Number of the controller?    (0-3)" CARDNO 4
                ask_str "I/O address      (150/250/300/340)" TIOADDR iochk
                ask_str "IRQ number     (3,5,7,10,11,12,15)" TIRQ irqchk
	fi

	echo -e "\n\nYou have chosen the following:\n"
	echo "D-channel protocol       : $TTPROTO"
	if [ "$CARD" == "t1isa" -o "$CARD" == "t1bisa" ]; then
		echo "Number of the controller : $CARDNO"
                echo "I/O address              : $TIOADDR"
                echo "IRQ number               : $TIRQ"
	fi
	echo ""
	ask_bool "Are these settings correct? (y/n)" TOK
done

TPROTO=$TTPROTO
TFILE=$TTFILE

echo -e "\n"
