#!/bin/sh

#####Removing Old Configuration
rm -rf /etc/asterisk/sip.conf
rm -rf /etc/asterisk/mspd.conf
rm -rf /etc/asterisk/extensions.conf
rm -rf /etc/asterisk/voicemail.conf

ulaw=`uci get asterisk.general.codec_ulaw`
alaw=`uci get asterisk.general.codec_alaw`
g729=`uci get asterisk.general.codec_g729`
g722=`uci get asterisk.general.codec_g722`
ip=`uci get asterisk.general.ipaddr_other_ast`

codec=""
[ "$g729" = "yes" ] && codec=g729
[ "$g722" = "yes" ] && codec=g722,$codec
[ "$alaw" = "yes" ] && codec=alaw,$codec
[ "$ulaw" = "yes" ] && codec=ulaw,$codec

#######to change mac address in mspd.conf
mac=`ifconfig eth0 | grep HWaddr | cut -d ' ' -f 11`

##################################################################
#####################UD change in Script##########################
##################################################################
dst_ipaddr=`uci get asterisk.ud.dst_ipaddr`
sport=`uci get asterisk.ud.sport`
dport=`uci get asterisk.ud.dport`
all=`uci get asterisk.ud.all`
[ -n "$all" ] || all="0"
tdm_tx=`uci get asterisk.ud.tdm_tx`
[ -n "$tdm_tx" ] || tdm_tx="0"
tdm_rx=`uci get asterisk.ud.tdm_rx`
[ -n "$tdm_rx" ] || tdm_rx="0"
spu_iopram=`uci get asterisk.ud.spu_iopram`
[ -n "$spu_iopram" ] || spu_iopram="0"
spu_in=`uci get asterisk.ud.spu_in`
[ -n "$spu_in" ] || spu_in="0"
spu_out=`uci get asterisk.ud.spu_out`
[ -n "$spu_out" ] || spu_out="0"
packet_tx=`uci get asterisk.ud.packet_tx`
[ -n "$packet_tx" ] || packet_tx="0"
packet_rx=`uci get asterisk.ud.packet_rx`
[ -n "$packet_rx" ] || packet_rx="0"
ud_report=`uci get asterisk.ud.ud_report`
[ -n "$ud_report" ] || ud_report="0"

##################################################################
#####Creating sip.conf
echo ";
;DO NOT MODIFY, FILE AUTOMATICALLY GENERATED BY SCRIPT "configure_asterisk"
;

[general]
context=default                 ; Default context for incoming calls
bindport=5060                   ; UDP Port to bind to (SIP standard port is 5060)
bindaddr=0.0.0.0                ; IP address to bind to (0.0.0.0 binds to all)
srvlookup=yes                   ; Enable DNS SRV lookups on outbound calls
disallow=all
allow=$codec                ;also defines preference
dtmfmode=rfc2833

;A.B.C.D is IP-Address of other board
;replace A.B.C.D and reload configuration files
;register => 6789:6789@$ip

[2345]
type=peer
context=default   ; Where to start in the dialplan when this phone calls
username=2345; SIP username for registration
secret=2345; SIP password for registration
host=dynamic   ; Sip phone has a dynamic IP address
canreinvite=yes   ; allow RTP voice traffic to bypass Asterisk
insecure=invite

" >> /etc/asterisk/sip.conf

#####Creating mspd.conf
echo ";
;DO NOT MODIFY, FILE AUTOMATICALLY GENERATED BY SCRIPT "configure_asterisk"
;

[global]
context=default                     ;context for calls originating from this channel module
callgroups=1                        ;put calls to MSPD-channels into these groups
pickupgroups=1                      ;MSPD-channels can pickup calls in these groups
ipaddr=169.254.0.1                  ;CSP eth1 IP-address
allow=$codec                ;also defines preference
dtmfmode=inband

