#!/bin/sh -x
# /bin/setabbrnewip
# Sets a new IP based on DTMF digits collected by Asterisk
# Asterisk places its collected IP in /tmp/newip.txt
# Adapted from /bin/setnewip for last octet entry by TLG

if [ ! -f /tmp/newip.txt ]; then
  exit
  fi
  
  OCTET_A=`uci show network.lan.ipaddr | cut -d = -f2 | cut -d . -f1`
  OCTET_B=`uci show network.lan.ipaddr | cut -d = -f2 | cut -d . -f2`
  OCTET_C=`uci show network.lan.ipaddr | cut -d = -f2 | cut -d . -f3`
  OCTET_D=`cat /tmp/newip.txt`

if ping -c 5 $OCTET_A.$OCTET_B.$OCTET_C.$OCTET_D; then
   exit
   fi
  
  uci set network.lan.ipaddr=$OCTET_A.$OCTET_B.$OCTET_C.$OCTET_D
  uci set network.lan.gateway=$OCTET_A.$OCTET_B.$OCTET_C.1
  uci commit network
  
  reboot
  
