#!/bin/bash

if [ $# -lt 2 ]
then
  echo "USAGE $0 <channel> <IP1> [ <IP2> ...]"
  exit 255
fi

if [ $1 != DEBUG ]
then
  bash -x $0 DEBUG "$@" >/tmp/manage_naawigo_change_channel.log 2>&1
  exit
fi
shift

FIRMWARE_OK=igominiN
WIFI_CHANNEL=$1
shift
IP_LIST="$*"

RESULT=/tmp/naaw_change_channel_result
rm -f $RESULT

function get_firmware()
{
  IP_ADDRESS=$1
  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.$IP_ADDRESS --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 ] >&2

    rm -f /tmp/simple_lua.$IP_ADDRESS.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.$IP_ADDRESS.html
    NAAW_FIRMWARE=`cat /tmp/simple_lua.$IP_ADDRESS.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 [ -z "$NAAW_FIRMWARE" ] # Maybe NaawigoN with provisioning system
    then
      NAAW_IF_NAME="radio0"
      rm -f /tmp/hedera_info.$IP_ADDRESS.txt
      ssh-keygen -f "/root/.ssh/known_hosts" -R $IP_ADDRESS
      
      scp -o StrictHostKeyChecking=no -i /opt/euro-beta/systools/id_e3g_rsa provision@$IP_ADDRESS:/var/hedera/$IP_ADDRESS /tmp/hedera_info.$IP_ADDRESS.txt > /dev/null 2>&1  
      if [ ! -f /tmp/hedera_info.$IP_ADDRESS.txt ]
      then
        echo "NOT_READY"
      fi
      NAAW_FIRMWARE=`cat /tmp/hedera_info.$IP_ADDRESS.txt | grep host | head -1 | cut -d'=' -f2`
#      NAAW_FIRMWARE=`ssh -o StrictHostKeyChecking=no -i /opt/euro-beta/systools/id_e3g_rsa provision@$IP_ADDRESS "cat /var/hedera/$IP_ADDRESS" | grep host | head -1 | cut -d'=' -f2`
      echo "HederaInfo : $NAAW_FIRMWARE  WiFi Device: $NAAW_IF_NAME" >&2
#      rm -f /tmp/hedera_info.$IP_ADDRESS.txt
    fi
#    rm -f /tmp/simple_lua.$IP_ADDRESS.html
    echo $NAAW_FIRMWARE
  fi  
}

function prepare_script()
{
  CHAN=$1
  IP_ADDRESS=$2
  FNAME=/tmp/change_channel.$IP_ADDRESS
  
  cat <<EOF >$FNAME
#!/bin/ash
sleep 5
/lib/provisioning-system/change_channel radio0 $CHAN && /lib/provisioning-system/reboot 3
EOF
  scp -o StrictHostKeyChecking=no -i /opt/euro-beta/systools/id_e3g_rsa $FNAME provision@$IP_ADDRESS:/tmp/change_channel
  RES=$?
  rm -f $FNAME
  return $RES
}

function run_script()
{
  IP_ADDRESS=$1
  ssh -o StrictHostKeyChecking=no -i /opt/euro-beta/systools/id_e3g_rsa provision@$IP_ADDRESS 'nohup ash /tmp/change_channel >/dev/null 2>&1' &
  PIDSSH=$$
  for((i=0;i<5;i++))
  do
    sleep 1
    if ! ps -p $PIDSSH | grep -q ssh 
    then
      return
    fi
  done
  kill $PIDSSH
}

#get_firmware 192.168.160.115
#exit
rm -f /tmp/fw
FW_STATUS=`
for i in $IP_LIST
do
  { 
    STATUS=$(get_firmware $i)
    if echo "$STATUS" | grep -q NOT_READY 
    then
      echo "ERROR: Access point $i non e' ancora pronto, riprovare piu' tardi %" | tee $RESULT
    elif ! echo "$STATUS" | grep -q $FIRMWARE_OK 
    then
      echo "ERROR: Firmware incompatibile $i %" | tee $RESULT
    fi
  } &
done
wait
`

if echo "$FW_STATUS" | grep -q ERROR
then
  echo "Uno degli access point ha la versione del firmware incompatibile" | tee $RESULT
  echo $FW_STATUS | sed 's/% /\n/g' | tr -d % | tee -a $RESULT
  exit 255
fi

COPY_STATUS=`
for i in $IP_LIST
do
  { 
    prepare_script $WIFI_CHANNEL $i ||
    echo "ERROR: errore durante la preparazione dello script al $i %" | tee $RESULT
  } &  
done
wait
`
if echo $COPY_STATUS | grep -q ERROR
then
  echo $COPY_STATUS | sed 's/% /\n/g' | tr -d %  
  exit 255
fi

for i in $IP_LIST
do
  echo xxx >/dev/null
  run_script $i &
done
wait
echo "Gli access point si stanno riavviando, tra 1 minuto e mezzo circa dovrebbero essere pronti." | tee $RESULT
exit 0