[device]
name=msp
csmfile=/dev/m821xx0                ; device-file for csm access (Do not modify)
dcifile=/dev/dci0                   ; device-file for dci access (Do not modify)
firmware=/lib/firmware/msp.axf      ; Firmware to load into device (Do not modify)
reset=0                             ; Reset device (Do not modify)
srcmac=$mac            ; MSP(ETH0) Mac Address (Do not modify)
dstmac=00:AA:BB:CC:DD:EE            ; ETH1 Mac Address (Do not modify)
ipaddr=169.254.0.2                  ; MSP IP-address that will be used (Must be in the same network as ipaddr paarmeter)

[tdmbus]
name=pri                            ;name of this TDM-bus
device=msp                          ;device of this TDM-bus
bus-id=0x0100                       ;STI type/index of this bus (0x0100 == pri0)
lines=4                             ;number of lines (timeslots 0..n-1)
istrunk=0                           ;not trunk -> can only be called via phone
param1=0x8380
param2=0x07FF
param3=0x03FF
param4=0x0000

" >> /etc/asterisk/mspd.conf

[ -n "$dst_ipaddr" ] && {
echo "
[ud_info]
ud_enable=$all
ud_dest_ip_addr=$dst_ipaddr
ud_src_port=$sport
ud_dest_port=$dport
ud_tdm_tx=$tdm_tx
ud_tdm_rx=$tdm_rx
ud_spu_io_params=$spu_iopram
ud_spu_instream=$spu_in
ud_spu_outstream=$spu_out
ud_pkt_tx=$packet_tx
ud_pkt_rx=$packet_rx
ud_report_cmd=$ud_report
" >> /etc/asterisk/mspd.conf
}

#####Creating extensions.conf
echo ";
;DO NOT MODIFY, FILE AUTOMATICALLY GENERATED BY SCRIPT "configure_asterisk"
;

[general]
static=yes
writeprotect=no

[default]
include => phones
include => parkedcalls
include => conference

[phones] 
" >> /etc/asterisk/extensions.conf
echo "exten => _9XXX,1,Macro(stdexten,MSPD_FXO/\${EXTEN:1},,104)

;connecting to other network which has 1XX numbers thru SIP protocol
;A.B.C.D is IP-Address of other board
;replace A.B.C.D and reload configuration files
exten => _4xx,1,Macro(stdexten,SIP/\${EXTEN}@$ip,,401)
;connecting to other network which has 2XX numbers thru SIP protocol
exten => _5xx,1,Macro(stdexten,SIP/\${EXTEN}@$ip,,401) " >> /etc/asterisk/extensions.conf

######Creating voicemail.conf
echo "
;
;DO NOT MODIFY, FILE AUTOMATICALLY GENERATED BY SCRIPT "configure_asterisk"
;

[general]
review=yes
format=gsm
serveremail=asterisk
attach=yes
skipms=3000
maxsilence=10
maxlogins=3
silencethreshold=128
emaildateformat=%A, %B %d, %Y at %r
sendvoicemail=yes  

[default]
" >> /etc/asterisk/voicemail.conf 



#####For T1 Phones Configuration
t1phones=`uci get asterisk.general.t1phones`
madgeT1line=13
phone=1
while [ "$phone" -le "$t1phones" ]
do
name=`uci get asterisk.t1_$phone.name`
number=`uci get asterisk.t1_$phone.number`
voicemail=`uci get asterisk.t1_$phone.voicemail`
ud_enable=`uci get asterisk.ud.phone$phone`
[ -n "$ud_enable" ] || ud_enable="0"

##################################################################
#################Echo Cancellation change in Script###############
##################################################################
field_ec_enable=`uci get asterisk.t1_$phone.field_ec_enable`
field_dc_filter_enable=`uci get asterisk.t1_$phone.field_dc_filter_enable`
field_hec_filter_enable=`uci get asterisk.t1_$phone.field_hec_filter_enable`
field_npl_control_enable=`uci get asterisk.t1_$phone.field_npl_control_enable`
npl_tune=`uci get asterisk.t1_$phone.npl_tune`
field_comfort_noise=`uci get asterisk.t1_$phone.field_comfort_noise`
field_ec_tail=`uci get asterisk.t1_$phone.field_ec_tail`

