#!/bin/bash

######################################################################################

function display_usage()
{
  echo ""
  echo "  usage :"
  echo "           $0  ip_adress"
  echo ""
  echo ""
}

######################################################################################

if [[ $RBF_PATH == "" ]]; then
  RBF_PATH="/usr/share/faster/rbf"
fi

nodes_list=$RBF_PATH/hardware_nodes.list
basad="0x40"
epcs_ver="64"

ip_address=$1
no_off_on=$2
if [[ $ip_address == "" ]]; then
   ip_address=192.168.1.204
fi

fping=$(faster_ping $ip_address 0 --verbose)
read FASTER PING IP NODE ARROW CLOCK COLON TIME SECOND MOTHERBOARD NODETYPE < <(echo $fping)

if [[ $NODETYPE == "SYST_NODE" ]]; then
   echo ""
   echo "    Mother board : $MOTHERBOARD"
else
   echo ""
   echo "    Unknown board, bye."
   echo ""
   exit 1
fi


if [[ $MOTHERBOARD == "NIM" ]]; then
   read -ra filename <<< "$(cat $nodes_list | grep "SYST_NODE-NIM-")"
elif [[ $MOTHERBOARD == "AMC" ]]; then
   read -ra filename <<< "$(cat $nodes_list | grep "SYST_NODE-AMC-")"
elif [[ $MOTHERBOARD == "AMC_C5" ]]; then
   #  ####
   PCB=$(mother_pcb $ip_address)
   fflash=$(fdlm $ip_address | awk '{print$8}')
   read -ra filename <<< "$(cat $nodes_list | grep "SYST_NODE-AMC_C5-" | grep "pcb=$PCB")"
   read FLASH < <(echo $fflash)
   #read -ra filename <<< "$(cat $nodes_list | grep "SYST_NODE-AMC_C5-")"
   #  ####
   if [[ $FLASH == "EPCS128" ]]; then
      epcs_ver="128"
   else
      epcs_ver="64"
   fi
elif [[ $MOTHERBOARD == "MASTER" ]]; then
   read -ra filename <<< "$(cat $nodes_list | grep "MASTER")"
   epcs_ver="128"
else
   fping=$(faster_ping $ip_address 1 --verbose)
   read FASTER PING IP NODE ARROW CLOCK COLON TIME SECOND MOTHERBOARD NODETYPE < <(echo $fping)
   if [[ $MOTHERBOARD == "V2" ]]; then
      read -ra filename <<< "$(cat $nodes_list | grep "${NODETYPE}-V2-")"
   elif [[ $MOTHERBOARD == "C5" ]]; then
      read -ra filename <<< "$(cat $nodes_list | grep "${NODETYPE}-C5-")"
      epcs_ver="128"
   else
      echo "    Mother board type not recognized, bye."
      exit 1
   fi
fi

if ! [ $filename ]; then
   echo "   Not found in hardware_nodes.list, bye."
   exit $EXIT_FILE_NOT_FOUND
fi

filename=$RBF_PATH/$filename

epcs_prog $ip_address $filename 1 $basad $epcs_ver

#  enum returncode_e {
#    RC_OK             = 0,
#    RC_ERR            = 1,
#    //
#    RC_FILE_ERR       = 10,
#    //
#    RC_SOCKET_ERR     = 20,
#    RC_READ_ERR       = 21,
#    RC_COUNT
#  };

Error=$?
if [[ $Error == "0" ]]; then
   echo "    Version ok."
   echo ""
   exit 0
else
   echo "==> Erasing the flash memory, please wait ..."
   epcs_erase $ip_address $filename 0 $basad $epcs_ver
   echo "==> Programming new version ($filename), please wait ..."
   epcs_prog $ip_address $filename 0 $basad $epcs_ver
   echo "==> Checking the new flash memory firmware, please wait ..."
   epcs_prog $ip_address $filename 1 $basad $epcs_ver
   epcs_ret=$?
   if [[ $no_off_on == "" ]]; then
      if [[ $epcs_ret == "0" ]]; then
         echo ""
         echo "==> Off/On your device to use the new firmware."
         echo ""
         exit $epcs_ret
      else
         echo "==> Programming error, Don't swith your module off ..."
         echo "    "
         exit $epcs_ret
      fi
   fi
   exit $epcs_ret
fi
