#!/bin/bash

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

function display_usage()
{
  echo ""
  echo "usage :"
  echo "         CARAS"
  echo "         faster_syroco_reprog  RF_CARAS"
  echo "         faster_syroco_reprog  QDC_CARAS"
  echo "         faster_syroco_reprog  CRRC4_CARAS"
  echo "         faster_syroco_reprog  TRAPEZOIDAL_CARAS"
  echo "         faster_syroco_reprog  QT2T"
  echo "         faster_syroco_reprog  SCALER"
  echo "         faster_syroco_reprog  SAMPLER"
  echo "         faster_syroco_reprog  NULL_NODE"
  echo "         MOSAHR"
  echo "         faster_syroco_reprog  CRRC4_MOSAHR"
  echo "         faster_syroco_reprog  TRAPEZOIDAL_MOSAHR"
  echo "         faster_syroco_reprog  ..."
  echo "         faster_syroco_reprog  -c"
  echo ""
  echo "options :"
  echo "         -c check if the version of the firmware is up-to-date "
  echo "         -v display the mnms version "
  echo ""
}

caras_mnms="RF_CARAS QDC_CARAS CRRC4_CARAS TRAPEZOIDAL_CARAS QT2T SCALER SAMPLER NULL_NODE"
mosahr_mnms="CRRC4_MOSAHR TRAPEZOIDAL_MOSAHR"
######################################################################################


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

nodes_list=$RBF_PATH/hardware_nodes.list
ip_address=192.168.1.204
ping_timeout=1
force=0

name=$1
if [[ $name == "-f" ]]; then
   force=1
   name=$2
fi

if [[ $name == "-c" ]]; then
   echo $(faster_mother_board_firmware_check $ip_address)
   echo " "
   exit
elif [[ $name == "-v" ]]; then
   echo $(faster_ping $ip_address 1 --verbose)
   echo " "
   exit
elif [[ $name == "" ]]; then
   display_usage
   exit
fi

no_ping=$(ping -w $ping_timeout $ip_address | grep " 0 received")

if [[ $no_ping ]]; then
   echo "Error : can't connect to syroco $ip_address"
   echo ""
   exit
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
      filename=$(cat $nodes_list | grep "${name}-V2-" | awk '{print $1}')
      svn_version=$(cat $nodes_list | grep "${name}-V2-" | awk '{print $2}')
   elif [[ $MOTHERBOARD == "C5" ]]; then
      filename=$(cat $nodes_list | grep "${name}-C5-" | awk '{print $1}')
      svn_version=$(cat $nodes_list | grep "${name}-C5-" | awk '{print $2}')
   else
      echo "Error : device at $ip_address isn't a Syroco board."
      echo ""
      exit
   fi
fi


for mnms in $mosahr_mnms
do
   if [[ $NODETYPE == $mnms ]]; then
      daughterboard="MOSHAR"
   fi
done

for mnms in $caras_mnms
do
   if [[ $NODETYPE == $mnms ]]; then
      daughterboard="CARAS"
   fi
done

for mnms in $caras_mnms
do
   if [[ $name == $mnms ]]; then
     if [[ $daughterboard == "MOSAHR" ]]; then
        echo "Error : device at $ip_address has a MOSAHR daughterboard"
        echo ""
        exit
      fi
   fi
done


for mnms in $mosahr_mnms
do
   if [[ $name == $mnms ]]; then
      if [[ $daughterboard == "CARAS" ]]; then
         echo "Error : device at $ip_address has a CARAS daughterboard"
         echo ""
         exit
      fi
   fi
done

for mnms in $caras_mnms
do
   if [[ $name == $mnms ]]; then
     if [[ $NODETYPE == *"MOSAHR"* ]]; then
        echo "Error : device at $ip_address has a MOSAHR daughterboard"
        echo ""
        exit
      fi
   fi
done

for mnms in $mosahr_mnms
do
   if [[ $name == $mnms ]]; then
      if [[ $NODETYPE == *"CARAS"* ]]; then
         echo "Error : device at $ip_address has a CARAS daughterboard"
         echo ""
         exit
      fi
   fi
done

for mnms in $caras_mnms
do
   if [[ $name == $mnms ]]; then
     if [[ $NODETYPE == *"MOSAHR"* ]]; then
        echo "Error : device at $ip_address has a MOSAHR daughterboard"
        echo ""
        exit
      fi
   fi
done

if [ -f "$RBF_PATH/$filename" ]; then
   true
else
   echo "Error : binary file not found"
   echo ""
   exit
fi

if [[ $(grep $filename $nodes_list | awk '{print $1}') == $filename ]]; then
   true
else
   echo "Error : binary file not defined in hardware nodes list"
   echo ""
   exit
fi
filename=$RBF_PATH/$filename

echo ""
echo "==> REPROGRAMMING Syroco $name"

faster_mother_board_firmware_check $ip_address

if [[ $? == 1 ]]; then
   force=1
fi

if [[ $force == "0" ]]; then
   if [[ $NODETYPE == $name ]]; then
      echo "Your Syroco is already configured as $name."
      echo "The reprogrammation can be forced with \"$0 -f $name\"."
      echo ""
      exit
   fi
fi

echo ""
echo -n "==> WARNING, you are going to reprogram the firmware of a Syroco with the $name function, continue? [Y|n]"; read Yes
if [[ $Yes == "Y" ]] || [[ $Yes == "y" ]] || [[ $Yes == "" ]]; then
   if [[ $MOTHERBOARD == "V2" ]]; then
      #echo "epcs_prog $ip_address $filename"
      echo "==> Erasing the flash memory, please wait ..."
      epcs_erase $ip_address $filename
      echo "==> Programming new version, please wait ..."
      epcs_prog  $ip_address $filename
   elif [[ $MOTHERBOARD == "C5" ]]; then
      #echo "epcs_prog $ip_address $filename 0 0x40 128"
      echo "==> Erasing the flash memory, please wait ..."
      epcs_erase $ip_address $filename
      echo "==> Programming new version, please wait ..."
      epcs_prog $ip_address $filename 0 0x40 128
   else
      echo "!!!! IMPOSSIBLE ERROR HAPPENED !!!!"
      echo "==> Aborting, bye."
      exit 1
   fi
   echo "==> Checking flash memory firmware, please wait ..."
   epcs_prog $ip_address $filename 1 $basad $epcs_ver
   epcs_ret=$?
   echo "    Done ..."
   if [[ $epcs_ret == "0" ]]; then
      echo ""
      echo "==> Power off and on the module to load the new firmware"
      echo ""
   else
      echo ""
      echo "==> Programming error, Don't swith your module off ..."
      echo ""
    fi
else
   echo "==> Aborting, bye."
fi

