#!/bin/sh
# TODO: добавить поддержку crc
# TODO: чтение конфига (для возможности изменения, а не переписывания заново)
ZAPTEL=/etc/zaptel.conf
set +e

T=`mktemp`

dialog --menu "Interface type" 0 0 0  \
	"E1"	"E1"	\
	"T1"	"T1"	2> $T

if [ "`cat $T`" = "E1" ]; then
	IF=E1
	dialog \
		--default-item "ccs" \
		--menu "Framing type" 0 0 0 \
	    "ccs"	"ccs framing" \
		"cas"	"cas, not supported by Tau32-PCI/Lite" \
		2> $T
	FT=`cat $T`
	dialog --menu "Coding type" 0 0 0 \
		"hdb3"	"hdb3 coding (more common)" \
	    "ami"	"ami coding (rare)" \
		2> $T
	CT=`cat $T`

elif [ "`cat $T`" = "T1" ]; then
	IF=T1
	dialog --menu "Framing type" 0 0 0 \
	    "sf"	"d4 (sf, superframe) framing" \
		"esf"	"esf framing" \
		2> $T
	FT=`cat $T`
	dialog --menu "Coding type" 0 0 0 \
	    "ami"	"ami coding" \
		"b8zs"	"hdb3 coding" \
		2> $T
	CT=`cat $T`
fi

dialog \
		--default-item "ru" \
		--item-help \
		--menu "Tonezone" 0 0 0 \
	"ru"		"Russian"	"" \
	"us"		"us"	""	\
	us-old		us-old	""	\
	gr			Germany	""	\
	it			it	""	\
	fr			fr	""	\
	de			de	""	\
	uk			uk	""	\
	"fi"		"fi"	""	\
	jp			jp	""	\
	sp			sp	""	\
	no			no	""	\
	hu			hu	""	\
	lt			lt	""	\
	pl			pl	""	\
		2> $T
TONEZONE=`cat $T`

dialog --clear

echo "loadzone=$TONEZONE"		> $ZAPTEL
echo "defaultzone=$TONEZONE"	>> $ZAPTEL
echo "span=1,1,0,$FT,$CT"  >> $ZAPTEL
if [ "$IF" = "E1" ]; then
	echo "dchan=16" >> $ZAPTEL
	echo "bchan=1-15,17-31" >> $ZAPTEL
	SWITCHTYPE=euroisdn
else
	SWITCHTYPE=national
fi
		
dialog \
		--default-item "pri_cpe" \
		--item-help \
		--menu "Master/slave" 0 0 0 \
	    "pri_net"	"Master"  "" \
		"pri_cpe"	"Slave" "Used, when connecting Asterisk to PSTN" \
		2> $T
SIGNALLING=`cat $T`

dialog \
		--default-item "$SWITCHTYPE" \
		--item-help \
		--menu "Master/slave" 0 0 0 \
	national	"National ISDN type2" 	"Common in the US"\
	euroisdn	"EuroISDN"				""\
	qsig		"Minimalistic protocol" "Protocol to build a "network" with two or more PBX of different vendors"\
	ni1			"National ISDN type 1"	""\
	dms100		"Nortel DMS100"			""\
	4ess		"AT&T 4ESS"				""\
	5ess		"Lucent 5ESS"			""\
		2> $T
SWITCHTYPE=`cat $T`

dialog \
	--inputbox "Context for inbound calls from $IF" 0 0 \
	"pstn" 2> $T
CONTEXT=`cat $T`

dialog \
	--inputbox "rxgain for $IF" 0 0 \
	"0" 2> $T
RXGAIN=`cat $T`

dialog \
	--inputbox "txgain for $IF" 0 0 \
	"0" 2> $T
TXGAIN=`cat $T`

cat > /etc/asterisk/zapata.conf << EOF
[channels]
priindication=outofband
echotraining=800
echocancel=256
echocancelwhenbridged=yes
relaxdtmf=no
context=$CONTEXT
switchtype=$SWITCHTYPE
signalling=$SIGNALLING
usecallerid=yes
callerid=asreceived
; If we need to make it big, it is VERY-VERY bad thing
jitterbuffer=2
rxgain=$RXGAIN
txgain=$TXGAIN

group=0
channel => 1-15
channel => 17-31
EOF

rm -f "$T"

