#!/opt/bin/sh

ipui="qaz
xsw
edc
vfr
tgb
nhy"

check_nvox() {
if [ "$(ndmc -c "show version" | grep -o "nvox")" == "nvox" ]; then return; else exit; fi
}

check_lines() {
if [ "$(ndmc -c "show nvox sip-lines" | grep -o "line1001")" == "line1001" ]; then exit; fi
if [ "$(ndmc -c "show nvox sip-lines" | grep -o "line1002")" == "line1002" ]; then exit; fi
}

check_telephony_type() {
ndmc -c "show nvox info" | grep "tel_type:" | sed 's/tel_type: //g' | sed 's/[ \n\r]//g' 
}

wait_nvox_start() {
local cnt=0
ndmc -c "service nvox" > /dev/null
sleep 1
while true
do
if [ $cnt -eq 30 ]; then return; fi
telephonyType=$(check_telephony_type)
#echo "Telephony type: $telephonyType"
if [ "$telephonyType" != "" ]; then return; fi
let cnt+=1
/opt/bin/logger 'Waiting for NVOX started'
#echo "Waiting for NVOX started"
sleep 1
done
}

check_usb_module() {
local data=$(ndmc -c "show usb" | grep -o "Keenetic Linear")
if [ "$data" == "" ]; then data=$(ndmc -c "show usb" | grep -o "Keenetic Plus DECT"); fi
echo $data
}

check_dect_handsets() {
data=$(echo $(ndmc -c "show nvox handsets" | grep -o "handset:"))
if [ "$data" == "" ]; then echo exit; fi
}

get_line_id() {
local i=1
for i in 1 2 3 4 5 6 7 8 9 10
do
#echo $lineId
if [ "$(ndmc -c "show nvox sip-lines" | grep -o "id: $i")" == "" ]
then echo $i; return
fi
done
let i=1; echo $i; return
}

add_lines_for_dect() {
local i=1
local j=1
local cnt=0
local data=""
local lineId=0
while true
do
if [ $cnt -eq 3 ]; then return; fi
if [ "$(ndmc -c 'show nvox handsets' | sed "$i!d" | grep -o ":")" == "" ]
then let cnt+=1; let i+=1; continue
else let cnt=0
fi
data=$(ndmc -c 'show nvox handsets' | sed -n "$i!d /ipui:/p" | sed 's/ipui: //g' | sed 's/[ \n\r]//g')
if [ "$data" != "" ]
	then
		echo "Found IPUI" $data		
		lineId=$(get_line_id)
		userId="100$j"
		authId="user100$j"
		mask=$data
		/opt/bin/logger "Creating line $userId, binding to the handset with the IPUI $mask"
		send_sip_param ${lineId} ${userId} ${authId} ${mask}
		let cnt=0; let j+=1
		wait_nvox_start
	fi
let i+=1
done
}

add_lines_for_linear() {
local i=0
for i in $(seq 1 2)
do 
lineId=$(get_line_id)
userId="100$i"
authId="user100$i"
mask=$i
/opt/bin/logger "Creating line '$userId', binding to port $mask"
send_sip_param ${lineId} ${userId} ${authId} ${mask}
wait_nvox_start
done
}

disable_parallel_calls() {
ndmc -c "nvox parallel disable" > /dev/null
wait_nvox_start
}

send_sip_param() {
ndmc -c "no service nvox" > /dev/null
ndmc -c "nvox sip $1" > /dev/null
ndmc -c "nvox sip $1 name line$2" > /dev/null
ndmc -c "nvox sip $1 identity $2" > /dev/null
ndmc -c "nvox sip $1 priority $1" > /dev/null
ndmc -c "nvox sip $1 display-name $2" > /dev/null
ndmc -c "nvox sip $1 registration-uri localhost" > /dev/null
ndmc -c "nvox sip $1 login $3" > /dev/null
ndmc -c "nvox sip $1 password ast18-opkg-mipsel" > /dev/null
ndmc -c "nvox sip $1 domain localhost" > /dev/null
ndmc -c "nvox sip $1 proxy localhost:6060" > /dev/null
ndmc -c "nvox sip $1 disable-stun" > /dev/null
ndmc -c "nvox sip $1 incoming-mask $4" > /dev/null
ndmc -c "nvox sip $1 outgoing-mask $4" > /dev/null
ndmc -c "nvox sip $1 keepalive 0" > /dev/null
ndmc -c "nvox sip $1 reg-timeout 180" > /dev/null
ndmc -c "nvox sip $1 dtmf-mode rfc2833" > /dev/null
ndmc -c "nvox sip $1 transport udp" > /dev/null
ndmc -c "nvox sip $1 audio-protocol rtp" > /dev/null
ndmc -c "nvox sip $1 tls-security-mode sip-tls" > /dev/null
ndmc -c "nvox sip $1 codec g711u" > /dev/null
ndmc -c "nvox sip $1 lock-codec" > /dev/null
ndmc -c "nvox sip $1 digit-map \"999x|9000|80x|[12]xxx|[1-9]*x.|001\"" > /dev/null
}

#============================ MAIN ============================
check_nvox
#echo "NVOX installed"
usbModule=$(check_usb_module)
if [ "$usbModule" == "" ]; then exit; fi
#echo "USB module: $usbModule" 
wait_nvox_start
#echo "NVOX started"
/opt/bin/logger "Found Keenetic phone station."
/opt/bin/logger "USB module: $usbModule."
if [ "$usbModule" == "Keenetic Plus DECT" ]; then check_dect_handsets; fi
#echo "Checking lines exist"
check_lines
#### Creating lines
if [ "$telephonyType" == "DECT" ]; then add_lines_for_dect; fi
if [ "$telephonyType" == "FXS" ]; then add_lines_for_linear; fi
disable_parallel_calls
ndmc -c "copy running-config startup-config" > /dev/null