bit13=0
bit12=1
bit09=0
bit08=0
bit05=0

if [ $field_ec_enable ] ; then
bit15=1
else
bit15=0
fi

if [ $field_dc_filter_enable ] ; then
bit14=0
else
bit14=1
fi

if [ $field_hec_filter_enable ] ; then
bit11=1
else
bit11=0
fi

if [ $field_npl_control_enable ] ; then
bit10=0
else
bit10=1
fi

if [ $field_comfort_noise ] ; then
bit06=0
else
bit06=1
fi

case "$npl_tune" in
00) bit07=0
    bit04=0;;
01) bit07=0
    bit04=1;;
10) bit07=1
    bit04=0;;
esac
            
a=$bit15$bit14$bit13$bit12
b=$bit11$bit10$bit09$bit08
c=$bit07$bit06$bit05$bit04
d=$field_ec_tail

output="0x"
for i in $a $b $c $d
do
case "$i" in
0000) out=0;;
0001) out=1;;
0010) out=2;;
0011) out=3;;
0100) out=4;;
0101) out=5;;
0110) out=6;;
0111) out=7;;
1000) out=8;;
1001) out=9;;
1010) out=A;;
1011) out=B;;
1100) out=C;;
1101) out=D;;
1110) out=E;;
1111) out=F;;
esac

output=$output$out

done
##################################################################
###########Enhanced Echo Cancellation change in Script############
##################################################################
field_full_filter=`uci get asterisk.t1_$phone.field_full_filter`
field_fast_convergence_control=`uci get asterisk.t1_$phone.field_fast_convergence_control`

if [ $field_full_filter ] ; then
bit15=1
else
bit15=0
fi

if [ $field_fast_convergence_control ] ; then
bit12=1
else
bit12=0
fi

temp=$bit15\00$bit12
case "$temp" in
0000) out=0;;
0001) out=1;;
1000) out=8;;
1001) out=9;;
esac

en_echocan=0x$out\000

##################################################################
#####Creating mspd.conf
echo "
[phone]
name=phone`expr $phone - 1`
tdmbus=pri
timeslot=`expr 72 + 4 \* $phone`
callerid=$name <$number>
echocan=$output
enh_echocan=$en_echocan
ud_enable=$ud_enable
dialout_number=`expr $madgeT1line \* 100 + $phone`"  >> /etc/asterisk/mspd.conf

#####Dial Plan Configuration
#####Adding POTS Phone Dial Plan in extensions.conf
echo "exten => $number,1,Macro(stdexten,MSPD/phone`expr $phone - 1`,,$number)" >> /etc/asterisk/extensions.conf

#####Voicemail Configuration
#####Creating voicemail.conf
[ "$voicemail" == "enable" ] && echo "$number => $number,$number,root@localhost" >> /etc/asterisk/voicemail.conf

phone=`expr $phone + 1`
done

###################################

echo "" >> /etc/asterisk/extensions.conf
echo "" >> /etc/asterisk/voicemail.conf



#####For VoIP Phones Configuration
voipphones=`uci get asterisk.general.voipphones`
phone=1
while [ "$phone" -le "$voipphones" ]
do
name=`uci get asterisk.voip$phone.name`
number=`uci get asterisk.voip$phone.number`
authpwd=`uci get asterisk.voip$phone.authpwd`
protocol=`uci get asterisk.voip$phone.protocol`
voicemail=`uci get asterisk.voip$phone.voicemail`
canreinvite=`uci get asterisk.voip$phone.canreinvite`

