#!/bin/bash
#
# Download Firmware Updates
#
# Usage: download-firmware-updates
#
# Return values:
#
#  1 - Firmware successfully downloaded
#  0 - Firmware download failed
#
# (C) Copyright 2019, Bogen Communications, Inc. All rights reserved.
#

compare_versions () {
    if [[ $1 == $2 ]]
    then
        #echo "EQUAL"
        return "0"
    fi
    local IFS=.
    local i v1=($1) v2=($2)
    # fill in empty fields with 0s
    for ((i=${#v1[@]}; i<${#v2[@]}; i++))
    do
        v1[i]=0
    done
    for ((i=0; i<${#v1[@]}; i++))
    do
        if [[ -z ${v2[i]} ]]
        then
            # fill in empty fields with 0s
            v2[i]=0
        fi
        if ((10#${v1[i]} > 10#${v2[i]}))
        then
            #echo "GREATER"
            return "1"
        fi
        if ((10#${v1[i]} < 10#${v2[i]}))
        then
            #echo "LESS"
            return "2"
        fi
    done
    return "0"
}

NQ_PRODUCT=STANDALONE

if [ -z "`ssh-keygen -F bogen-ssu.bogen.com`" ];
then
   ssh-keyscan -H bogen-ssu.bogen.com >> ~/.ssh/known_hosts 2> /dev/null
fi

rm -f /tmp/NyqFirmware.txt

export SSHPASS="s~jYh6g}zGwb"

sshpass -e sftp -2 -oServerAliveInterval=30 -oServerAliveCountMax=20 -oBatchMode=no -b - ftp_bg_user@bogen-ssu.bogen.com << EOF 2> /dev/null | grep "^Nyq" > /tmp/NyqFirmware.txt
cd FIRMWARE/$NQ_PRODUCT/LATEST
ls -1
quit
EOF

if [ ! -s /tmp/NyqFirmware.txt ];
then
   rm -f /tmp/NyqFirmware.txt
   echo "0"
   exit
fi

FD_STATUS=0
DID_DASH_RM=0
LATEST_VERSION=0
FILE_PREFIX="NyqUpdate-"
FILE_SUFFIX=".tar.gz"
for fname in `cat /tmp/NyqFirmware.txt`
do
   if [ ! -e /srv/tftp/$fname ];
   then
      NEW_FW_VER="$(echo $fname | sed -e 's/NyqUpdate-\(.*\).tar.gz/\1/')"
      #echo "$NEW_FW_VER"
      compare_versions $NEW_FW_VER $LATEST_VERSION
      IS_NEW_FIRMWARE="$?"
      #echo "IS_NEW_FIRMWARE=$IS_NEW_FIRMWARE"
      if [ "$IS_NEW_FIRMWARE" = "1" ];
      then
         LATEST_VERSION=$NEW_FW_VER
      fi
   fi
done

#echo "Get latest version: $LATEST_VERSION"
LATEST_FILE_NAME="${FILE_PREFIX}${LATEST_VERSION}${FILE_SUFFIX}"
#echo "   File name: $LATEST_FILE_NAME"



   # TODO: Check for available disk space befor downloading
   if [ ! -e /srv/tftp/$LATEST_FILE_NAME ];
   then
      notes=${LATEST_FILE_NAME/NyqUpdate-/Release-Notes-Firmware_Ver.}
      notes=${notes/.tar.gz/.txt}
      sshpass -e sftp -2 -oServerAliveInterval=30 -oServerAliveCountMax=20 -oBatchMode=no -b - ftp_bg_user@bogen-ssu.bogen.com << EOF2 > /dev/null 2>&1
lcd /tmp
cd FIRMWARE/$NQ_PRODUCT/LATEST
get $LATEST_FILE_NAME
cd /FIRMWARE
get $notes
quit
EOF2

INCOMING_DIR=/opt/bogen/incoming

      #
      # Move downloaded firmware to tftp directory
      #
      if [ -e /tmp/$LATEST_FILE_NAME ];
      then
         /bin/sync
         /bin/gunzip -t /tmp/$LATEST_FILE_NAME > /dev/null 2>&1
         if [ $? -eq 1 ];
         then
            rm -f /tmp/$LATEST_FILE_NAME;
            rm -f /tmp/$notes
            continue
         fi
         if [ "$DID_DASH_RM" = "0" ];
         then
            /usr/local/bin/dash-text -r -t Firmware > /dev/null 2>&1
            DID_DASH_RM=1
         fi
         cp -f /tmp/$LATEST_FILE_NAME $INCOMING_DIR/$LATEST_FILE_NAME
         chown -f www-data $INCOMING_DIR/$LATEST_FILE_NAME
         chgrp -f www-data $INCOMING_DIR/$LATEST_FILE_NAME
         chmod -f 664 $INCOMING_DIR/$LATEST_FILE_NAME
         rm -f /tmp/$LATEST_FILE_NAME
         FD_STATUS=1
      fi
   fi

rm -f /tmp/NyqFirmware.txt

echo $FD_STATUS
