#!/bin/bash

IP_ADDRESS=$1
WIFI_CHANNEL=$2

echo "<pre>Nessun NaawiGo mini su questo indirizzo<pre>" > /tmp/survey.txt

if [ -z $IP_ADDRESS ]
then
  echo Use $0 IP_ADDRESS [ new_channel ]
  exit 0
fi 
if curl -s -m 5 http://$IP_ADDRESS/cgi-bin/simple/login.lua | grep -q NAAW ;
then
  NAAW_AUTH=`curl -s -m 5 -c /tmp/winext.cookie --data-binary 'user=root&password=spintec' http://$IP_ADDRESS/cgi-bin/simple/login.lua |  tr ';' '\n' | grep document.cookie | tr '"' '\n' | grep naaw_auth`";naaw_user=root;"

  echo Cookie [ $NAAW_AUTH ]

  rm -f /tmp/simple_lua.html
  VERIFY_URL="http://"$IP_ADDRESS"/cgi-bin/simple/simple.lua"
  echo Verify URL [ $VERIFY_URL ]
  curl -s -m 5 -b $NAAW_AUTH $VERIFY_URL > /tmp/simple_lua.html
  NAAW_FIRMWARE=`cat /tmp/simple_lua.html | sed 's/tr/\n/g' | grep "Fw Version:" | sed 's/th//g; s/td//g;' | tr -d '<>/' | cut -d : -f2`
  NAAW_IF_NAME="wifi0"

  if [ "A${NAAW_FIRMWARE:0:3}" = "A4.2" ]
  then
    NAAW_IF_NAME="radio0"
  fi
  echo "Firmware : $NAAW_FIRMWARE  WiFi Device: $NAAW_IF_NAME"
 

  if [ "A$WIFI_CHANNEL" = "AS" ]
  then
    rm -f /tmp/survey.txt
    rm -f /tmp/survey.html
    curl -s -m 15 http://$IP_ADDRESS/cgi-bin/simple/ssurvey.lua"?if=" > /tmp/survey.html
    echo "<pre>" > /tmp/survey.txt
#    VERIFY_URL="http://"$IP_ADDRESS"/cgi-bin/simple/simple.lua"
#    echo Verify URL [ $VERIFY_URL ]
#    OP_MODE=`curl -s -m 5 -b $NAAW_AUTH $VERIFY_URL | sed 's/tr/\n/g' | grep Profile: | sed 's/th//g; s/td//g;' | tr -d "<>/" | cut -d ":" -f2`
    OP_MODE=`cat /tmp/simple_lua.html | sed 's/tr/\n/g' | grep Profile: | sed 's/th//g; s/td//g;' | tr -d "<>/" | cut -d ":" -f2`

    echo -n "<br>Modalita' operativa : "$OP_MODE >> /tmp/survey.txt 
    ping -c2 $IP_ADDRESS
    MAC_ADDR=`arp -n | grep $IP_ADDRESS | awk '{ print $3; }'`
    LAST_MAC_LETTER=${MAC_ADDR:16:1}
    echo " Indirizzo MAC: " ${MAC_ADDR:0:16}`echo $LAST_MAC_LETTER | tr "02468ACE" "13579BDF"` "<br><br>" >> /tmp/survey.txt
    lesspipe /tmp/survey.html 
    lesspipe /tmp/survey.html >> /tmp/survey.txt
    echo "</pre>" >> /tmp/survey.txt
    
    exit 0
  fi
  
  if [ ! "A$WIFI_CHANNEL" = "A" ]
  then

    echo Setting Channel $WIFI_CHANNEL 

    CHANNEL_URL="http://"$IP_ADDRESS"/cgi-bin/simple/douci.lua?c=wireless%2C$NAAW_IF_NAME%2Cchannel%2C"$WIFI_CHANNEL"%3B&a=commit"
    echo Update URL [ $CHANNEL_URL ]

    curl -s -m 5 -b $NAAW_AUTH $CHANNEL_URL > /dev/null 2>&1
    sleep 3
  fi
  VERIFY_URL="http://"$IP_ADDRESS"/cgi-bin/simple/ap.lua"
  echo Verify URL [ $VERIFY_URL ]
  NEW_CNANNEL=""
  for (( i=0; $i<5; i++ ))
  do
    NEW_CHANNEL=`curl -s -m 5 -b $NAAW_AUTH $VERIFY_URL | grep Radio | sed 's/class="uci"/\n/g' | grep "wireless,.*,channel" | sed 's/option/\n/g' | grep selected | cut -d '"' -f2 `
    echo "$i: Checking Actual Channel [$NEW_CHANNEL]"
    
    if [ "A$NEW_CHANNEL" != "A" ]
    then
      break
    fi  
    sleep 3
  done
  
  echo Actual Channel: $NEW_CHANNEL

#  VERIFY_URL="http://"$IP_ADDRESS"/cgi-bin/simple/simple.lua"
#  echo Verify URL [ $VERIFY_URL ]
#  OP_MODE=`curl -s -m 5 -b $NAAW_AUTH $VERIFY_URL | sed 's/tr/\n/g' | grep Profile: | sed 's/th//g; s/td//g;' | tr -d "<>/" | cut -d ":" -f2`
  OP_MODE=`cat /tmp/simple_lua.html | sed 's/tr/\n/g' | grep Profile: | sed 's/th//g; s/td//g;' | tr -d "<>/" | cut -d ":" -f2`

  echo Operating Mode: $OP_MODE

  if [ -z $NEW_CHANNEL ]
  then
    NEW_CHANNEL=15
  fi
  if echo $OP_MODE | grep -iq repeater ;
  then 
    NEW_CHANNEL=$(( $NEW_CHANNEL + 16 ))
  fi

  exit $NEW_CHANNEL
else
  echo No NaawiGO on $IP_ADDRESS
  exit 0
fi