#####Creating sip.conf
echo "
[$number]
type=friend
context=default
username=$number
secret=$authpwd
callerid=$name
host=dynamic" >> /etc/asterisk/sip.conf
if [ "$canreinvite" == "yes" ]; then
	echo "canreinvite=yes" >> /etc/asterisk/sip.conf
else
	echo "canreinvite=no" >> /etc/asterisk/sip.conf
fi

#####Dial Plan Configuration
#####Adding VoIP Phone Dial Plan in extensions.conf
echo "exten => $number,1,Macro(stdexten,SIP/$number,,$number)" >> /etc/asterisk/extensions.conf

#####Voicemail Configuration
#####Creating voicemail.conf
[ "$voicemail" == "enable" ] && echo "$number => $number,$number,root@localhost" >> /etc/asterisk/voicemail.conf

phone=`expr $phone + 1`
done

#####Completeing extensions.conf
echo "
exten => s,1,GotoIf(\$[\${LEN(\${ARG3})} > 0]?4)
exten => s,2,SetVar(VMBOX=\${MACRO_EXTEN})
exten => s,3,Goto(5)
exten => s,4,SetVar(VMBOX=\${ARG3})
exten => s,5,Dial(\${ARG1},20,t\${ARG2})
exten => s,6,Goto(s-\${DIALSTATUS},1)
exten => s-ANSWER,1,Hangup
exten => s-BUSY,1,Voicemail(b\${VMBOX})
exten => s-BUSY,2,Hangup
exten => _s-.,1,Voicemail(u\${VMBOX})
exten => _s-.,2,Hangup
" >> /etc/asterisk/extensions.conf

vm_enable=`uci get asterisk.general.vm`
vm_ext=`uci get asterisk.general.vm_main`
if [ "$vm_enable" == "yes" ];then
echo "
exten => $vm_ext,1,VoiceMailMain()
exten => $vm_ext,1,NoOp(\${EXTEN})
exten => $vm_ext,2,NoOp(\${MACRO_EXTEN})
exten => $vm_ext,3,Hangup()
" >> /etc/asterisk/extensions.conf
fi

call_record_ext=`uci get asterisk.general.call_record_ext`
if [ "$call_record_ext" ];then
echo "
exten => $call_record_ext,1,Answer()
exten => $call_record_ext,2,Record(/tmp/testrecordingGSM%d.gsm|2)
exten => $call_record_ext,3,NoOp()
exten => $call_record_ext,4,Wait(1)
exten => $call_record_ext,5,Playback(\${RECORDED_FILE})
exten => $call_record_ext,6,Hangup()
" >> /etc/asterisk/extensions.conf
fi

echo "
[macro-stdexten]
exten => s, 1, Dial(\${ARG1}, 25, tT)
exten => s, 2, SetVar(VMBOX=\${MACRO_EXTEN})
exten => s, 3, NoOp(\${MACRO_EXTEN})
exten => s, 4, NoOp(\${VMBOX})
exten => s, 5, Goto(s-\${DIALSTATUS},1)
exten => s-ANSWER,1,Hangup
exten => s-BUSY,1,Voicemail(b\${VMBOX})
exten => s-BUSY,2,Hangup
exten => _s-.,1,Voicemail(u\${VMBOX})
exten => _s-.,2,Hangup
#exten => s, 2, Goto(s, 102)
#exten => s, 102, Playback(vm-nobodyavail)
#exten => s, 103, Hangup()

" >> /etc/asterisk/extensions.conf

##############################################################
####################Conference Changes########################
##############################################################
echo "[conference]" >> /etc/asterisk/extensions.conf
confcount=`uci get asterisk.general.confcount`
conf=1
while [ "$conf" -le "$confcount" ]
do
name=`uci get asterisk.conf$conf.name`
number=`uci get asterisk.conf$conf.number`
echo "exten => $number,1,Conference($name,\"S|L\")" >> /etc/asterisk/extensions.conf
conf=`expr $conf + 1`
done
##############################################################
