#!/bin/bash

EXIT_UNKNOWN_BOARD=41
EXIT_PING_FAIL=42
EXIT_FILE_NOT_FOUND=43

# other exit codes after epcs_prog :
#  enum returncode_e {
#    RC_OK             = 0,
#    RC_ERR            = 1,
#    //
#    RC_FILE_ERR       = 10,
#    //
#    RC_SOCKET_ERR     = 20,
#    RC_READ_ERR       = 21,
#    RC_COUNT
#  };


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

function display_usage()
{
  echo ""
  echo "  usage :"
  echo "           $0  ip_address"
  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
echo_off=$2
if [[ $ip_address == "" ]]; then
   ip_address=192.168.1.204
fi

fping=$(faster_ping $ip_address 0 --verbose) # beware : faster_ping returns 0 on success !
[ $? -ne 0 ] && {
	echo ping "$ip_address" failed, exiting.
	exit $EXIT_PING_FAIL
}

read FASTER PING IP NODE ARROW CLOCK COLON TIME SECOND MOTHERBOARD NODETYPE < <(echo $fping)

if [[ $echo_off == "" ]]; then
   if [[ $NODETYPE == "SYST_NODE" ]]; then
      echo ""
      echo "    Mother board : $MOTHERBOARD"
   else
      echo ""
      echo "    Unknown board, bye."
      echo ""
      exit $EXIT_UNKNOWN_BOARD
   fi
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)
   read -ra filename <<< "$(cat $nodes_list | grep "SYST_NODE-AMC_C5-" | grep "pcb=$PCB")"
   #read -ra filename <<< "$(cat $nodes_list | grep "SYST_NODE-AMC_C5-")"
   #  ####
   epcs_ver="128"
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
      if [[ $echo_off == "" ]]; then
         echo "    Mother board type "$MOTHERBOARD" not recognized, bye."
      fi
      exit $EXIT_UNKNOWN_BOARD
   fi
fi

if ! [ $filename ]; then
   if [[ $echo_off == "" ]]; then
      echo "   Not found in hardware_nodes.list, bye."
   fi
   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
   if [[ $echo_off == "" ]]; then
      echo "    Version ok."
      echo "    "
   fi
   exit 0
else
   if [[ $echo_off == "" ]]; then
      echo "    Firmware should be updated ..."
      echo "    "
   fi
   exit $Error
fi

