#!/bin/bash
#-----------------------------------------------------------------------------
#
#   \brief   CheckUpdateSuccess()
#
#   Function checks if updating was successful.
#
#   \param[in]     none
#   \param[out]    none
#   \param[in,out] none
#
#   \return
#   possible return value(s) and description
#   return 0 -> C_NO_ERROR
#   return 1 -> C_UNKOWN_ERROR
#
#   \created    30.08.2013  STW/A.Appelt
#
#-----------------------------------------------------------------------------

function CheckUpdateSuccess()
{
   # wait for system to be booted
   if (! waitForString "Have a lot of fun..."); then
      echo -e "\nUpdate was not successful."
      return 1
   fi

   sleep 1

   if [ "$VarPrepareNandFlashOnly" == false ]; then
      # read in: $VarTmpFile
      tmp_input=$( grep -o "Update data loaded" "$script_dir/$VarTmpFile" )
      if [ -z "$tmp_input" ]; then
         echo -e "\nUpdate was not successful."
         return 1
      fi
   fi

   echo -e "\n$VarTargetName is started.\n"

   # start buffering RS232 traffic
   OUTPUT=`cat $serial_port > "$script_dir/$VarTmpFile" &`
   sleep 0.1

   # login as root
   echo "root" > $serial_port

   # wait for TC3G promt
   if (! waitForPromt "#"); then
      echo -e "\nError: Timeout!"
      echo -e "Unable to find $VarTargetName promt.\n"
      return 1
   fi

   if [ "$VarPrepareNandFlashOnly" == false ]; then
      # start buffering RS232 traffic
      OUTPUT=`cat $serial_port > "$script_dir/$VarTmpFile" &`
      sleep 1

      # read out eeprom status variable
      echo "stw_eep -r udstat" > $serial_port

      # wait for TC3G promt
      if (! waitForPromt "#"); then
         echo -e "\nError: Timeout!"
         echo -e "Unable to find $VarTargetName promt.\n"
         return 1
      fi

      # check if status = success
      if (! grep -o "SUCCESS:" "$script_dir/$VarTmpFile"); then
         echo -e "Error: Update was not successful."
         return 1
      fi

      echo -e "Update FIT image was successful.\n"
   fi
return 0
}

# function to search for a string
function waitForString()
{
#   StartTime=$(date +%s)
   tail -f "$script_dir/$VarTmpFile" |
   while read line; do
#      CurrTime=$(date +%s)
#      TimeDiff=$(( $CurrTime - $StartTime ))
#      if [ $TimeDiff -ge 50 ];
#      then
#         killall tail
#         return 1
      if test "${line#*"$1"}" != "$line"
      then
         killall tail 2>/dev/null
         return 0
      elif test "${line#*"aborting auto-update"}" != "$line"
      then
         killall tail 2>/dev/null
         sleep 0.5
         echo -e "\033[31m\n\nError: Cannot update via tftp. Please check network settings!\033[37m"
         return 1
      elif test "${line#*"No Update data available"}" != "$line"
      then
         killall tail 2>/dev/null
         sleep 0.5
         echo -e "\033[31m\n\nError: Cannot update via usb. Make sure flash drive is connected!\033[37m"
         return 1
      fi
   done
   OUT=$?
#   CurrTime=$(date +%s)
#   TimeDiff=$(( $CurrTime - $StartTime ))
   if [ "$OUT" == 0 ]; then #-a $TimeDiff -le 50 ]; then
      killall cat & > /dev/null &
      return 0
   fi
   killall cat & > /dev/null &
return 1
}

