#!/opt/bin/sh

lineName=""
trunkPrefix=""
trunkName=""
trunkPwd=""
trunkDomain=""
trunkProxy=""
trunkProxyPort=""
trunkTransport=""
lineId=0
cfgChange=0

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

check_ast_line() {
cat /tmp/run/nvox.conf | grep "sip.lines\[$1\].name" | cut -d "=" -f2 | grep -o "*"
}

get_line_cfg() {
lineName=$(cat /tmp/run/nvox.conf | grep "sip.lines\[$1\].name" | cut -d "=" -f2)
trunkPrefix=$(cat /tmp/run/nvox.conf | grep "sip.lines\[$1\].name" | cut -d "=" -f2 | cut -d* -f1)
trunkName=$(cat /tmp/run/nvox.conf | grep "sip.lines\[$1\].name" | cut -d "=" -f2 | cut -d* -f2)
trunkName=$(echo "trunk$trunkName")
trunkUid=$(cat /tmp/run/nvox.conf | grep "sip.lines\[$1\].sip_id" | cut -d ":" -f2| cut -d "@" -f1 -s)
trunkAid=$(cat /tmp/run/nvox.conf | grep "sip.lines\[$1\].login" | cut -d "=" -f2)
trunkPwd=$(cat /tmp/run/nvox.conf | grep "sip.lines\[$1\].password" | cut -d "=" -f2)
trunkDomain=$(cat /tmp/run/nvox.conf | grep "sip.lines\[$1\].domain" | cut -d "=" -f2)
trunkProxy=$(cat /tmp/run/nvox.conf | grep "sip.lines\[$1\].proxy" | cut -d "=" -f2 | cut -d ";" -f1 | cut -d ":" -f2 -s)
trunkProxyPort=$(cat /tmp/run/nvox.conf | grep "sip.lines\[$1\].proxy" | cut -d '=' -f2 | cut -d ':' -f3 -s | cut -d ';' -f1)
if [ "$trunkProxyPort" == "" ]; then trunkProxyPort="5060"; fi
trunkTransport=$(cat /tmp/run/nvox.conf | grep "sip.lines\[$1\].proxy" | cut -d '=' -f3 | cut -d ';' -f1)
}

check_trunk_exists() {
cat /opt/etc/asterisk/pjsip.conf | grep -o "\[$1\]"
}

write_trunk_cfg() {
echo; echo "Creating SIP trunk '$trunkName'"
sed -i "s/ITSP-NAME/${trunkName}/g" /opt/etc/asterisk/pjsip.conf
sed -i "s/SIP-DOMAIN/${trunkDomain}/g" /opt/etc/asterisk/pjsip.conf
sed -i "s/SIP-UID/${trunkUid}/g" /opt/etc/asterisk/pjsip.conf
sed -i "s/SIP-AID/${trunkAid}/g" /opt/etc/asterisk/pjsip.conf
sed -i "s/SIP-PWD/${trunkPwd}/g" /opt/etc/asterisk/pjsip.conf
sed -i "s/SIP-TRANSPORT/${trunkTransport}/g" /opt/etc/asterisk/pjsip.conf
sed -i "s/SIP-PROXY/${trunkProxy}/g" /opt/etc/asterisk/pjsip.conf
sed -i "s/SIP-PORT/${trunkProxyPort}/g" /opt/etc/asterisk/pjsip.conf
echo "Done."
}

write_outgoing_dialplan() {
echo; echo "Add outgoing dialplan for SIP trunk '$trunkName'"
sed -i "s/\;include => EXTERNAL-OUTGOING/include => ${trunkName}-outgoing\n\;include => EXTERNAL-OUTGOING/g" /opt/etc/asterisk/extensions.conf
sed -i "s/ITSP-NAME/${trunkName}/g" /opt/etc/asterisk/extensions.conf
sed -i "s/PREFIX/${trunkPrefix}/g" /opt/etc/asterisk/extensions.conf
echo "Done."
}

add_pjsip_template() {
cat /opt/etc/asterisk/scripts/trunk-pjsip-template.conf >> /opt/etc/asterisk/pjsip.conf
}

add_extensions_template() {
cat /opt/etc/asterisk/scripts/trunk-extensions-template.conf >> /opt/etc/asterisk/extensions.conf
}

restart_ast() {
asterisk -x "core restart now"
}

show_trunk_registration() {
asterisk -x 'pjsip show registrations'
}

get_line_id() {
local i=4
local data=""
local lineId
local cnt=0
while true
do
if [ $cnt -eq 3 ]; then return; fi
data=$(ndmc -c "show nvox sip-lines" | sed "$(echo $i)!D" | cut -d ":" -f2 -s | sed -r 's/[\n\r]//g')
if [ "$(echo $data)" == "" ]; then let cnt+=1; else let cnt=0; fi
#echo "Data:" $data
if [ "$(echo $data)" == "$1" ]; then let j=i-1; data=$(ndmc -c "show nvox sip-lines" | sed "$(echo $j)!D" | cut -d ":" -f2 -s ); echo $data; return; fi
let i+=1
done
}

disable_nvox_line() {
echo "Disabling NVOX line" $lineName
ndmc -c "nvox sip $1 disable"
ndmc -c "copy running-config startup-config"
}

#============================ MAIN ============================
check_nvox
echo "NVOX found"
for lineId in 0 1 2 3 4 5 6 7 8 9
do
if [ "$(check_ast_line $lineId)" == "*" ]
then
echo "Line $lineId has name with prefix"
get_line_cfg ${lineId}
echo "Trunk name: $trunkName"
if [ "$(check_trunk_exists $trunkName)" != "" ]; then continue; fi
echo "Found trunk config in Line $lineId"
echo "Line name: " $lineName
echo "Prefix: " $trunkPrefix
echo "Trunk name: " $trunkName
echo "User ID: " $trunkUid
echo "Auth ID: " $trunkAid
echo "Pwd: " $trunkPwd
echo "Domain: " $trunkDomain
echo "Proxy: " $trunkProxy
echo "Proxy port: " $trunkProxyPort
echo "Transport: " $trunkTransport
echo
disable_nvox_line $(get_line_id $lineName)
echo "Writing trunk config to pjsip.conf and extensions.conf"
add_pjsip_template; write_trunk_cfg; add_extensions_template; write_outgoing_dialplan
let cfgChange=1
fi
done
if [ $cfgChange -eq 1 ]; then echo "Restarting Asterisk core"; restart_ast; fi


